Hello
small example:
Public Class Form1
Dim pictures() As PictureBox
Const NO_OF_PICTURES As Int32 = 15
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
ReDim pictures(NO_OF_PICTURES - 1)
For picture As Int32 = 0 To NO_OF_PICTURES - 1
pictures(picture) = New PictureBox
With pictures(picture)
.BackColor = Color.Red
.Width = 30
.Height = 30
.Top = 50
.Left = picture * 35 + 10
.Visible = True
.Tag = picture.ToString
AddHandler .Click, AddressOf Picture_Click
End With
Me.Controls.Add(pictures(picture))
Next picture
End Sub
Private Sub Picture_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)
Dim picture As PictureBox = CType(sender, PictureBox)
MsgBox("Picture " & picture.Tag.ToString & " clicked")
End Sub
Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As
System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
If pictures IsNot Nothing Then
For picture As Int32 = 0 To NO_OF_PICTURES - 1
With pictures(picture)
RemoveHandler .Click, AddressOf Picture_Click
End With
Next picture
End If
End Sub
End Class
In Button1_Click die picture boxes are created and a handler is adder
(allways to the same sub!).
In Picture_Click the parameter sender says, which picture box is clicked. If
reffered them by the tag property.
Finally in Form1_FormClosed all handlers are removed again - I think this
should be done if you load new data.
Greeting, Lars
"T Clancey" <tull@idcodeware.co.ukschrieb im Newsbeitrag
news:jq2dnauBCYQIa0TZnZ2dnUVZ8tOdnZ2d@bt.com...
Thanks for your reply. I'm confused!
>
Let me explain what I actually want to achieve, that may help.
>
I want to create a new picture box for every record in a database table,
there may be 1, there may be 10 records, so I have to create the controls
at run time.
>
I then need to add mouse handling so a user can move the picture boxes
around the form. So, not only do I not know the number of boxes I will
need, I also don't know how many handlers I will need.
>
Would I be better off creating an array of picture boxes?
>
Any help you can offer would be very much appreciated.
>
Cheers,
Tull.
>
>
"Dennis" <Dennis@discussions.microsoft.comwrote in message
news:1A05E657-1BE4-4964-BF20-18C3524ADD90@microsoft.com...
>Add an event handlerfor each event you want to handle using AddHandler.
>Note
>that you are naming all of your picture boxes the same name in your loop.
>--
>Dennis in Houston
>>
>>
>"T Clancey" wrote:
>>
>>Hi all. I've found a small bit of code that allows me to create a bunch
>>of
>>picture boxes at run time, but I still need an example of how to handle
>>the
>>controls in code. I need to access the mouse events for each individual
>>picture and place graphics and text in the boxes. Can anyone help?
>>>
>>Code for creating the boxes follows.
>>>
>>Cheers,
>>Tull.
>>>
>>For i = 0 To NumbOfPix - 1
>>>
>>Dim PIC As New System.Windows.Forms.PictureBox
>>>
>>PIC.Size = New System.Drawing.Size(20, 20)
>>>
>>PIC.Location = MynewPos
>>>
>>PIC.Name = "PictureBox"
>>>
>>PIC.BackColor = System.Drawing.Color.Black
>>>
>>PIC.Text = i
>>>
>>PIC.ForeColor = Color.Black
>>>
>>MynewPos.Y += 30
>>>
>>Me.Controls.Add(PIC)
>>>
>>Next
>>>
>>>
>>>
>
>