Avatar of BlearyEye
BlearyEye
Flag for United States of America asked on

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?
C#.NET Programming

Avatar of undefined
Last Comment
BlearyEye

8/22/2022 - Mon
AndyAinscow

It is actually a MouseEventArgs, not what you try to cast it to.
ASKER CERTIFIED SOLUTION
AndyAinscow

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
BlearyEye

ASKER
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.
Your help has saved me hundreds of hours of internet surfing.
fblack61