Question picBox_Click and strict on

Zexor

Well-known member
Joined
Nov 28, 2008
Messages
520
Programming Experience
3-5
i have dynamically created a picturebox and its click event

VB.NET:
Dim pic As New PictureBox
AddHandler pic.Click, AddressOf pic_Click
Private Sub pic_Click(sender As Object, e As EventArgs)


End Sub


and it works fine, but i also want to sense which mouse button was clicked, so i changed it to

VB.NET:
AddHandler pic.Click, AddressOf pic_Click
Private Sub pic_Click(sender As Object, e As System.Windows.Forms.MouseEventArgs)


End Sub

but because i have Strict On, i get a narrowing in implicit type conversions error on declaring the handler. How do i fix that?
 
Hi,

The easiest way to do this is to add an additional event handler for the MouseClick event. You can then use the MouseEventArgs correctly to test which button has been clicked using:-

VB.NET:
If e.Button = Windows.Forms.MouseButtons.Left Then
  MsgBox("Left Button Clicked")
End If
The thing to remember here is that the Click event occurs before the MouseClick event so you may need to reorganise your code accordingly to accommodate this.

Cheers,

Ian
 
I mean the code works if Strict was off. with strict on , it said the method doesnt match the delegate.
VB.NET:
            AddHandler pic.Click, AddressOf pic_Click    'Error here
 
you said add an additional event handler for the MouseClick event? You mean add events for MouseUp, MouseDown, and MouseMove events?

VB.NET:
AddHandler pic.MouseClick, AddressOf pic_MouseClick

oh you mean going from click to mouseclick. Thanks its working now.
 
Last edited:
Hi,

OK, You got it, I was just about to say:- No, that's not right. I will help you here but I have to say that you need to study up on the individual events that are available to you with each object that you interact with.

In this case the PictureBox, along with many other objects, has an available event called MouseClick which accepts the arguments, sender as object and e as System.Windows.Forms.MouseEventArgs as its expected parameters. See this:-

VB.NET:
Private Sub MyPicBoxMouseClick(sender As System.Object, e As System.Windows.Forms.MouseEventArgs)
  If e.Button = Windows.Forms.MouseButtons.Left Then
    MsgBox("Left Button Clicked")
  End If
End Sub
At the same time you declare your Click event, you could also declare a MouseClick event being:-

VB.NET:
AddHandler Pic.MouseClick, AddressOf MyPicBoxMouseClick
This will then enable you to interrogate the buttons that have been pressed.

Does that make better sense?

Cheers,

Ian
 
Hi,

If you think about it, it's all about non-repetition of code. Lets say you want something to happen on every button click, regardless of the button pressed, then you would use the Click event. If you then want to do something special when a particular button is pressed then you can code that in the MouseClick event. The only limitation is that the Click event occurs before the MouseClick event.

Hope that helps.

Cheers,

Ian
 
cant you just put the thing you want to do in the click event at the beginning of the mouseclick event before you check for mousebutton? wouldnt that be the same?
 
Why have a separate click and mouse click event when they do the same thing?

Because they don't do the same thing. The Click event has existed since .NET 1.0 and simply notifies you when a control is clicked. It provides no additional information. The MouseClick event was added in .NET 2.0 and it does provide additional information, like which mouse button caused the click and the location of the mouse pointer. If you care about that information, e.g. you're using the event to draw on an Image in a PictureBox, then you handle the MouseClick event. If you don't care about that information, e.g. you want to open a dialogue when the user clicks a Button, then you handle the Click event. This is another case of the right tool for the job. Generally there would be no point handling both events for the same control, so it simply comes down to whether you want to use the extra information that the MouseClick event provides.
 
Note that they could have removed the Click event when they added the MouseClick event because MouseClick is like a superset of Click. That would have broken all the existing code that used Click though.
 
Back
Top