Add Event Handler to array of controls?

G

Guest

Hello,

I create multiple pictureboxe controls on a form in a For Loop and display
thumbnail pictures. I need to add a Click event to these pictureboxes. Here
is the routine that creates the pictureboxes:
----------------------------------------------------------------------------
Dim MynewPos As New Point(50, 40)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim pnt As New Point(CType(TextBox2.Text, Integer), CType(TextBox3.Text,
Integer))

CreateThem(CType(TextBox1.Text, Integer), pnt)
End Sub

Public Sub CreateThem(ByVal NumbOfPix As Integer, ByRef mypt As Point)
MynewPos = mypt

Dim i As Integer
If NumbOfPix = 0 Then
Exit Sub
End If

For i = 0 To NumbOfPix - 1
Dim PIC As New System.Windows.Forms.PictureBox
PIC.Size = New System.Drawing.Size(100, 100)
PIC.Location = MynewPos
PIC.Name = "PicBox" & i.ToString
PIC.BackColor = System.Drawing.Color.Black
PIC.Text = i
PIC.ForeColor = Color.Black
PIC.SizeMode = PictureBoxSizeMode.StretchImage
PIC.Image = Image.FromFile("C:\pic1.jpg")
MynewPos.Y += 120
Me.Controls.Add(PIC)
Next
End Sub
--------------------------------------------------------------------------------------

and here is some handler code that I don't know how to implement:

---------------------------------------------------------------------------------------
AddHandler Form1.PicBox0, _
New SomethingEventHandler(AddressOf OnClick)

Sub OnClick(ByVal sender As Object, _
ByVal args As somethingEventArgs)

'--Open a form with full sized picture
End Sub
------------------------------------------------------------------------------------

How do I implement the click event handler for each picturebox that I
create? Obviously I need to add a handler for each PicBox, like PicBox0,
PicBox1, ... so these would be in an array. How to do this?

Thanks,
Rich
 
C

Chris

Rich said:
Hello,

I create multiple pictureboxe controls on a form in a For Loop and display
thumbnail pictures. I need to add a Click event to these pictureboxes. Here
is the routine that creates the pictureboxes:
----------------------------------------------------------------------------
Dim MynewPos As New Point(50, 40)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim pnt As New Point(CType(TextBox2.Text, Integer), CType(TextBox3.Text,
Integer))

CreateThem(CType(TextBox1.Text, Integer), pnt)
End Sub

Public Sub CreateThem(ByVal NumbOfPix As Integer, ByRef mypt As Point)
MynewPos = mypt

Dim i As Integer
If NumbOfPix = 0 Then
Exit Sub
End If

For i = 0 To NumbOfPix - 1
Dim PIC As New System.Windows.Forms.PictureBox
PIC.Size = New System.Drawing.Size(100, 100)
PIC.Location = MynewPos
PIC.Name = "PicBox" & i.ToString
PIC.BackColor = System.Drawing.Color.Black
PIC.Text = i
PIC.ForeColor = Color.Black
PIC.SizeMode = PictureBoxSizeMode.StretchImage
PIC.Image = Image.FromFile("C:\pic1.jpg")
MynewPos.Y += 120
Me.Controls.Add(PIC)
Next
End Sub
--------------------------------------------------------------------------------------

and here is some handler code that I don't know how to implement:

---------------------------------------------------------------------------------------
AddHandler Form1.PicBox0, _
New SomethingEventHandler(AddressOf OnClick)

Sub OnClick(ByVal sender As Object, _
ByVal args As somethingEventArgs)

'--Open a form with full sized picture
End Sub
------------------------------------------------------------------------------------

How do I implement the click event handler for each picturebox that I
create? Obviously I need to add a handler for each PicBox, like PicBox0,
PicBox1, ... so these would be in an array. How to do this?

Thanks,
Rich

Public Sub CreateThem(ByVal NumbOfPix As Integer, ByRef mypt As Point)
MynewPos = mypt

Dim i As Integer
If NumbOfPix = 0 Then
Exit Sub
End If

For i = 0 To NumbOfPix - 1
Dim PIC As New System.Windows.Forms.PictureBox
PIC.Size = New System.Drawing.Size(100, 100)
PIC.Location = MynewPos
PIC.Name = "PicBox" & i.ToString
PIC.BackColor = System.Drawing.Color.Black
PIC.Text = i
PIC.ForeColor = Color.Black
PIC.SizeMode = PictureBoxSizeMode.StretchImage
PIC.Image = Image.FromFile("C:\pic1.jpg")

AddHandler Pic.OnClick, addressof OnClick

MynewPos.Y += 120
Me.Controls.Add(PIC)
Next
End Sub

Sub OnClick(ByVal sender As Object, _
ByVal args As somethingEventArgs)

Dim Pic as PictureBox
Pic = DirectCast(sender, PictureBox)
'--Open a form with full sized picture

End Sub
 
G

Guest

Thanks for responding to my post. This works like a charm, but I had to make
a few minor changes-

I changed your code slightly --AddHandler PIC.Click, AddressOf OnClick

I did not get a dropdown for PIC.OnClick, so I changed it to PIC.Click...

Then I had to modify Sub OnClick... to

Overloads Sub OnClick because I got an error message:
"sub 'OnClick' shadows an overloadable member declared in the base class
'Control'. If you want to overload the base method, this method must be
declared 'Overloads'"

But it seems to work now. Any further suggestions appreciated if there is
another way to do this without Overloads.

Rich
 
C

Chris

Rich said:
Thanks for responding to my post. This works like a charm, but I had to make
a few minor changes-

I changed your code slightly --AddHandler PIC.Click, AddressOf OnClick

I did not get a dropdown for PIC.OnClick, so I changed it to PIC.Click...

Then I had to modify Sub OnClick... to

Overloads Sub OnClick because I got an error message:
"sub 'OnClick' shadows an overloadable member declared in the base class
'Control'. If you want to overload the base method, this method must be
declared 'Overloads'"

But it seems to work now. Any further suggestions appreciated if there is
another way to do this without Overloads.

Rich





:


Sorry, I did the code from memory. You do not what to do OverLoads
Onclick. You are overriding the onclick event of your form at that
point. Change the same to something other than OnClick and you will not
have to use overloads.

Chris
 
G

Guest

Yes. I ended up changing the name of OnClick to ClickPic right after my last
post. That worked fine, and no Overloads.

Many thanks for your help.

Rich
 

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