Solved

Visual Studio 2005 desktop program: how to let TextChanged event only respond to user input from GUI, but not text valus assignment from the code?

Posted on 2009-04-04
2
240 Views
Last Modified: 2013-12-17
Hi Experts:

    I made a GUI control with a TextBox and a internal state variable. I want it to work like this: when user change the string in the textbox, then internal state variable change. I used TextChanged event handler of the textbox to implement this. However, inside the control there is another piece of code assigning values to the textbox, which will also trigger the TextChanged event.

    I only want user modification of the string to change the state variable, but not the internal code operation, what can I do?

    I currently only have a basic idea of using KeyPress event instead of TextChanged event, is this a good idea? or there is something easier, such as blocking the event?

   Thank you!
0
Comment
Question by:huangs3
2 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
Comment Utility
You can use RemoveHandler/AddHandler to turn off the TextChanged() Event while you set the Text() Property:
Public Class Form1
 

    Private IgnoreChange As Boolean = False
 

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

        Debug.Print("TextChanged: " & TextBox1.Text)

    End Sub
 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        SetText(TextBox1, "New Text Here")

    End Sub
 

    Private Sub SetText(ByVal tb As TextBox, ByVal txt As String)

        RemoveHandler tb.TextChanged, AddressOf TextBox1_TextChanged

        Me.TextBox1.Text = "New Text Here"

        AddHandler tb.TextChanged, AddressOf TextBox1_TextChanged

    End Sub
 

End Class

Open in new window

0
 

Author Closing Comment

by:huangs3
Comment Utility
Thank you!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now