KeyPress in RadMaskedEdit

5 posts, 1 answers
  1. Eastern
    Eastern avatar
    43 posts
    Member since:
    Sep 2010

    Posted 09 Nov 2010 Link to this post

    hello

    i am using 2010 Q2 controls and inherited my user control from RadMaskedEdit. the problem is none of the KeyPress , KeyDown, KeyUp, PreviewKewDown fires.

    why? and how can i solve it ?
  2. Answer
    Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 09 Nov 2010 Link to this post

    Hi Eastern,

    You need to create some events to mirror the ones that you want to handle.

    1: In the user control, create some events that mirror the ones that you wish to handle.
    2: Create event handlers for the RadMaskedEdit to handle the KeyPress , KeyDown, KeyUp, PreviewKewDown events
    3: In the method body of these Event Handlers, raise your mirrored events.

    A basic example is shown below:
    Imports System.ComponentModel
      
    Public Class MyMaskedEditBox
      
        Public Sub New()
            InitializeComponent()
        End Sub
      
        ' Some of the propeties will exposed here
        ' Some of the methods may be exposed here
      
        <Description("The event for the controls keydown event")> _
        <Category("MyMaskedEdit Events")> _
        Public Event MaskedEditKeyDown As KeyEventHandler
      
        <Description("The event for the controls keypress event")> _
        <Category("MyMaskedEdit Events")> _
        Public Event MaskedEditKeyPress As KeyPressEventHandler
      
        <Description("The event for the controls keyup event")> _
        <Category("MyMaskedEdit Events")> _
        Public Event MaskedEditKeyUp As KeyEventHandler
      
        Private Sub RadMaskedEditBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RadMaskedEditBox1.KeyDown
            RaiseEvent MaskedEditKeyDown(sender, e) ' Handle MaskedEditKeyDown where you are using this this user control
        End Sub
      
        Private Sub RadMaskedEditBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles RadMaskedEditBox1.KeyPress
            RaiseEvent MaskedEditKeyPress(sender, e) ' Handle MaskedEditKeyPress where you are using this this user control
        End Sub
      
        Private Sub RadMaskedEditBox1_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RadMaskedEditBox1.KeyUp
            RaiseEvent MaskedEditKeyUp(sender, e) ' Handle MaskedEditKeyUp where you are using this this user control
        End Sub
      
    End Class

    and then in the form where you have the MaskedEditBox
    Private Sub MyMaskedEditBox1_MaskedEditKeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyMaskedEditBox1.MaskedEditKeyDown
        ' handle something here
    End Sub

    hope that helps
    Richard
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 09 Nov 2010 Link to this post

    Hello Eastern,

    Please accept my apologies. I missed that this was an inherited control.
    Please see this forum post to see about subscribing to these events.

    Best regards,

    Richard
  5. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 09 Nov 2010 Link to this post

    Hello,

    Here is a small sample for you too.

    Imports System.ComponentModel
      
    Public Class MyMaskedEditControl
        Inherits Telerik.WinControls.UI.RadMaskedEditBox
      
        <Category("MyMaskedEdit events")> _
        Public Event MaskedEditKeyPress As KeyPressEventHandler
        <Category("MyMaskedEdit events")> _
        Public Event MaskedEditPreviewKeyDown As PreviewKeyDownEventHandler
      
        Public Sub New()
            InitializeComponent()
            AddHandler MyBase.MaskedEditBoxElement.TextBoxItem.KeyPress, AddressOf MyMaskedEditBox_KeyPress
            AddHandler MyBase.MaskedEditBoxElement.TextBoxItem.PreviewKeyDown, AddressOf MyMaskedEditBox_PreviewKeyDown
        End Sub
      
      
        Private Sub MyMaskedEditBox_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
            RaiseEvent MaskedEditKeyPress(sender, e)
        End Sub
      
        Private Sub MyMaskedEditBox_PreviewKeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs)
            RaiseEvent MaskedEditPreviewKeyDown(sender, e)
        End Sub
    End Class


    and in the form where the control is:
    Private Sub MyMaskedEditControl1_MaskedEditPreviewKeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles MyMaskedEditControl1.MaskedEditPreviewKeyDown
        MessageBox.Show(e.KeyValue.ToString())
    End Sub
    Private Sub MyMaskedEditControl1_MaskedEditKeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyMaskedEditControl1.MaskedEditKeyPress
        MessageBox.Show(e.KeyChar.ToString())
    End Sub

    Hope this helps
    richard
  6. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 10 Nov 2010 Link to this post

    Hello guys,

    The problem here is that when the keydown, keypressed and other events are being fired from the MaskedEditBoxElement not from the control itself, so you can use the following:

        this.customMaskedEditBox1.MaskedEditBoxElement.KeyPress += new KeyPressEventHandler(MaskedEditBoxElement_KeyPress);
     
    void MaskedEditBoxElement_KeyPress(object sender, KeyPressEventArgs e)
    {
        MessageBox.Show("KeyPress called");
    }

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga
Back to Top
UI for WinForms is Visual Studio 2017 Ready