Button Array Question

Hi

I have the following button array:

MachineryTotal = DS_Machinery.Tables(0).Rows.Count
Dim ButtonArray(MachineryTotal) As button

For x As Integer = 0 To MachineryTotal - 1
            ButtonArray(x) = New button

            If Xaxis > 880 Then
                Xaxis = 400
                Yaxis = Yaxis + 75
            End If

            With ButtonArray(x)
                .Tag = x
                .Name = "BTN_Mach" & x
                .Text = DS_Machinery.Tables(0).Rows(x).Item("MachineName")
                .Size = New System.Drawing.Size(100, 48)
                .Location = New System.Drawing.Point(Xaxis, Yaxis)
                .Parent = Me
                .Visible = True
                .Enabled = False
                .BackColor = System.Drawing.Color.DodgerBlue
                .ForeColor = System.Drawing.Color.Black
                AddHandler .Click, AddressOf ButtonHandler
            End With
            Xaxis = Xaxis + 120
Next
Xaxis = 400
Yaxis = 100

How can I get access to these buttons once they are created. For example, Say if I wanted to change the backcolor of one of the buttons in the array using the click event of another button

Public Sub BTN_TestArray_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTN_TestArray.Click
        Me.ButtonArray(2).BackColor = System.Drawing.Color.Bisque
End Sub

This gives error: "Object reference not set to an instance of an object." a System.NullReferenceException
How do I do this???
I am offering 500 points for this as I need a solution quickly

Thanks
daly__paulAsked:
Who is Participating?
 
bchoorConnect With a Mentor Commented:
Instead of Private ButtonArray() As button

Try this

Dim ButtonArray(0) As Button ' this is still outside of the method

then

ReDim ButtonArray(.........

Give this a shot, I'm a bit baffled since the array is already declared.

If this does not work, then the other option is to either use an ArrayList or HashTable

So Outside your method
Private ArrLstButtons As New ArrayList

Inside your Method
ArrLstButtons.Items.Clear

' Add your item
dim but As New Button
With but
   .backcolor....

   AddHandler .click,....

End with

ArrLstButtons.Add(But)


' -- In your click handler
' To retrieve Index 5 (so 6th button)
Dim but as Button = CType(Me.ArrLstButtons.Item(5), Button)
but.Backcolor = System.Drawing.Color.Bisque

HTH
~BC





0
 
bchoorCommented:
Just change scope of
Dim ButtonArray(MachineryTotal) As Button

to allow it to be used in other methods within the class
Private ButtonArray(MachineryTotal) As Button ' Right inside your class but outside your method

HTH
~BC

0
 
bchoorCommented:
Acutally u may need to do in 2 steps

Private ButtonArray() As Button  ' This is outside of your method


'In your method
Redim ButtonArray(MachineryTotal) As Button


HTH
~BC
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
Fernando SotoRetiredCommented:
Hi daly__paul;

Thios should solve your immediate problem.

    ButtonArray(2).BackColor = System.Drawing.Color.Bisque

Removing the Me. I do not know at this time why, but I am looking into it.

Fernando
0
 
Fernando SotoRetiredCommented:
Hi daly__paul;

I had an error in the code I was testing. My last post is wrong.

bchoor Has it correct.
0
 
daly__paulAuthor Commented:
Bchoor,

I know what you mean about declaring 'Private ButtonArray() As Button' This is outside of your method

but here is a cut down version of my code

Public Class FRM_FactoryFloor

Private ButtonArray() As button <-- Declare an empty array (MachineryTotal is not known yet!! )

Public Sub AddButtons()

MachineryTotal = DS_Machinery.Tables(0).Rows.Count
Dim ButtonArray(MachineryTotal) As button <-- Declare size of array (This could change if machinery is added or deleted from database)

  For x As Integer = 0 To MachineryTotal - 1
            ButtonArray(x) = New button

            If Xaxis > 880 Then
                Xaxis = 400
                Yaxis = Yaxis + 75
            End If

            With ButtonArray(x)
                .Tag = x
                .Name = "BTN_Mach" & x
                .Text = DS_Machinery.Tables(0).Rows(x).Item("MachineName")
                .Size = New System.Drawing.Size(100, 48)
                .Location = New System.Drawing.Point(Xaxis, Yaxis)
                .Parent = Me
                .Visible = True
                .Enabled = False
                .BackColor = System.Drawing.Color.DodgerBlue
                .ForeColor = System.Drawing.Color.Black
                AddHandler .Click, AddressOf ButtonHandler
            End With
            Xaxis = Xaxis + 120
  Next
Xaxis = 400
Yaxis = 100
end sub

Public Sub BTN_TestArray_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTN_TestArray.Click
        Me.ButtonArray(2).BackColor = System.Drawing.Color.Bisque
End Sub
End class

The 'Redim ButtonArray(MachineryTotal) As Button' give a complilation error " 'ReDim' statements can no longer be used to declare array variables."

Is there anyway to make the first declaration dynamic so that it can change size? I could then eliminate the declaration in the AddButtons().

Daly__paul
0
 
Fernando SotoConnect With a Mentor RetiredCommented:

This Declare Hides the one you declare at class level.
Dim ButtonArray(MachineryTotal) As button <-- Declare size of array (This could change if machinery is added or deleted from database)

It should be

ReDim ButtonArray(MachineryTotal) As Button

That is why you were getting the error. Because you were placeing the Buttons in the local array and not the class array.

Fernando
0
 
daly__paulAuthor Commented:
Well Done!!

I Used

Dim ButtonArray(0) As Button ' this is still outside of the method

then...

ReDim ButtonArray(MachineryTotal) ' It give an error if you use ReDim ButtonArray(MachineryTotal) as button dropped the AS BUTTON and it works

Both of you's got the answer so it is only fair that the points are split accordingly

Thanks Guys

daly__paul
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.