We help IT Professionals succeed at work.
Get Started

How to match Button array from a class to dynamically created button on a form

Last Modified: 2012-06-22
here is my code...
from this code i get a null reference exception...
all other labels,buttons.tooltips are already there the form1 and i got no problem ..the problem is only with the btn
that i want to match it with the dynamically created Button...
thank you very much...
Public Class md
    Public state As String
    Public snoozeIconBtn As Button
    Public lbl As Label
    Public pb As PictureBox
    Public stateLbl As Label
    Public tooltip As ToolTip
    Public hover As Label
    Public c As Integer
    Public sch(40) As timeSpan
    Public sch_count As Integer
    Public name As String
    Public active As Boolean
    Public alarmOn As Boolean
    Public attentionOn As Boolean
    Public Sensor As String
    Public GetSensorsName As String
    Public tr As Label
    Public rec As String
    Public btn As Button
    Private RedFlag As Boolean
    'public readonly property

    Public Sub New(ByVal i As Integer)

        Me.btn = Form1.fbtn(i)   '>>>>>>if fbtn(i) buttons are alreadey there , i can figure it out but there are not yet
        Me.lbl = Form1.lbl(i)
        Me.stateLbl = Form1.state(i)
        Me.tooltip = Form1.tooltip(i)
        Me.hover = Form1.hover(i)
        Me.c = i
        Me.sch_count = 0
        Me.snoozeIconBtn = Form1.snoozeIconBtn(i)
        Me.pb = Form1.Picturebox(i)
        Me.Sensor = Form1.sensor(i)
        Me.tr = Form1.act(i)
    End Sub
    Public Sub setAlarm(ByVal val As Boolean)
        If val = True Then
            RedFlag = True
            Me.btn.BackColor = Color.Red    '>>>> here i got the exception !!!!
            Me.alarmOn = True
            ' rec = tr.Text
            GetSensorsName = Me.btn.Text
            RedFlag = False
            Me.btn.BackColor = Color.Lime
            Me.alarmOn = False
            ' rec = tr.Text
            GetSensorsName = Me.btn.Text
        End If
    End Sub

'in my form

Public Class Form1

    Public md(NumberOfSensors) As md          '>>>>>>  NumberOfSensors is a public integer variable 
    Public fbtn(NumberOfSensors) As Button
    Public snoozeIconBtn(NumberOfSensors) As Button
    Public lbl(NumberOfSensors) As Label
    Public Picturebox(NumberOfSensors) As PictureBox
    Public state(NumberOfSensors) As Label
    Public tooltip(NumberOfSensors) As ToolTip
    Public hover(NumberOfSensors) As Label
    Public sensor(NumberOfSensors) As String
    Public act(NumberOfSensors) As Label

 Private Sub initMdArray()   'this sub is set to form load event
              For j = 0 To NumberOfSensors - 1
'here i initiallize also the other controls
            btn(j) = Me.fbtn(j + 1)

            md(j) = New md(j)
    End Sub

'form1 create buttons

For i As Integer = 0 To Rcount - 1

            Button.Name = "Button" & i + 1
            Button.Text = ds.Tables(0).Rows(i).Item(1)
            Button.Dock = DockStyle.Fill
            Button.FlatStyle = FlatStyle.Flat
            Button.Cursor = Cursors.Hand
            Button.BackColor = Color.White
                        fbtn(i) = Button      '<<<<<< is this correct   ??????
            dynamicTableLayoutPanel.Controls.Add(Button, 0, i)

Open in new window

Watch Question
Most Valuable Expert 2012
Top Expert 2014
This problem has been solved!
Unlock 1 Answer and 12 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE