none
Getting TextBox.Text Inside CustomControl on KeyDown

    Question

  • Hi all,

    I have a simple custom control, basically is a custom textbox with a structure like this:

    - CustomControl
           -Panel
                 -Textbox (txt)

    Public Class DxTextBox
    
    Public Event DxKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
    
    
        Public Property Textx() As String
            Get
                Return txt.Text
            End Get
            Set(ByVal value As String)
                txt.Text = value
            End Set
        End Property
    
     Private Sub txt_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txt.KeyDown
            RaiseEvent DxKeyDown(sender, e)
        End Sub
    
     Public Sub New()
            InitializeComponent()
     End Sub
    
    End Class

    Now, the problem I have is:

    when I type "a" the customcontrol in parent form returns empty

    when I type "b" ("ab")  the customcontrol in parent form returns "a"

    So I have a delay of one character, any ideas what happens?

    Thanks in advance


    G.Waters



    Wednesday, April 19, 2017 1:30 AM

Answers

  • So I have a delay of one character, any ideas what happens?

    The text has not been updated when the keydown event fires.  It is possible that code in the keydown event handler will suppress or handle the keystroke, so it is not recorded in the text until that event has completed.  Your keydown event at the custom control level should use the same signature as the keydown event at the textbox level, and should receive the same argument.

    • Marked as answer by George Waters Thursday, April 05, 2018 5:45 PM
    Wednesday, April 19, 2017 1:52 AM

All replies

  • So I have a delay of one character, any ideas what happens?

    The text has not been updated when the keydown event fires.  It is possible that code in the keydown event handler will suppress or handle the keystroke, so it is not recorded in the text until that event has completed.  Your keydown event at the custom control level should use the same signature as the keydown event at the textbox level, and should receive the same argument.

    • Marked as answer by George Waters Thursday, April 05, 2018 5:45 PM
    Wednesday, April 19, 2017 1:52 AM
  • Acamar,

    This is the code at the parent form, I am using the same signature as in the customcontrol:

     Private Sub DxTextBox3_DxKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DxTextBox3.DxKeyDown
            MsgBox(DxTextBox3.Textx)
    End Sub

    So, what should I do in order to have my custom control working as if it were textbox?


    G.Waters

    Wednesday, April 19, 2017 2:41 AM
  • So, what should I do in order to have my custom control working as if it were textbox?

    It is working like a text box.   That code executes when the key down event in the textbox is fired. But at that point the Text property of the 'txt' textbox has not been updated, so the character is not included in what is returned as the Textx property.  The code is working as it should - it's just that you are using the wrong event to enquire about the state of the control.

    If you test that property after the keydown event has finished processing (eg, the next time a key is struck) the you will see the character - in other words, one keypress too late.

    You need to move that code to an event that fires after the Text property of the txt textbox has been updated.


    • Edited by AcamarMVP Wednesday, April 19, 2017 3:42 AM sp
    Wednesday, April 19, 2017 3:42 AM
  • May This Help You

    Correct Event to handle is KeyUp

    Custom Control --> Corrected Code:

    Public Class DxTextBox
    
        Public Event DxKeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
    
    
        Public Property Textx() As String
            Get
                Return txt.Text
            End Get
            Set(ByVal value As String)
                txt.Text = value
            End Set
        End Property
    
        Private Sub txt_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txt.KeyUp
            RaiseEvent DxKeyUp(sender, e)
        End Sub
    
        Public Sub New()
            InitializeComponent()
        End Sub
    
    End Class
    Test in Parent Form:
    Private Sub DxTextBox3_DxKeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DxTextBox3.DxKeyUp
            MsgBox(DxTextBox3.Textx)
    End Sub
    Best Regards
    Vikram Manjare
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.


    Wednesday, April 04, 2018 7:18 PM
  • I don't understand why you don't use the TextBox's text changed event to get the Text. Why is key up or down necessary?

    La vida loca

    Wednesday, April 04, 2018 11:48 PM
  • I don't understand why you don't use the TextBox's text changed event to get the Text. Why is key up or down necessary?

    The thread is a year old.  It is very unlikely that OP is still considering responses.

    Wednesday, April 04, 2018 11:52 PM
  • I don't understand why you don't use the TextBox's text changed event to get the Text. Why is key up or down necessary?

    The thread is a year old.  It is very unlikely that OP is still considering responses.


    LOL.

    La vida loca

    Thursday, April 05, 2018 1:11 AM