A few n00b questions

I have a few questions to ask, so i'll ask them all here. :P

I'm building a program that will get a string like (010305060405 e.t.c.). They are co-ordinates in the form of XXYYXXYY e.t.c. how can i split them to 4 characters and then their respective XX and YY co-ordinates to be added to an array I've researched string.split and couldn't find anything that helped me.

Also, what would be the best way of showing images for these values in a grid I want to show them as images in a grid that could be up to 99 x 99 cells (maximum, bare basic is something like 50 x 50 or 60 x 40 or 70 x 30 [default sizes]). And how would i go about doing that And would it be possible to see if a usr has clicked on a cell and to then perform an event, like change the image or remove it from that cell If i get the information about how to add the images i should be able to code the refreshes myself when the values change, but if i get stuck i'll be back! Also with that, how would i be able to check if a cell contains an image, and if the image is one type or another

Sorry for being such a n00b, it's not as easy as you think coming from PHP to VB! I keep putting ; after my lines, which gets annoying :P



Answer this question

A few n00b questions

  • bessermt

    weirdbeardmt wrote:
    rkimble wrote:

    'Exponse the list of tiles as a read-
    'only property
    Public ReadOnly Property Tiles() As List(Of Tile)

    Exponse is my new favourite word! :D

    LMAO
    I'm not even sure how that typo was possible! The answer may be at the bottom of that bottle of Chivas I threw away last night...



  • DRoden

    OK to split function can be accomplished using the substring or mid$ functions and numerous ways of achieving but a general idea on this would be. You can put more validation in to make it more secure but this shows the general concept.

    Module Module1

    Sub Main()
    Dim a As String = "12345678"
    Dim x1 As Integer
    Dim X2 As Integer
    Dim Y1 As Integer
    Dim Y2 As Integer

    SplitFunction(a, x1, Y1, X2, Y2)

    End Sub

    Function SplitFunction(ByVal a As String, ByRef x1 As Integer, ByRef y1 As Integer, ByRef x2 As Integer, ByRef y2 As Integer) As Boolean
    Dim bProblem As Boolean = False
    If a.Length = 8 Then
    SplitandProcessItem(a, 1, bProblem, x1)
    SplitandProcessItem(a, 2, bProblem, y1)
    SplitandProcessItem(a, 3, bProblem, x2)
    SplitandProcessItem(a, 4, bProblem, y2)

    If bProblem = True Then
    x1 = 0
    x2 = 0
    y1 = 0
    y2 = 0
    End If
    End If
    End Function

    Function SplitandProcessItem(ByVal a As String, ByVal i As Integer, ByRef bproblem As Boolean, ByRef v As Integer)
    Dim svalue As String = ""
    svalue = a.Substring((i - 1) * 2, 2)
    If IsNumeric(svalue) And bproblem = False Then
    v = CType(svalue, Integer)
    Else
    bproblem = True
    End If
    End Function

    End Module

    As to positioning graphics. You are basically going to calculate a coordinates and then either create a new picture box and set its left and top properties to a calculated point.

    The following will show coordinates of basically a grid with the appropriate values assuming each cell is 100 * 100 pixels. Then you have a function which is used to calculate the pixels from a cell reference. So you could create a new picturebox and then set its coordinates.

    Also remember the origin will be top/left and not bottom/left as you might expect and you may need to include an intial offset so that the first cell isnt immediately in the top / left of the window.
    This is only intended to demonstrate principles.

    Module Module1
    Const xstep As Integer = 100
    Const ystep As Integer = 100

    Sub Main()

    '//Show X, Y Coordinates for a grid
    Dim x As Integer, y As Integer
    For x = 0 To 1000 Step xstep
    For y = 0 To 1000 Step ystep
    Console.WriteLine(x.ToString & "," & y.ToString)
    Next
    Next

    '//Calculate top, left coordinates for a variaty of cells in this grid
    Dim xpos, ypos As Integer
    calculatePosition(0, 0, xpos, ypos)
    Console.WriteLine("0,0," & xpos.ToString & "," & ypos.ToString)
    calculatePosition(5, 5, xpos, ypos)
    Console.WriteLine("5,5," & xpos.ToString & "," & ypos.ToString)
    calculatePosition(2, 5, xpos, ypos)
    Console.WriteLine("2,5," & xpos.ToString & "," & ypos.ToString)
    calculatePosition(10, 10, xpos, ypos)
    Console.WriteLine("10,10," & xpos.ToString & "," & ypos.ToString)
    calculatePosition(3, 5, xpos, ypos)
    Console.WriteLine("3,5," & xpos.ToString & "," & ypos.ToString)

    Dim ioffsetx as integer = 500
    Dim ioffsety as integer = 300

    'Create a picturebox as a specific coordinate and with a loaded image
    ' Including an offset

    calculatePosition(3, 5, xpos, ypos)
    Dim x as new picturebox
    with x
    .left = xpos + ioffsetx
    .top = ypos + ioffsety
    .Load("test.jpg")
    end with

    End Sub

    Function calculatePosition(ByVal xcell As Integer, ByVal ycell As Integer, ByRef xpos As Integer, ByRef ypos As Integer)
    xpos = (xcell) * xstep
    ypos = (ycell) * ystep
    End Function

    End Module


  • Tamas Pocker

    rkimble wrote:

    'Exponse the list of tiles as a read-
    'only property
    Public ReadOnly Property Tiles() As List(Of Tile)

    Exponse is my new favourite word! :D



  • abhishek_6023

    Basically you want to create a tile rendering device. I'm not sure how you're determining what image goes with what location, but the following code is a simple custom control that renders a collection of tiles and raises an even when one is clicked.

    'Add a new CustomControl to the project

    Public Class TileControl

    'Create a private list of tiles to hold
    'the tiles to be rendered by the control
    Private myTiles As New List(Of Tile)

    'Declare the event that indicates a tile
    'has been clicked
    Public Event TileClicked As EventHandler(Of TileClickedEventArgs)

    'Exponse the list of tiles as a read-
    'only property
    Public ReadOnly Property Tiles() As List(Of Tile)

    Get

    Return myTiles

    End Get

    End Property

    'Create a routine that parses your string - this
    'version sets all tiles to the same image, you
    'could add an overload that takes an array or list
    'of images or a list of Tiles
    Public Sub LoadTiles(ByVal points As String, ByVal img As Image)

    Dim index As Integer
    While index <= points.Length - 4

    myTiles.Add(New Tile((New Point( _
    Integer.Parse(points.Substring(index, 2)), _
    Integer.Parse(points.Substring(index + 2, 2)))), img))
    index += 4

    End While

    End Sub

    'Draw each tile in the OnPaint event
    Protected Overrides Sub OnPaint(ByVal pe As System.Windows.Forms.PaintEventArgs)

    MyBase.OnPaint(pe)
    'Add your custom paint code here

    For Each t As Tile In myTiles

    pe.Graphics.DrawImage(t.Image, t.Location)

    Next

    End Sub

    'Check to see if a tile was clicked
    Private Sub TileControl_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click

    'Get a reference to the tile control
    Dim tc As TileControl = CType(sender, TileControl)
    'Check each tile

    For Each t As Tile In myTiles

    'Create a rectange the size and position of the tile
    Dim trect As New Rectangle(t.Location, t.Image.Size)
    'Create a rectange 1 pixel by 1 pixel at the position
    'of the mouse click relative to the control
    Dim click As New Rectangle(tc.PointToClient(Control.MousePosition), New Size(1, 1))
    'If the two rectangles intersect, then the user
    'clicked this tile

    If click.IntersectsWith(trect) Then

    'Raise the event
    RaiseEvent TileClicked(Me, New TileClickedEventArgs(t))
    'Since only one tile can be clicked, exit
    'the routine
    Exit For

    End If

    Next

    End Sub

    End Class

    'Create the Tile object
    Public Class Tile

    Public Location As Point
    Public Image As Image

    Public Sub New()

    MyBase.New()

    End Sub

    Public Sub New(ByVal loc As Point, ByVal img As Image)

    MyBase.New()
    Me.Location = loc
    Me.Image = img

    End Sub

    End Class

    'Create an EventArgs for the click event
    Public Class TileClickedEventArgs

    Inherits System.EventArgs
    Public Tile As Tile

    Public Sub New()

    MyBase.New()

    End Sub

    Public Sub New(ByVal t As Tile)

    MyBase.New()
    Me.Tile = t

    End Sub

    End Class

    This CustomControl is very basic but it demonstrates how to do the various things you've asked. Look into GDI or DirectX for more information on drawing.



  • Zulbaric

    The string splitting could be done by utilizing the Point and List(Of T) classes. The function would look something like this

    Public Function ParsePointString(ByVal points As String) As Point()
    Dim ret As New List(Of Point)
    Dim index As Integer

    'make sure there is a whole point left to parse
    While index < points.length - 4
    ret.Add(New Point( _
    Integer.Parse(points.Substring(index, 2)), _
    Integer.Parse(points.Substring(index + 2, 2))))
    index += 4
    End While

    Return ret.ToArray
    End Function


    I'm not sure I understand the other part of your problem, but you may want to take a look at multidimensional or jagged arrays.

  • martinx1

    That string splitting is a good idea, but what i didn't mention is that I won't actually know the length of the string because it could be any number of cell refrecences, therefore i can't state how many X and Y values i want out of it, so i was thinking of an array with all the X values and an array with all the Y values, each should have their own ID then, and should match up with the ID of the one in the other array. Would this be a similar process (Arrays are good, as is explained about in the next paragraph)

    Sorry if i didn't explain myself that well, when i said 99 x 99 cells, i didn't mean size, i meant 99 cells on the X axis and 99 cells on the Y axis (maximum, meaning 9801 cells in total), the cells would only need to be 6 x 6 pixels in size to fit the images. I was afraid you were going to put up a code like that. Would it be possible to do all the image drawing and checking if there is an image in a cell next to it and calculating what cell was clicked to change the image in a cell. I have the code for managing the datagrid already in my program so it would be good if i could use that instead of trashing it, because the program changes it's size and look if the grid is going to be large. Actually, i was just thinking, if all the values were in an array i wouldn't have to check to see if they were on the datagrid, but i would still need to be able to check where a user has clicked and see if there is an image there, and if so change the image or remove it, depending on it's current status. Also, i forgot to mention, but once it's drawn it doesn't stop there, it does a bit of coding a changes a few things, so i'm not sure if your code would take to nicely to changes, i'm no expert, but i think a datagrid might be useful.

    Actually, nevermind about drawing images, i found this topic: http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=507071&SiteID=1

    But i still need to know how to calculate what cells are clicked e.t.c.

    Any help you can give is greatly appreciated, sorry for not being so clear before.


  • A few n00b questions