• 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

    • Stellarium 25.2 by Razvan Serea Stellarium is a free open source planetarium for your computer. It shows a realistic sky in 3D, just like what you see with the naked eye, binoculars or a telescope. It is being used in planetarium projectors. Just set your coordinates and go. Stellarium key features: Realistic simulation of the sky, sunrise and sunset Default catalogue of over 600,000 stars Downloadable additional catalogues for up to 210 million stars Catalog data for all New General Catalogue (NGC) objects Images of almost all Messier objects and the Milky Way Artistic illustrations for all 88 modern constellations More than a dozen different cultures with their constellations Solar and lunar eclipse simulation Photorealistic landscapes (more are available on the website) Scripting support with ECMAScript (a few demo scripts are included) Extendable with plug-ins: 8 plug-ins installed by default, including: artificial satellites plug-in (updated from an on-line TLE database) ocular simulation plug-in (shows how objects look like in a given ocular) Solar System editor plug-in (imports comet and asteroid data from the MPC) telescope control plug-in (Meade LX200 and Celestron NexStar compatible) The major changes of this version: Many changes in support of sky cultures Added 2 new plugins: Mosaic Camera and Custom Nebula Textures Many improvements in Core Updates in sky cultures (include new one) [full release notes] Download: Stellarium 25.2 (64-bit) | 361.0 MB (Open Source) View: Stellarium Home Page | Other Operating Systems | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Microsoft 365 Roadmap Weekly: Compact notifications in Teams, improvements to Copilot & more by David Uzondu It is the end of another week, which means there's a new batch of additions to the public Microsoft 365 Roadmap. Most of the additions this week were focused on the usual Microsoft products like Copilot, Edge, Teams, and Outlook. Let's start with Edge, because a change is coming to how the browser handles links from other apps, with an expected arrival in August. If you use different Edge profiles for work and personal stuff, you know the drill. Right now, your setting for a default profile takes priority. When this Edge update rolls out, an application like Teams or Outlook can recommend a profile, and Edge will prioritize that recommendation instead. An admin policy called "EdgeOpenExternalLinksWithAppSpecifiedProfile" will be available for organizations that want to control this behavior. Microsoft Teams is getting a few tweaks aimed at improving day-to-day use, spread out across this summer. Arriving next month, notifications are getting a compact mode, which shrinks them down for a more minimalist look on your screen. The ability to save messages and posts is also on the way for August. Teams will also get noise suppression for people who dial into meetings with a phone. It will automatically filter out background sounds like traffic or keyboard clatter, making calls clearer for everyone involved. This is set to roll out in September. If you spend too much time digging through old emails, an update for Outlook planned for next month should help. A new "Browse Conversation Files" feature will gather every file shared within an email thread into a single, easy-to-access view at the top of the conversation. Moving on to Copilot, the AI assistant will get one-click buttons to instantly generate FAQ or briefing pages from content in your Copilot Notebook. For managers, a new "scoped access" feature for the Copilot Dashboard will let them see adoption and usage stats just for their specific teams. Microsoft is also bringing the basic Copilot chat to its Department of Defense (DoD) customers next month, with web access turned off by default to "meet US Government requirements."
    • AMD power plans are not a thing anymore. That was their poor man's attempt to influence scheduling in Windows until they got Microsoft to update the scheduler to better understand the concepts of CCDs, which are super important to getting the correct performance form Ryzen. I never really understood how a power plan could have that effect, but I do know they are no longer needed.
    • I didn't measure it precisely, just clicked around a few things and noticed they mostly happened at that barely preservable amount of time, which I estimated to be around 0.25.
    • 'Animate controls and elements inside windows' affects the operation progress animations (e.g., when writing metadata to items) in Windows Vista; for example, this animation does not appear if that option is disabled: This little animation also does not animate in the Details Pane in Windows Explorer (or in the Welcome Center) if that option is disabled:
  • Recent Achievements

    • First Post
      Johnny Mrkvička earned a badge
      First Post
    • Week One Done
      viraltui earned a badge
      Week One Done
    • One Month Later
      serfegyed earned a badge
      One Month Later
    • Dedicated
      firey earned a badge
      Dedicated
    • Dedicated
      fettermanj earned a badge
      Dedicated
  • Popular Contributors

    1. 1
      +primortal
      648
    2. 2
      Michael Scrip
      224
    3. 3
      ATLien_0
      222
    4. 4
      Xenon
      145
    5. 5
      +FloatingFatMan
      143
  • Tell a friend

    Love Neowin? Tell a friend!