none
Catch event from textbox inside a custom control

    Question

  • Hi all,

    I have a simple custom control with this structure:

    - CustomControl
           -Panel
                 -Textbox

    So this customcontrol acts just like a textbox (I'm just trying to remove textbox border and add some effect) and I need to pass the event TextChanged to the parent form, any ideas how to achieve it?

    Thanks in advance !!!

    UPDATE:

    I have created an event inside the custom control:

    Public Event DxKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)

    and try to catch the event in the textbox:

     Private Sub txt_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txt.KeyDown
            RaiseEvent DxKeyDown(sender, e)
     End Sub
    seems to work, but when I press a key it shows empty, if I press a second key it shows only the first and so on, so I always have a delay of 1 character, any ideas.


    G.Waters


    Tuesday, April 18, 2017 8:16 PM

Answers

  • George,

    Create a public event for that and in the consuming class (for example, Form1), subscribe to that event.

    If you just need it to be a flag that something has changed, a simple default EventArgs will work.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    • Marked as answer by George Waters Wednesday, April 19, 2017 12:13 AM
    Tuesday, April 18, 2017 8:29 PM

All replies

  • George,

    Create a public event for that and in the consuming class (for example, Form1), subscribe to that event.

    If you just need it to be a flag that something has changed, a simple default EventArgs will work.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    • Marked as answer by George Waters Wednesday, April 19, 2017 12:13 AM
    Tuesday, April 18, 2017 8:29 PM
  • I have created an event inside the custom control:

    Public Event DxKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)

    and try to catch the event in the textbox:

     Private Sub txt_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txt.KeyDown
            RaiseEvent DxKeyDown(sender, e)
     End Sub

    then in the parent form:

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

    it seems to work, but when I press a key it shows empty, if I press a second key it shows only the first and so on, so I always have a delay of 1 character, any ideas.


    G.Waters


    Tuesday, April 18, 2017 8:43 PM
  • I have created an event inside the custom control:

    Public Event DxKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)

    and try to catch the event in the textbox:

     Private Sub txt_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txt.KeyDown
            RaiseEvent DxKeyDown(sender, e)
     End Sub

    then in the parent form:

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

    it seems to work, but when I press a key it shows empty, if I press a second key it shows only the first and so on, so I always have a delay of 1 character, any ideas.


    G.Waters


    That's a bit different; the signature in .TextChanged is just an empty EventArgs. ;-)

    *****

    In the revised one, what's in "e" from your TextBox? Put a breakpoint in and step through it - that usually proves it out.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Tuesday, April 18, 2017 8:50 PM
  • Hi  Frank,

    Well, the "e" argument shows the pressed key properly, the problem seems to be that the KeyDown event is fired before the textbox.text property is filled with the character.

    This is how the custom control looks like:

    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
    In the custom control a TextBox is used (called "txt")


    G.Waters


    Tuesday, April 18, 2017 11:42 PM
  • George,

    We're getting away from your question but have you considered using some other event like keypress instead?

    ***** EDIT *****

    This shows the sequence:

    https://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress(v=vs.110).aspx


    "A problem well stated is a problem half solved.” - Charles F. Kettering


    Tuesday, April 18, 2017 11:49 PM
  • I'll make another question, thanks for your help

    G.Waters

    Wednesday, April 19, 2017 12:12 AM