By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,369 Members | 1,584 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,369 IT Pros & Developers. It's quick & easy.

Add Event Handler to array of controls?

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Rich wrote:
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
Nov 21 '05 #2

P: n/a
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

"Chris" wrote:
Rich wrote:
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

Nov 21 '05 #3

P: n/a
Rich wrote:
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

"Chris" wrote:

Rich wrote:
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

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
Nov 21 '05 #4

P: n/a
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

"Chris" wrote:
Rich wrote:
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

"Chris" wrote:

Rich wrote:

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

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

Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.