Home All Groups Group Topic Archive Search About

Referencing Controls created at run time.

Author
9 Aug 2006 1:07 PM
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

Author
9 Aug 2006 1:16 PM
Dennis
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


Show quoteHide quote
"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
>
>
>
Author
9 Aug 2006 2:52 PM
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.


Show quoteHide quote
"Dennis" <Den***@discussions.microsoft.com> wrote 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
>>
>>
>>
Author
9 Aug 2006 11:33 PM
Dennis
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

--
Dennis in Houston


Show quoteHide quote
"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" <Den***@discussions.microsoft.com> wrote 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
> >>
> >>
> >>
>
>
>
Author
12 Aug 2006 12:16 PM
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



Show quoteHide quote
"T Clancey" <t***@idcodeware.co.uk> schrieb 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" <Den***@discussions.microsoft.com> wrote 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
>>>
>>>
>>>
>
>
Author
15 Aug 2006 7:51 AM
T Clancey
Many thanks for the example, this is exactly what I was looking for.
Cheers,
Tull.

Show quoteHide quote
"Lars Graeve" <LarsGra***@web.de> wrote in message
news:ebkgs8$qph$02$1@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" <t***@idcodeware.co.uk> schrieb 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" <Den***@discussions.microsoft.com> wrote 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
>>>>
>>>>
>>>>
>>
>>
>
>
Author
9 Aug 2006 11:52 PM
gene kelley
Show quote Hide quote
On Wed, 9 Aug 2006 14:07:33 +0100, "T Clancey" <t***@idcodeware.co.uk> 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
>

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