petroid Posted October 28, 2004 Share Posted October 28, 2004 Hello folks! I've been looking for a way to act on a mouse click that is performed anywhere on a form. As an example, is there any way that I could get a msgbox to pop up if the righ mouse button is clicked anywhere on a form, even if it is on a control? Thanks guys. :) Link to comment https://www.neowin.net/forum/topic/236949-vbnet-trap-mouse-click/ Share on other sites More sharing options...
0 James Rose Posted October 28, 2004 Share Posted October 28, 2004 Here you go Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown Select Case e.Button Case MouseButtons.Left MsgBox("LEFT") Case MouseButtons.Right MsgBox("RIGHT") Case MouseButtons.Middle MsgBox("MIDDLE") End Select End Sub Link to comment https://www.neowin.net/forum/topic/236949-vbnet-trap-mouse-click/#findComment-584822107 Share on other sites More sharing options...
0 petroid Posted October 29, 2004 Author Share Posted October 29, 2004 That code works for a blank form, but it doesn't affect a groupbox or mozilla control... Any other suggestions? :) Link to comment https://www.neowin.net/forum/topic/236949-vbnet-trap-mouse-click/#findComment-584826099 Share on other sites More sharing options...
0 nowimnothing Posted October 29, 2004 Share Posted October 29, 2004 petroid said: That code works for a blank form, but it doesn't affect a groupbox or mozilla control... Any other suggestions? :) 584826099[/snapback] someone correct me if i'm wrong, but i don't think there's a simple way to do this. but one method would be to hook all of the _Click events of the controls into the same handler as the form (you can have it hooked into multiple places so it doesn't interfere with the other functionality) and that might work. Link to comment https://www.neowin.net/forum/topic/236949-vbnet-trap-mouse-click/#findComment-584828089 Share on other sites More sharing options...
0 James Rose Posted October 29, 2004 Share Posted October 29, 2004 You are correct. There is no simple way to do this, as each control has it's own functions/methods. The control is on top of the form and there is no way to have a mouse event call the form's mouse event UNLESS you specifically have the control's Mouse event call the form's mouse event. I guess the real question is WHY would a person want this functionality. What sort of issue are you trying to deal with and why would you want a generic event to happen for all controls. (hey, there could be a reason... *I* just don't know of one) Link to comment https://www.neowin.net/forum/topic/236949-vbnet-trap-mouse-click/#findComment-584828578 Share on other sites More sharing options...
0 petroid Posted October 31, 2004 Author Share Posted October 31, 2004 Well, I have a mozilla browser control, and I want to prevent the right click on it without having to resort to modifying the control itself... Link to comment https://www.neowin.net/forum/topic/236949-vbnet-trap-mouse-click/#findComment-584837299 Share on other sites More sharing options...
0 lexecutil Posted October 31, 2004 Share Posted October 31, 2004 Well I suppose you could use System.Windows.Forms.Application.AddMessageFilter to filter out the mouse messages. :) Dan Link to comment https://www.neowin.net/forum/topic/236949-vbnet-trap-mouse-click/#findComment-584837469 Share on other sites More sharing options...
0 petroid Posted October 31, 2004 Author Share Posted October 31, 2004 Care to elaborate? I'm new to .net :). Link to comment https://www.neowin.net/forum/topic/236949-vbnet-trap-mouse-click/#findComment-584837865 Share on other sites More sharing options...
0 Linkinfamous Posted October 31, 2004 Share Posted October 31, 2004 If you wanted, you could try this..... but you'd have to add each thing in your form. Private Sub Universal_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles _ Button1.MouseDown, Label1.MouseDown, MyBase.MouseDown, TextBox1.MouseDown MsgBox("Hello!") End Sub Link to comment https://www.neowin.net/forum/topic/236949-vbnet-trap-mouse-click/#findComment-584837984 Share on other sites More sharing options...
0 petroid Posted November 1, 2004 Author Share Posted November 1, 2004 While that works for most things, I already tried that, the browser doesn't support mousedown events. :( Link to comment https://www.neowin.net/forum/topic/236949-vbnet-trap-mouse-click/#findComment-584842442 Share on other sites More sharing options...
0 lexecutil Posted November 1, 2004 Share Posted November 1, 2004 Yeah sure, Ill make you an example now :) Dan Link to comment https://www.neowin.net/forum/topic/236949-vbnet-trap-mouse-click/#findComment-584842457 Share on other sites More sharing options...
0 lexecutil Posted November 1, 2004 Share Posted November 1, 2004 (edited) Ok heres an example :) //Doing this will block all mouseup messages to button1 Application.AddMessageFilter(new MessageFilter(button1.Handle, 0x0205 /*WM_RBUTTONUP (Right mouse button up!*/)); ...... public class MessageFilter : IMessageFilter { ?public MessageFilter(IntPtr handle, int msg) ?{ ? Handle = handle; ? Msg = msg; ?} ?private IntPtr Handle; ?private int Msg; ?public bool PreFilterMessage(ref Message m) ?{ ? if (m.HWnd == Handle) ? { ? ?if (m.Msg == Msg) ? ?{ ? ? return true; ? ?} ? } ? return false; ?} } Dan Edited November 1, 2004 by dannyres Link to comment https://www.neowin.net/forum/topic/236949-vbnet-trap-mouse-click/#findComment-584842541 Share on other sites More sharing options...
0 petroid Posted November 3, 2004 Author Share Posted November 3, 2004 Sorry for asking, but can someone translate that to VB.NET, as I'm still getting used to translating. Thanks guys :D. Link to comment https://www.neowin.net/forum/topic/236949-vbnet-trap-mouse-click/#findComment-584856786 Share on other sites More sharing options...
Question
petroid
Hello folks!
I've been looking for a way to act on a mouse click that is performed anywhere on a form. As an example, is there any way that I could get a msgbox to pop up if the righ mouse button is clicked anywhere on a form, even if it is on a control?
Thanks guys. :)
Link to comment
https://www.neowin.net/forum/topic/236949-vbnet-trap-mouse-click/Share on other sites
12 answers to this question
Recommended Posts