Programatticaly programme control with events

G

Guest

I am adding a seriese of controls to 'panel1' on 'form1' This works well but
I can't find a way to enable events. This example adds a 10 x 10 grid of
check boxes.

How can I create an event that will run if any of the check boxes are ticked.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim x As Integer
Dim y As Integer
Dim pt As New System.Drawing.Point
Dim sz As New System.Drawing.Size
For x = 1 To 10
For y = 1 To 10
pt.X = x * 25 : pt.Y = y * 25
sz.Height = 17 : sz.Width = 17
myBox(x, y) = New CheckBox
If x = 1 And y = 1 Then myBox(1, 1) = cb
Panel1.Controls.Add(myBox(x, y))
With myBox(x, y)
.Text = ""
.Location = pt
.Size = sz
End With
Next y
Next x
Dim lbl As New LinkLabel
pt.X = 37 : pt.Y = 280
lbl.Text = "JF"
lbl.Location = pt
Panel1.Controls.Add(lbl)
End Sub
 
A

Armin Zingler

John F said:
I am adding a seriese of controls to 'panel1' on 'form1' This works
well but I can't find a way to enable events. This example adds a
10 x 10 grid of check boxes.

How can I create an event that will run if any of the check boxes
are ticked.

[...]
myBox(x, y) = New CheckBox

AddHandler mybox(x,y).CheckedChanged, addressof _____ 'Insert Name of
event handler


BTW, this is also explained in the docs:
http://msdn.microsoft.com/library/en-us/vbcn7/html/vaconEventsDelegatesInheritance.asp
(also available via <F1>)


Armin
 
P

Peter Proost

Hi you can use addhandler for that

for example

Private Sub CheckBox_CheckedChanged(ByVal sender As Object, ByVal e As
System.EventArgs)
msgbox(directcast(sender,CheckBox).Checked)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim x As Integer
Dim y As Integer
Dim pt As New System.Drawing.Point
Dim sz As New System.Drawing.Size
Dim myChk As CheckBox
For x = 1 To 10
For y = 1 To 10
myChk = New CheckBox
AddHandler myChk.CheckedChanged, AddressOf
CheckBox_CheckedChanged
pt.X = x * 25 : pt.Y = y * 25
sz.Height = 17 : sz.Width = 17
With myChk
.Text = ""
.Location = pt
.Size = sz
End With
myBox(x, y) = myChk
Panel1.Controls.Add(myChk)
Next y
Next x
Dim lbl As New LinkLabel
pt.X = 37 : pt.Y = 280
lbl.Text = "JF"
lbl.Location = pt
Panel1.Controls.Add(lbl)
End Sub

Hth Greetz Peter
 

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