Handles

  • Thread starter Thread starter Cokegod
  • Start date Start date
C

Cokegod

Guest
I just made 5 pic boxes in the form load like this:

Dim b(4) As PictureBox
Dim I As Integer
Dim MyImage As Bitmap
For i = 0 To 4
B(I) = New PictureBox()
MyImage = New Bitmap("C:\Documents and Settings\Geoff\My Documents\Visual Studio Projects\WindowsApplication1\b" & I & ".bmp")
B(I).Image = CType(MyImage, Image)
B(I).Top = 120
B(I).Left = 110 + (I * 50)
B(I).SizeMode = PictureBoxSizeMode.StretchImage
B(I).Height = 45
B(I).Width = 50
Me.Controls.Add(b(I))
Next

And now I am wonder how do i get the click event from these new pics: I tried:

Private Sub B_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles b.Click '

but it dosen't recognize my "b" in "Handles b.click"
Thanks for all the help, this forum has been great!
 
Make yourself a sub with the right method signature, then use AddHandler:

Visual Basic:
AddHandler button.Click, AddressOf MyFunction
 
Handle Clause Requires A WithEvents Variable

I added this line to my above loop:

AddHandler b(I).Click, AddressOf B_Click

But it still dosen't like my sub of

Private Sub B_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles b.Click

it gives me the withevents error above. Sorry for all the questions and thanks for the help!
 
Get rid of the "Handles b.Click" after the method declaration, you don't need it since you're wiring them up manually.
 
Which One?

Thanks for the help!
but now when i click on my pics i can not figure out which one i clicked on in the array. Thanks Again!
 
That's what the sender object is for, that is passed to the method. If you cast it using CType to a Button, or whatever type it is, you then have the button that raised the event.
 
???

so, do i add the line:
b(i) = ctype(b(i), picturebox) ' i declared it as a picbox and im not sure what to put in for the expression of ctype
And my sender variable in my sub is always empty.
There is only a sender.gettype(), which can not be converted into an integer or string.
Thanks again!
 
In your sub, I suggest you do this:

Visual Basic:
Dim picSender As PictureBox

'This is ok if you know the only thing to raise this event is going
'to be a PictureBox control
picSender = CType(sender, PictureBox)

Once you have that code, you can use picSender as the instance of the PictureBox that was clicked. If you need to get the index of the array, you can loop through your array and check for equality with this object using the Is operator, or you could possibly just store the index of the array in the Tag property of each PictureBox when you create them.
 
???

Here is the code i have so far

Code:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim b(4) As PictureBox
Dim I As Integer
Dim MyImage As Bitmap
For I = 0 To 4
            b(I) = New PictureBox()
            MyImage = New Bitmap("C:\Documents and  Settings\Geoff\My Documents\Visual Studio Projects\WindowsApplication1\b" & I & ".bmp")
            b(I).Image = CType(MyImage, Image)
            b(I).Top = 120
            b(I).Left = 110 + (I * 50)
            b(I).SizeMode = PictureBoxSizeMode.StretchImage
            b(I).Height = 45
            b(I).Width = 50
            b(I) = CType(sender, PictureBox)
            AddHandler b(I).Click, AddressOf B_Click
            Me.Controls.Add(b(I))
        Next
End Sub

Private Sub B_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
[\code]
I get the following error:
"An unhandled exception of type 'System.InvalidCastException' occurred in WindowsApplication1.exe Additional information: Specified cast is not valid."
for this line: b(I) = CType(sender, PictureBox)
Thanks for all the help!
 
The ctype is in the wrong place in your code. Use it in the click procedure. One way of doing this is to give each picture box a name in the code in the loop:-

..
...
...
b(I).Image = CType(MyImage, Image)
b(I).Top = 120
b(I).Left = 110 + (I * 50)
b(I).SizeMode = PictureBoxSizeMode.StretchImage
b(I).Height = 45
b(I).Width = 50
b(I).Name = "b" & i.ToString
..
..
etc.

Then in your click event:-

Private Sub B_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

Dim curPicBox as PictureBox = Ctype(Sender, PictureBox)
If curPicBox.Name = "b1" then
'your code for box 1
elseif curPicBox.Name = "b2" then
..
.
.
etc.
..
.
.
End If

End Sub
 
Back
Top