• 0

vb.net trap mouse click


Question

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

  • 0

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

  • 0
  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.

  • 0

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)

  • 0

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

  • 0

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 by dannyres
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Yeah it is like Aero Glass reborn! (which I loved) but I do not have any Apple devices.
    • Amazon to invest $20 billion in Pennsylvania for AI infrastructure, creating 1,250 jobs by Paul Hill The governor of Pennsylvania, Josh Shapiro, has announced that Amazon has agreed to invest at least $20 billion in AI infrastructure in the state. Shapiro noted that this is the largest-ever private sector investment in the state’s history and is set to create at least 1,250 high-paying, high-tech jobs, plus thousands of construction jobs to build the AWS data centers. The initial locations for the first two data centers are Salem Township in Luzerne County and Falls Township in Bucks County. The governor said that more sites were also under consideration, so this $20 billion figure could grow further. Aside from the obvious benefits for the locals, it also helps to keep America in a strong position in its AI race with China. Why Pennsylvania? The Shapiro administration's role “Pennsylvania is competing again – and I’m proud to announce that with Amazon’s commitment of at least $20 billion to build new state-of-the-art data center campuses across our Commonwealth, we have secured the largest private sector investment in the history of Pennsylvania,” said Governor Josh Shapiro. “This initial investment from Amazon will create thousands of good-paying, stable jobs as Pennsylvania workers build, maintain, and operate the first two data center campuses in Luzerne County and Bucks County. Our team worked closely with local leaders and Amazon to land this deal, and we continue to be actively engaged on securing additional sites in Pennsylvania – helping them secure local support, developing the infrastructure needed to support more data centers, and ensuring our permitting processes move quickly and efficiently. With this historic announcement, we’re creating opportunity for our workers, generating new revenue for our local communities, and ensuring the future of AI runs right through Pennsylvania.” Based on what Shapiro said, the state has tried quite hard to get Amazon to build there instead of somewhere else. States really have to put in effort to convince big tech, as other areas are also clamoring for businesses to set up there too. Back in 2021, we reported that Samsung had agreed to build its Taylor, Texas factory after the state made a tax break offer with the Korean company. The impact on jobs, communities, and the AI landscape Initially, Amazon will need thousands of construction workers to build the data centers and when this is done, it will be hiring 1,250 operators, technicians, and engineers to help manage the facilities - these jobs promise to be high-paying jobs. These jobs are also expected to drive up tax revenues, which will be positive for the state, counties, and municipalities. David Zapolsky, Amazon’s chief global affairs and legal officer, also said that the investments would create workforce development programs and community initiatives for the benefit of residents. It’s unclear from the announcement what form these will take.
    • Feels like Apple is kind of bringing Aero to their apple stuff, honestly. I get that same vibe.
    • Yeah a lot of people do that, but there are also terrible people on secondhand market places that will claim things like the CPU isn't working, or there's a problem with the motherboard etc It's harder to confirm to a buyer that the item is fine when it is all sold separately. I was thinking of taking out the 4070 and riser kit and lowering to 1400. I think people who would buy this sort of system would pair a better GPU with it anyway (I had a 4080 SUPER in it).
  • Recent Achievements

    • Enthusiast
      the420kid went up a rank
      Enthusiast
    • Conversation Starter
      NeoToad777 earned a badge
      Conversation Starter
    • Week One Done
      VicByrd earned a badge
      Week One Done
    • Reacting Well
      NeoToad777 earned a badge
      Reacting Well
    • Reacting Well
      eric79XXL earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      477
    2. 2
      +FloatingFatMan
      281
    3. 3
      ATLien_0
      253
    4. 4
      Edouard
      202
    5. 5
      snowy owl
      201
  • Tell a friend

    Love Neowin? Tell a friend!