• 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 don't think there is any problem with the two-digit number. Even if someone didn't understand the meaning, 26 > 18, so they will still understand at a glance that it is a newer version. The only downside I see is someone assuming 18 is a MUCH older than 26, but IMO, that confusion really isn't a big issue. The truth is that the majority of software companies use the 2-digit number; Microsoft is the outlier, and even they used 2-digit numbers in the past.
    • So, now it can give inaccurate answers and peddle lies in an even more human sounding voice? :)
    • Aiarty Image Enhancer for PC & Mac (worth $85) free offer ends today June 8 by Steven Parker Claim your complimentary eBook worth $85 for free, before the offer ends today June 8. Get Aiarty Image Enhancer to restore and upscale images to 32K with more details. Make your images and photos larger, sharper and clearer with AI. Tired of the tiny, blurry or low-resolution images downloaded from web or grainy pictures taken by old smartphones? Aiarty Image Enhancer comes to help you out. Leveraging tailored AI models and deep learning algorithms, it can transform any images, photos and AI arts into high-quality visuals with exceptional clarity and realistic details. No matter you’re enhancing web graphics, restoring cherished family memories, or preparing visuals for creative projects, Aiarty can produce studio-quality results in just a few clicks. Get it now to transform your images to stunning masterpieces at zero cost! Key Features: Upscale: enlarge images and photos to 4K/8K/32K for printing, archiving, sharing, displaying, or more. More details: generate more realistic details for hair, fur, feature, skin textures and so on, ensuring crystal-clear pixels. Restore face with faithful details, perfect for portraits, selfies, old photos, group shot. 4-in-1 enhancement: deblur, denoise, dejpeg and upscale image in one click. Batch process. Enhance 3000+ images at once, streamlining image editing workflow. Fast & Simple. Upscale 100 images in 2 minutes with just one click. No skills needed. Trained on 6.78 million images. Optimized for NVIDIA, AMD, and Intel GPUs & CPUs. The exclusive offer ends on June 8th. Don't miss out – grab your free licensed copy today. How to get it Please ensure you read the terms and conditions to claim this offer. Complete and verifiable information is required in order to receive this free offer. If you have previously made use of these free offers, you will not need to re-register. While supplies last! Download Aiarty Image Enhancer for PC/Mac (worth $85) for free Offered by Digiarty WinXDVD, view other free resources The below offers are also available for free in exchange for your (work) email: Winxvideo AI V3.0 Lifetime License for PC ($69.95 Value) FREE – Expires 6/8 Aiarty Image Enhancer for PC/Mac ($85 Value) FREE – Expires 6/8 Solutions Architect's Handbook, Third Edition ($42.99 Value) FREE – Expires 6/10 AI and Innovation ($21 Value) FREE – Expires 6/11 Unruly: Fighting Back when Politics, AI, and Law Upend [...] ($18 Value) FREE - Expires 6/17 SQL Essentials For Dummies ($10 Value) FREE – Expires 6/17 Continuous Testing, Quality, Security, and Feedback ($27.99 Value) FREE – Expires 6/18 VideoProc Converter AI v7.5 for FREE (worth $78.90) – Expires 6/18 Macxvideo AI ($39.95 Value) Free for a Limited Time – Expires 6/22 The Ultimate Linux Newbie Guide – Featured Free content Python Notes for Professionals – Featured Free content Learn Linux in 5 Days – Featured Free content Quick Reference Guide for Cybersecurity – Featured Free content We post these because we earn commission on each lead so as not to rely solely on advertising, which many of our readers block. It all helps toward paying staff reporters, servers and hosting costs. Other ways to support Neowin The above deal not doing it for you, but still want to help? Check out the links below. Check out our partner software in the Neowin Store Buy a T-shirt at Neowin's Threadsquad Subscribe to Neowin - for $14 a year, or $28 a year for an ad-free experience Disclosure: An account at Neowin Deals is required to participate in any deals powered by our affiliate, StackCommerce. For a full description of StackCommerce's privacy guidelines, go here. Neowin benefits from shared revenue of each sale made through the branded deals site.
    • You will find sketch and clone apps on both the Play Store and the App Store contrary to what Google and Apple will tell you. This fact negates their argument for why there shouldn't be other app stores and so on, it's BS at the end.
    • I ran the powershell script and it did not "recreate the inetpub directory if it has been deleted" I rebooted several times and also tried re running the script. I had to click y three times when I ran it initially as well, now running it just returns to a new poweshell line with no errors but still no directory recreated. meh
  • Recent Achievements

    • First Post
      viraltui earned a badge
      First Post
    • Reacting Well
      viraltui earned a badge
      Reacting Well
    • Week One Done
      LunaFerret earned a badge
      Week One Done
    • Week One Done
      Ricky Chan earned a badge
      Week One Done
    • Week One Done
      maimutza earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      482
    2. 2
      +FloatingFatMan
      264
    3. 3
      snowy owl
      238
    4. 4
      ATLien_0
      231
    5. 5
      Edouard
      177
  • Tell a friend

    Love Neowin? Tell a friend!