how to remove/deactive a handler from an event (Textbox Changed) from Winform using VB.NET running on Visual Studio 2019

Josh S 26 Reputation points
2021-12-13T01:34:09.213+00:00

Hi,
First of all I want to say thank you and my sincere gratitude this community, for monumental help and support you all are providing to the Developer/Engineer/Scientist communities across the Globe.
I am facing a huge challenge that involve a Textbox Text Changed event. So here you go.

I have a WinForms application that is running on Windows 10 environment. Multiples copy on the same form running at the same time on different Windows Console. At least minimum two forms can run at any time and maximum fifteen forms.

A person can select a specific sailboat lets say its SailBoatA on a console and select a button "Details", which open the SailBoatDetails form. This provides details about the sailboatA.
Another console someone could select another sailboat - SailBoatB and select details button, which open for SailBoatB - SailBoatDetails form.

SailBoatDetails form has following two text boxes:
**Destination Distance:
Arriving Port:****

On SailBoatDetails form for SailBoatA, an user could enter value 1100 in "Destination Distance" text box.
Then user will select an Apply button on the form and this value will be saved on a table. The enter value still displayed on the text box.

However on another console SailBoatB "Destination Distance" text box displayed the same value. Which should not.
I debugged and observed that in another console for SailBoatB

Private Sub txtDestinationDistance_TextChanged(sender As Object, e As EventArgs) Handles txtDestinationDistance.TextChanged
sub gets activated.

Sender object has the 1100.

How I can stop textbox change events after a user select "Apply" button for "SailBoatA"?
So I can prevent this event to go any other console?

Here is the code for the text box

Private Sub txtDestinationDistance_TextChanged(sender As Object, e As EventArgs) Handles txtDestinationDistance.TextChanged

    Dim thisBoat As SpecialBoatA = AllBoats.HookAnyBoat

    If thisBoat IsNot Nothing Then
        thisBoat.distance = txtDestinationDistance.Text

        ...
    ...

    Else

        txtDestinationDistance.Text = ""
    End If

End Sub

Developer technologies | VB
{count} votes

3 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2021-12-13T03:22:23.193+00:00

    Rather than use Handles txtDestinationDistance.TextChanged, use AddHandler and RemoveHandler and eliminate using Handles

    Example, type Karen or karen or KAREN and insert Payne

    Public Class Form1  
        Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs)  
      
            If String.Equals(TextBox1.Text, "karen", StringComparison.OrdinalIgnoreCase) Then  
                RemoveHandler TextBox1.TextChanged, AddressOf TextBox1_TextChanged  
                TextBox1.Text = "Karen Payne"  
                TextBox1.SelectionStart = TextBox1.Text.Length  
                TextBox1.SelectionLength = 0  
                AddHandler TextBox1.TextChanged, AddressOf TextBox1_TextChanged  
            End If  
      
        End Sub  
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
            AddHandler TextBox1.TextChanged, AddressOf TextBox1_TextChanged  
        End Sub  
    End Class  
      
    
    1 person found this answer helpful.

  2. Josh S 26 Reputation points
    2021-12-14T06:35:03.183+00:00

    Hi,
    Countless gratitude for this code sample and implemented in my code, here is the sample

    Private Sub ShipDetails_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ....
    ....
    AddHandler txtDestinationDistance.TextChanged, AddressOf txtDestinationDistance_TextChanged
    End Sub

    Private Sub txtDestinationDistance_TextChanged(sender As Object, e As EventArgs)

        Dim thisBoat As SpecialBoatA = AllBoats.HookAnyBoat
    
        If thisBoat IsNot Nothing Then
        'First I try to add the following line in this part of the code. 
        'RemoveHandler txtDestinationDistance.TextChanged, AddressOf txtDestinationDistance_TextChanged
    
            thisBoat.distance = txtDestinationDistance.Text
    
            ...
        ...
    
        Else
    
            txtDestinationDistance.Text = ""
        End If
    'Then I commented out the line from "IF..then" part and add the following line. 
        RemoveHandler txtDestinationDistance.TextChanged, AddressOf txtDestinationDistance_TextChanged
    

    End Sub

    Private Sub Apply_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Apply.Click

    'Some other codes
    Save_Data(thisBoat)

    RemoveHandler txtDestinationDistance.TextChanged, AddressOf txtDestinationDistance_TextChanged

    End Sub

    I still have the same issue, another console SailBoatB "Destination Distance" text box displayed the same value that I entered for
    SailBoatA in a different console .

    I debugged and observed that for SailBoatB, sender object displayed the value I entered for SailBoatA.

    Also I added "RemoveHandler txtDestinationDistance.TextChanged..." line in "Apply" push button code, towards the end after entered text value is saved. This is test, if this will stop the issue but it didn't.

    Please enlighten me, thank you.

    0 comments No comments

  3. Josh S 26 Reputation points
    2021-12-14T06:49:26.64+00:00

    I am curious if this issue could be stop using an event. I am not familiar with Event and Event handler.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.