Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 692
  • Last Modified:

Exception on casting to HandledMouseEventArgs

This should be simple but ... I have this event handler on a textbox to ignore right mouse clicks. When it executes I get InvalidCastException on the 3rd line.

        private void txtUserName_MouseDown(object sender, MouseEventArgs e) {
            if (e.Button == System.Windows.Forms.MouseButtons.Right) {
                HandledMouseEventArgs eh = (HandledMouseEventArgs)e;
                eh.Handled = true;
            }
        }

Open in new window

Per http://stackoverflow.com/questions/4264579/how-to-prevent-mouse-scroll-in-toolstripcombobox and other examples, this ought to work. Instead, I get the exception. Ideas?
0
BlearyEye
Asked:
BlearyEye
  • 2
1 Solution
 
AndyAinscowFreelance programmer / ConsultantCommented:
It is actually a MouseEventArgs, not what you try to cast it to.
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
ps.  The HandledMouseEventArgs is (according to my help file) only for a mouse wheel event.
0
 
BlearyEyeAuthor Commented:
I see that now... so, I want to ignore right-click for this control. Is there another way to do it? MouseEventArgs doesn't support a .Handled property.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now