Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I implement a TextChanging property on a TextBox?
    primarykey
    data
    text
    <p>I figured there would be a question like this already, but I didn't have any luck searching. I saw a question where someone asked the same thing but the answer was to use the TextChanged event. That's not what I want though.</p> <p>TextBox has an event for TextChanged that occurs after the Text property has been changed. I need my control to raise an event before the Text property is actually changed to validate data. If it is valid the Text can be changed, if it is not valid the Text does not get changed.</p> <p>Here's what I tried:</p> <pre><code>Public Class TextChangingEventArgs Inherits System.ComponentModel.CancelEventArgs Private p_sNewValue As String Public Sub New() p_sNewValue = String.Empty End Sub Public Sub New(sNewValue As String) p_sNewValue = sNewValue End Sub Public Property NewValue As String Get Return p_sNewValue End Get Set(value As String) p_sNewValue = value End Set End Property End Class Public Class BetterTextBox Inherits TextBox Public Event TextChanging(sender As Object, e As TextChangingEventArgs) Public Overrides Property Text As String Get Return MyBase.Text End Get Set(value As String) Dim e As New TextChangingEventArgs(value) RaiseEvent TextChanging(Me, e) If e.Cancel = False Then MyBase.Text = value End If End Set End Property End Class </code></pre> <p>My in my Form I handle the TextChanging event:</p> <pre><code>Private Sub BetterTextBox1_TextChanging(sender As System.Object, e As TextChangingEventArgs) Handles BetterTextBox1.TextChanging e.Cancel = Not Regex.IsMatch(e.NewValue, REGEX_PATTERN) End Sub </code></pre> <p>This works for programmatically setting the Text value of the BetterTextBox control, but it does not work when you are typing into the text box.</p> <p>Does anyone know what I need to do to get this to work?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload