Mouse Capture

rbulph

Junior Contributor
Joined
Feb 17, 2003
Messages
397
I'm creating an autotext facility in a richtextbox (like in Word where entries like "Yours faithfully" are suggested with a tooltip). If the user clicks away from the richtextbox then then the autotext suggestion should vanish. But this is easier said than done. The richtextbox's leave and lost focus events don't occur if the user clicks on the parent form for instance. I can approach the problem by capturing the mouse as follows:

Visual Basic:
    Private Sub RichTextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RichTextBox1.KeyUp
        Me.RichTextBox1.Capture = True
        Me.RichTextBox1.Cursor = Cursors.Default   'I'm sure this line shouldn't be necessary, but without it the cursor disappears!
    End Sub

but then initial clicks on buttons etc. are not recognised, which isn't very good. I can't believe that something that should be so simple is so problematic. Any suggestions?
 
rbulph said:
I'm creating an autotext facility in a richtextbox (like in Word where entries like "Yours faithfully" are suggested with a tooltip). If the user clicks away from the richtextbox then then the autotext suggestion should vanish. But this is easier said than done. The richtextbox's leave and lost focus events don't occur if the user clicks on the parent form for instance. I can approach the problem by capturing the mouse as follows:

Visual Basic:
    Private Sub RichTextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RichTextBox1.KeyUp
        Me.RichTextBox1.Capture = True
        Me.RichTextBox1.Cursor = Cursors.Default   'I'm sure this line shouldn't be necessary, but without it the cursor disappears!
    End Sub

but then initial clicks on buttons etc. are not recognised, which isn't very good. I can't believe that something that should be so simple is so problematic. Any suggestions?
try mouseevents and keydown events
 
Ginanity said:
try mouseevents and keydown events
Of what? It would be a bit excessive to have to handle the mousedown event of every control in my application.
 
check to see if the procedure is firing. Generally you only want to do something on a mouseup if you want to give someone a way out.

like "oh, I didn't mean to click that button, I'll keep the mouse pressed down and move it off the button"

You want this to happen on the textbox mouse down event it seems. Check that the procedure is firing and your tooltip isn't stealing/preventing it from firing.
 
Denaes said:
check to see if the procedure is firing. Generally you only want to do something on a mouseup if you want to give someone a way out.

like "oh, I didn't mean to click that button, I'll keep the mouse pressed down and move it off the button"

You want this to happen on the textbox mouse down event it seems. Check that the procedure is firing and your tooltip isn't stealing/preventing it from firing.

Yes, I do want this to happen on the textbox mousedown event. And yes, the tooltip is stealing/preventing it from firing. And the tooltip has no mousedown event of its own. So I'm still looking at capturing the mouse to force the textbox's mousedown event to occur.
 
Hmm, then I'd say you would have to do a form mousedown event (or maybe a mouseover/mousein event for the textbox) and calculate if you're in the boundries of the textbox.
 
Denaes said:
Hmm, then I'd say you would have to do a form mousedown event (or maybe a mouseover/mousein event for the textbox) and calculate if you're in the boundries of the textbox.

If the mouse goes down on a control in the form this won't trigger the form's mousedown event. There are dozens of controls in the form for me to worry about. OK, I could do some sort of iterative loop to add handlers for mousedown events of all controls in the form, but it's a bit of a hassle for something so small. I don't want anything to happen in response to the mousepointer going into or out of the textbox, so I don't see that events related to that will help. I still think that capturing the mouse is the best way.
 
Hmm, I was thinking of triggering a boolean on in in/out which would negate calculations on a Form MouseDown Event, but if the Forms MouseDown event isn't happening, then that doesn't help any. Right now I'm neck deep in refining some custom controls, so I can't really play around with the code and test things out, but those were my ideas.
 
Back
Top