My project I am working on is a product location system for a large supermarket. My current issue is that the map I am using to display the product location is assembled from rectangle objects which I will then change the colour of when required.

The array storing the locations is dimensioned as LocationArray(x) Where x is equal to the max rows function used on a table in database. Stored information always proceeded with "loc_" and the location. All rectangles are named in accordance with this.

I wish to pull the field name stored in the array (which corresponds to the rectangle to change colour) and be able to change its colour. This is giving me issue as the straight forward command of "LocationArray(x).Color = #" doesnt work as an array does not inherit this property.

Can anyone know how I am able to pull from the array and then use the pulled data to manipulate object it refers to.

Many Thanks in advance,
Mike

Recommended Answers

All 9 Replies

See if this helps.
2 Buttons.

Public Class Form1
    Private LocationArray() As String = {"5,40,50,100", "75,125,30,45", "150,90,50,75", "75,40,180,25", "5,200,250,25"}
    Private penBlack As New Pen(Color.Black, 3) '// Default Color.
    Private penRed As New Pen(Color.Red, 3) '// Selection Color.
    Private arTemp() As String = Nothing '// .Split Locations Strings into(location.X, location.Y, width, height)

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Visible = True
        drawRectangles()
    End Sub

    '// Draw ALL Rectangles as Default Color.
    Private Sub drawRectangles()
        Dim myGraphics As Graphics = Me.CreateGraphics
        For Each rect As String In LocationArray '// Loop thru all Locations Strings.
            arTemp = rect.Split(",") '// Split into 4 Arrays.
            myGraphics.DrawRectangle(penBlack, CInt(arTemp(0)), CInt(arTemp(1)), CInt(arTemp(2)), CInt(arTemp(3))) '// draw Rectangle.
        Next
    End Sub

    '// Draw ALL Rectangles as Default Color AndAlso Draw the SELECTED Rectangle as Selection Color.
    Private Sub drawSelectedRectangle(ByVal RectangleLocationAndSizeFromStringArray As String)
        drawRectangles() '// Redraw all Rectangles to set ALL to Default Color.
        Dim myGraphics As Graphics = Me.CreateGraphics
        arTemp = LocationArray(RectangleLocationAndSizeFromStringArray).Split(",") '// Split into 4 Arrays.
        '// Redraw a Selected Rectangle, with the Selection Color.
        myGraphics.DrawRectangle(penRed, CInt(arTemp(0)), CInt(arTemp(1)), CInt(arTemp(2)), CInt(arTemp(3))) '// draw Rectangle.
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        drawSelectedRectangle(3) '// 4th in LocationArray().
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        drawSelectedRectangle(1) '// 2nd in LocationArray().
    End Sub
End Class

Was debating whether or not to use this code at first due to the nature of the complexity of mapping out an entire grid using this method.

Must firstly apologise for the very slow reply, i have been distracted by other aspects of life and havn't really had a chance to get back to looking at this part of my project.

I must question however, what do the four numbers in the location array represent? Co-ordinates? or a measurement of some kind?

Also is this method viable considering i am using approx. 100-150 rectangles :)


Many thanks and apologies again,
Mike

The 4 numbers of each rectangle are: "location.X, location.Y, Width, Height"

As for it being viable, I cannot decide that for you. You test what options are best and apply those.
I only offered a suggestion or a possible solution.

Ok thank you for that, i will need to debate the recoding of my entire grid now hehe :)

Will mark thread as solved as this is what im looking for, great help.

Cheers,
Mike

Codeorder, one bit of the code is confusing me :)

At the very beginning when your are declaring the array contents, its done in a way i have never seen and so therefore adapting the code is proving a tad difficult.

How would one adapt the array contents and split to use a 2D array. The reasoning for the 2D array is simple i can match the second half of the array to my database before redrawing with first part of arrays dimensions.

I have tried simply removing the declaration of contents from the Dim statement and doing manually on form load for 2D however that didn't seem to work.

Any suggestions on how to convert the code?

Many thanks,
Mike

See if this helps.

Public Class Form1
    Private myGraphics As Graphics '// for Drawing.
    Private arRectangles(2, 2) As Rectangle '// Rectangle 2D Array.
    Private penBlack As New Pen(Color.Black, 3) '// Pen Default Color.

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '// add some Rectangles to your 2D Array.
        arRectangles(0, 0) = New Rectangle(5, 40, 50, 100) '// New Rectangle(load values from database here)
        arRectangles(0, 1) = New Rectangle(150, 90, 50, 75)
        arRectangles(1, 0) = New Rectangle(75, 40, 180, 25)
        arRectangles(2, 2) = New Rectangle(5, 200, 250, 25)
        myGraphics = Me.CreateGraphics '// initialize.
        Me.Show()
        '// Draw Rectangles.
        drawRectangle(0, 0) : drawRectangle(0, 1) : drawRectangle(1, 0) : drawRectangle(2, 2)
    End Sub

    Private Sub drawRectangle(ByVal selectedArray As Integer, ByVal selectedRectangle As Integer)
        myGraphics.DrawRectangle(penBlack, arRectangles(selectedArray, selectedRectangle)) '// draw Rectangle.
    End Sub
End Class

Is it possible to use an array setup which can store text as well as the rectangle information?

Cheers,
Mike

You could declare Private arRectangles(2, 2) As String and set values as: arRectangles(0, 0) = "5, 40, 50, 100" . Then you will need to use similar code as posted in my first reply to draw the Rectangles. The CInt part after splitting the String into 4 Arrays. CInt(arTemp(0)), CInt(arTemp(1)), CInt(arTemp(2)), CInt(arTemp(3))

Shall have an experiment with the code and solutions you have provided. Most appreciated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.