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

Referencing Controls created at run time.

P: n/a
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
Aug 9 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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
Aug 9 '06 #2

P: n/a
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" <De****@discussions.microsoft.comwrote in message
news:1A**********************************@microsof t.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

Aug 9 '06 #3

P: n/a
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("Pictur e","")
Select Case picboxname
case "0"
'Do something with picturebox 0
case "1"
...
......
End Select
End Sub

--
Dennis in Houston
"T Clancey" wrote:
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" <De****@discussions.microsoft.comwrote in message
news:1A**********************************@microsof t.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


Aug 9 '06 #4

P: n/a
On Wed, 9 Aug 2006 14:07:33 +0100, "T Clancey" <tu**@idcodeware.co.ukwrote:
>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
Aug 9 '06 #5

P: n/a
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" <tu**@idcodeware.co.ukschrieb im Newsbeitrag
news:jq******************************@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" <De****@discussions.microsoft.comwrote in message
news:1A**********************************@microsof t.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


Aug 12 '06 #6

P: n/a
Many thanks for the example, this is exactly what I was looking for.
Cheers,
Tull.

"Lars Graeve" <La********@web.dewrote in message
news:eb*************@news.t-online.com...
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" <tu**@idcodeware.co.ukschrieb im Newsbeitrag
news:jq******************************@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" <De****@discussions.microsoft.comwrote in message
news:1A**********************************@microso ft.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



Aug 15 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.