locked
User Control Override Event Error RRS feed

  • Question

  • Hi I created a UserControl which consists of a textbox (txt1) and a label.  In my control I created the following evetn handler for the Key down

    public

     

    delegate void PreviewKeyDownEvent(object sender, KeyEventArgs

    e);

     

    public event PreviewKeyDownEvent

    PreviewKeyDown;

    And on the PreviewKeyDown of the TextBox I have the following code :

     private void txt1_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        if (PreviewKeyDown != null) PreviewKeyDown(this, e);
    }

     The control works fine but in the error list I keep getting the following error:

    Warning 3 'Clients.UserControls.uCurrencyTextBox.PreviewKeyDownEvent' hides inherited member 'System.Windows.UIElement.PreviewKeyDownEvent'. Use the new keyword if hiding was intended. C:\Users\Ivan\Desktop\OFS\OFS\Sales\Clients\Clients\UserControls\uCurrencyTextBox.xaml.cs 27 30 Clients

     

    Any one can help

    Thanks

    Ivan

    Monday, October 17, 2011 8:22 AM

Answers

  • When used as a modifier, the new keyword explicitly hides a member inherited from a base class. When you hide an inherited member, the derived version of the member replaces the base-class version. Although you can hide members without the use of the new modifier, the result is a warning. If you use new to explicitly hide a member, it suppresses this warning and documents the fact that the derived version is intended as a replacement.

    To hide an inherited member, declare it in the derived class by using the same name, and modify it with the new modifier. For example:

      if (PreviewKeyDown != null) new PreviewKeyDown(this, e);
    
    Source
    Monday, October 17, 2011 9:57 AM
  • Hi,

    Please override OnPreviewKeyDown to implement class handling for this event in derived classes.

     


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".
    My blog: http://csharptips.wordpress.com
    Monday, October 17, 2011 2:09 PM

All replies

  • When used as a modifier, the new keyword explicitly hides a member inherited from a base class. When you hide an inherited member, the derived version of the member replaces the base-class version. Although you can hide members without the use of the new modifier, the result is a warning. If you use new to explicitly hide a member, it suppresses this warning and documents the fact that the derived version is intended as a replacement.

    To hide an inherited member, declare it in the derived class by using the same name, and modify it with the new modifier. For example:

      if (PreviewKeyDown != null) new PreviewKeyDown(this, e);
    
    Source
    Monday, October 17, 2011 9:57 AM
  • Hi,

    Please override OnPreviewKeyDown to implement class handling for this event in derived classes.

     


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".
    My blog: http://csharptips.wordpress.com
    Monday, October 17, 2011 2:09 PM
  • Hi Ivan Sammut,

    How’s it going? Do you have any updates about the previous issue?

    Please reply to let us know if this issue has been resolved.


    Bob Shen [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, October 24, 2011 3:05 AM
    Moderator