Changing the checked state of Dynamically added checkboxes

D

Dan H

I have been able to add controls dyanmically to a windows application.
And I can get the Click event working beautifully. But now I want to
use an another event to change the state of the check box but I can
seem to figure out how to work with the control.

So in the code below I create a bunch of check boxes and I want the
event called IFK_InputChange (where I have an external hardware device
that I use as IFK, it works no problems) to update the chkInput check
boxes that I have created.

Any suggestions would be appreciated.



Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MyBase.Load

End Sub

Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
For i As Integer = 0 To IFK.GetNumInputs - 1
Dim chkInput As New CheckBox
chkInput.Location = New Drawing.Point(110, 170 + i * 20)
chkInput.TabIndex = i
chkInput.Tag = i.ToString
chkInput.Name = "input" + i.ToString
chkInput.Text = chkInput.Tag.ToString
AddHandler chkInput.Click, AddressOf ClickInputs
Controls.Add(chkInput)
Next

End Sub

Private Sub ClickInputs(ByVal sender As Object, ByVal e As
EventArgs)
MessageBox.Show("Clicked was " & DirectCast(sender,
CheckBox).Tag.ToString)
End Sub

Private Sub IFK_InputChange(ByVal sender As Object, ByVal e As _
InputChangeEventArgs) Handles IFK.InputChange
MessageBox.Show(e.getIndex & " is " & e.getState)
End Sub

End Class
 
C

Cor Ligthert [MVP]

Dan,

You mean something as beneath typed in this message so watch typos.
\\\
Public sub SetCheckBox(byval chkstate as Boolean, byval checkboxindex as
integer)
For each ctr as Control in me.controls
if Typeof ctr Is CheckBox then
dim chk as checkbox = ctr
if chk.name = "input" & checkboxindex.tostring then
chk.checked = chkstate
end if
end if
end for
///
(You can as well create an arraylist of refrences to the textboxes you use
global in your program)

I hope this helps,

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top