Form MouseDown event...

ALX

Well-known member
Joined
Nov 16, 2005
Messages
253
Location
Columbia, SC
Programming Experience
10+
Sometimes I can get hung up on the simplest things....:(
I have many forms in my app that use repetitve loops. A single mousedown event on the form itself breaks the loop.
Now I have added one more form to my app using the same strategy as all the others, but for reasons that are a mystery to me, it requires 2 sequential mousedown events to break the loop. So, as a test, I wrote this simple example to figue out what was going on. I was suprised that it also requires 2 mousedown events to break the loop. Setting a breakpoint in "Form1_MouseDown" does not trap the 1st mousedown event for the form. Maybe I just need another set of eyes here....
This sample form needs just two labels, ("Label1" and "GoButton"). After clicking "GO", clicking anywhere on the form (other than the labels) SHOULD set Repeat to 0 and break the loop.

VB.NET:
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE][SIZE=2] Form1[/SIZE]
 
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] C [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Int16[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Repeat [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Int16 = 0[/SIZE]
 
[SIZE=2][COLOR=#0000ff]  Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] GoButtonClick([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] GoButton.Click[/SIZE]
[SIZE=2]    Repeat = 1[/SIZE]
[SIZE=2]     C = 1[/SIZE]
[SIZE=2]     Go()[/SIZE]
[SIZE=2][COLOR=#0000ff]  End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]  Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Go()[/SIZE]
[SIZE=2][COLOR=#0000ff]     Do[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]While[/COLOR][/SIZE][SIZE=2] C < Int16.MaxValue[/SIZE]
[SIZE=2]         Label1.Text = C.ToString[/SIZE]
[SIZE=2]         Application.DoEvents()[/SIZE]
[SIZE=2][COLOR=#0000ff]         If[/COLOR][/SIZE][SIZE=2] Repeat = 0 [/SIZE][SIZE=2][COLOR=#0000ff]Then [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Exit[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2]         C += 1[/SIZE]
[SIZE=2][COLOR=#0000ff]     Loop[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]  End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]  Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Form1_MouseDown([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.MouseEventArgs) _ [/SIZE]
[SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] M[/SIZE][SIZE=2][COLOR=#0000ff]e[/COLOR][/SIZE][SIZE=2].MouseDown[/SIZE]
[SIZE=2]     Repeat = 0[/SIZE]
[SIZE=2][COLOR=#0000ff]  End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE]
 
set focus to form (Me.Focus()) before you call Go().
 
That's what I thought too...but no matter where I put "Me.Focus" I have the same behavior. Interestingly, This is not an issue if I use a button for the "GoButton" rather than a label. I have been using labels rather than buttons on this app to ease complications with the "TAB" key. What eats me up is that I have 10 other forms on this app that behave properly, although they were originally written in VB2003 and converted to VB2005.
 
I thought it was a button control you used for click since you named it a button.. do you see how this is a bad name for a label control ?
For label click put the code Label1.Capture = False instead of the first suggested me.focus.
 
Yes, it truely is a bad name for a label. I have many bad habits but I'm trying to clean up my act. Anyway, thanks for the solution. 'Works great now !
 
Back
Top