?
Solved

Button Array Question

Posted on 2006-05-25
8
Medium Priority
?
182 Views
Last Modified: 2010-04-23
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
0
Comment
Question by:daly__paul
  • 3
  • 3
  • 2
8 Comments
 
LVL 10

Expert Comment

by:bchoor
ID: 16764048
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
 
LVL 10

Expert Comment

by:bchoor
ID: 16764055
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
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 16764197
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 64

Expert Comment

by:Fernando Soto
ID: 16764375
Hi daly__paul;

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

bchoor Has it correct.
0
 

Author Comment

by:daly__paul
ID: 16764439
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
 
LVL 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 800 total points
ID: 16764641

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
 
LVL 10

Accepted Solution

by:
bchoor earned 1200 total points
ID: 16764681
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
 

Author Comment

by:daly__paul
ID: 16764831
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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Screencast - Getting to Know the Pipeline
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month16 days, 19 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question