Referencing Controls created at run time.

T

T Clancey

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
 
G

Guest

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.
 
T

T Clancey

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.
 
G

Guest

Try something like this:
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" & i.ToString
AddHandler PIC.MouseDown, AddressOf handles_PictureBoxMouseDown
PIC.BackColor = System.Drawing.Color.Black
PIC.Text = i
PIC.ForeColor = Color.Black
MynewPos.Y += 30
Me.Controls.Add(PIC)
Next

Private Sub handles_PictureBoxMouseDown (source as object, e as
MouseEventArgs)
dim picboxname as string =
directcast(source,picturebox).Name.Replace("Picture","")
Select Case picboxname
case "0"
'Do something with picturebox 0
case "1"
...
......
End Select
End Sub
 
G

gene kelley

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

What version VB? In VB2005, the PictureBox does not have a ForeColor or Text Property.
You would need to use the Graphics DrawString method if you want to put some text on a PictureBox.

In VB2005: (assumes a table where each row contains a picture)


For i As Integer = 0 To MyTable.Rows.Count - 1
Dim PIC As PictureBox = New PictureBox()
With PIC
.Name = String.Concat("MyPicture", i)
.BackColor = Color.Black
.Size = New Drawing.Size(20, 20)
.Location = New Drawing.Point(10, 10 + (30 * i))
'.Image = 'ImgData Column data in current row
'.Tag = 'any other useful info for this picture
AddHandler .MouseDown, AddressOf PICMouseDown

End With
Me.Controls.Add(PIC)
Next

Private Sub PICMouseDown(ByVal sender As Object, ByVal _
e As System.Windows.Forms.MouseEventArgs)
Dim BoxClicked As PictureBox = DirectCast(sender, PictureBox)
'Dim TagInfo as String = BoxClicked.Tag.ToString
MessageBox.Show(String.Concat(BoxClicked.Name, vbNewLine, e.X, ", ", e.Y))

End Sub

Add any additional handlers as needed.

You will have to add appropriate code to retrive the image data from the current row.


Gene
 
L

Lars Graeve

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

T Clancey

Many thanks for the example, this is exactly what I was looking for.
Cheers,
Tull.
 

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