• 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

    • I see the Winblows 11 BETA testing continues. This is just Windows 10 all over again. Oh well, everything is still smooth sailing over here on Windows 10 21H2 LTSC. You unpaid sheep BETA testers be sure to let us know when you get done BETA testing Winblows 11 for the rest of us. 🤣
    • What do you expect? Windows 11 is still in BETA. This is Windows 10 all over again. M$ doesn't hire people to do BETA testing anymore. They just let all the dumb sheep with FOMO do all the BETA testing for them. It's the same dumb sheep who use self-checkout at the store and do someone else's job for FREE! All while the worker who's getting paid just stands there and watches them. 🤣 🤡
    • Pet peeve: Microsoft using scaling options to get around the fact they removed font size settings, as if that's somehow acceptable to do. Thankfully third party apps still can adjust font sizes just fine, even though it often randomly resets on OS updates. And the funny thing is that not only are the scaling options horrible crutches, but they only work in some applications. And, ironically enough, even some apps from Microsoft themselves have hardcoded tiny fonts in certain dialogs in them. Example - certain parts of MS Office apps UI.
    • Unofficial script does the most useful official Windows 11/10 repairs you want automatically by Sayan Sen IT admins and system admins, and even home users have to run various Windows diagnostic runs from time to time in order to iron out or work around system problems. Last year, Microsoft published a guidance piece about various such native Windows apps, tools and utilities they include the like of Task Manager, Registry Editor, and more. Aside from them, Windows also comes with SFC (System File Checker) and DISM (Deployment Image Servicing and Management) to scan and fix corrupt and missing system files. Besides those, various other ways to help and diagnose network issues related to DNS (Domain Name System), among others, also exists. In order to save time running these, a Reddit user has created a new tool that automates all of these into a single package. The author writes that the Batch script (.BAT file) they have developed is "basically a one-stop script that can help clean up your system, run built-in diagnostics, fix common network issues, and generate system reports." The script is based on native Windows tools like netsh, ipconfig, systeminfo, among others, and the idea behind this is essentially to save time. The tool can be of help with Windows Update repairs, among others, something we all know is pretty common, and even Microsoft's own support articles may not prove to be helpful. Here is everything the utility can do for you: Run SFC, DISM, CHKDSK from a single menu Restart network adapters with auto-detection Flush or set DNS (Google, Cloudflare, or custom) Windows Update repair (resets services + cache) Generate system reports (saved as .txt files on Desktop) Show installed drivers Clean up temp files Registry backup and restore (manual) The latest version of the utility is now available for download on GitHub. The new version fixes issues related to admin privileges. As the script requires it to be run as an admin, it now restart itself to work in admin mode even if a user forgot to run it as an administrator. To download it, head over to its GitHub page here. The utility is named Windows Maintenance Tool. As always, though, make sure to back up your PC as this is an unofficial third-party app, and it's better if you test it first in a VM. Source: Lil_Batti (Reddit)
    • Come the hell on, do we need clickbait titles? "Overwatch 2" - 11 characters "a popular multiplayer hero shooter" - 34 characters What's the purpose here - delivering news or titles for clicks? I think we all know the answer. You're straying into "Number 7 on this list will SHOCK you" territory and while it may work on some crappy sites, this is why they are crappy sites. Just tell the story! Go back and look at popular articles that made Neowin what it was. Did they have headlines of "New OS from major technology conglomerate has astonishing new feature" or is it likely to tell the story succinctly and then elaborate within the content?
  • Recent Achievements

    • One Month Later
      CoolRaoul earned a badge
      One Month Later
    • First Post
      Kurotama earned a badge
      First Post
    • Collaborator
      Carltonbar earned a badge
      Collaborator
    • Explorer
      MusicLover2112 went up a rank
      Explorer
    • Dedicated
      MadMung0 earned a badge
      Dedicated
  • Popular Contributors

    1. 1
      +primortal
      508
    2. 2
      ATLien_0
      270
    3. 3
      +FloatingFatMan
      246
    4. 4
      +Edouard
      201
    5. 5
      snowy owl
      168
  • Tell a friend

    Love Neowin? Tell a friend!