none
adding event to custom controls RRS feed

  • Question

  • Hi all
    I created custom control. I need to add events to the control. I want to do it to the control itself( not when I create the control in another project)
    is it possible? I keep getting error massages
    thank
    Sunday, August 2, 2009 6:58 PM

Answers

  • Hi,

    Anil Kumar Kolla provided a good post above. Based on what you have mentioned, I suppose you are going to create a custom routed event in the custom control. To do so, you first register a routed event using EventManager.RegisterRoutedEvent method, then raise it use the following code:

    RoutedEventArgs newEventArgs = new RoutedEventArgs(customEvent);
    RaiseEvent(newEventArgs);

    Here is a MSDN documentation on how to create custom routed event:
    http://msdn.microsoft.com/en-us/library/ms752288.aspx
    If this still does solve your issue, could you please provide a small, complete and ready-to-run example?
    Thanks.


    Jim Zhou -MSFT
    Tuesday, August 4, 2009 10:08 AM

All replies

  • Hi,
    just go through the following example

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Controls;
    
    namespace WpfTextBoxEx
    {
    
        public partial class CustomTextBox : TextBox
        {
            public delegate void MyKeypress(object sender, System.Windows.Input.KeyEventArgs e);
            public event MyKeypress eKeyPress;//Custom Event
    
            public CustomTextBox()
            {
                InitializeComponent();
                this.eKeyPress += new MyKeypress(CustomControl1_eKeyPress);
            }
    
            protected override void OnKeyDown(System.Windows.Input.KeyEventArgs e)
            {
                if (eKeyPress != null)
                {
                    eKeyPress(this, e);
                }
            }
    
            void CustomControl1_eKeyPress(object sender, System.Windows.Input.KeyEventArgs e)
            {
                if (e.Key == System.Windows.Input.Key.T)
                {
                    e.Handled = true;
                }
            }
    
        }
    }
    In the above example just i am creating one Custom Textbox where user can not type "T" letter.
    I Think this example can help you.

    Regards
    Anil
    Monday, August 3, 2009 8:51 AM
  • Hi,

    Anil Kumar Kolla provided a good post above. Based on what you have mentioned, I suppose you are going to create a custom routed event in the custom control. To do so, you first register a routed event using EventManager.RegisterRoutedEvent method, then raise it use the following code:

    RoutedEventArgs newEventArgs = new RoutedEventArgs(customEvent);
    RaiseEvent(newEventArgs);

    Here is a MSDN documentation on how to create custom routed event:
    http://msdn.microsoft.com/en-us/library/ms752288.aspx
    If this still does solve your issue, could you please provide a small, complete and ready-to-run example?
    Thanks.


    Jim Zhou -MSFT
    Tuesday, August 4, 2009 10:08 AM