kentheprogger Posted April 2, 2006 Posted April 2, 2006 I am designing a program that grabs PictureBox information from an XML file and puts them on the form. I want to have an EventHandler that handles every PictureBox I create, which I have done. The problem is that in order to identify which PictureBox has been clicked I need to get the name. I would think that this would be extremely easy but I have not found a way to do it. Here is some sample code: Public Sub createButtons() Dim i As Integer For i = 0 To UBound(buttons) If buttons(i).Name <> "" Then PBWEB = New PictureBox With PBWEB .Size = buttons(i).Size .Location = buttons(i).Location .Name = buttons(i).Name .Image = Image.FromFile(Application.StartupPath & buttons(i).upPath) End With AddHandler PBWEB.MouseDown, AddressOf buttons_MouseDown Me.Controls.Add(PBWEB) End If Next End Sub Public Sub buttons_MouseDown(ByVal sender As Object, ByVal e As System.windows.Forms.MouseEventArgs) Handles PBWEB.MouseDown Dim i As Integer For i = 0 To UBound(buttons) 'If buttons(i).Name = (?Button's Name that was Pushed?) Then ' Change Graphic to Down Path 'End If Next End Sub I have a lot of buttons and I really would not like to create hundreds of different handlers. I also would not like to use Control Arrays, as VB.Net has stepped away from it but not completely. Any help would be greatly appreciated. (EDIT: PBWEB.Name only provides the name of the last created control.) Quote
Administrators PlausiblyDamp Posted April 2, 2006 Administrators Posted April 2, 2006 Public Sub buttons_MouseDown(ByVal sender As Object, ByVal e As System.windows.Forms.MouseEventArgs) Handles PBWEB.MouseDown Dim i As Integer dim b as button = DirectCast(sender, Button) 'b will be the button that was pressed, 'b.Name is the name of the button that was pressed etc. 'instead of looping you could just do b.Image = For i = 0 To UBound(buttons) 'If buttons(i).Name = (?Button's Name that was Pushed?) Then ' Change Graphic to Down Path 'End If Next End Sub Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
kentheprogger Posted April 2, 2006 Author Posted April 2, 2006 That worked great, thank you! I guess I'll have do do some studying into direct casting, seems to be very useful. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.