|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Referencing Controls created at run time.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 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. -- Show quoteHide quoteDennis 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 > > > 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 >> >> >> 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 -- Show quoteHide quoteDennis 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" <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 > >> > >> > >> > > > 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 >>> >>> >>> > > 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 >>>> >>>> >>>> >> >> > >
Show quote
Hide quote
On Wed, 9 Aug 2006 14:07:33 +0100, "T Clancey" <t***@idcodeware.co.uk> wrote: What version VB? In VB2005, the PictureBox does not have a ForeColor or Text Property.>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 > 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
FileListBox from VB6?
Help with first VB application - Data Entry form Easy Value Compare Question MS VB Newbie Tutorial Check to see if a server is running Check if a Windows service running Dataset Writing to a file opened in another class CreateObject does not work consistently in X64 systems Multi-level TreeNode storing? |
|||||||||||||||||||||||