Search code examples
vb.nettextchanged

How to prevent TextChanged event when dialog is loading


I have a dialog in my Program with 2 textboxes; Path and Prefix. and the following event.

  Private Sub Path_TextChanged(sender As Object, e As EventArgs) Handles Path.TextChanged
        Prefix.Text = GetDefaultPrefix(Path.Text)
   End Sub

My Path has a default value which is set before showing the dialog.

I don't want GetDefaultPrefix to be called when the dialog is loading, but only afterwards when the Path value is being changed. Is it possible to do so?


Solution

  • You could add the old Boolean hack where you create a Boolean value set to True, in the Load event change the value to False, and in the TextChanged event only execute the code if the value is False:

    Private is_startup As Boolean = True
    Private Sub FormDialog_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        is_startup = False
    End Sub
    
    Private Sub Path_TextChanged(sender As Object, e As EventArgs) Handles Path.TextChanged
        If Not is_startup Then Prefix.Text = GetDefaultPrefix(Path.Text)
    End Sub