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
243 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
ID: 24069062
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
ID: 31566626
Thank you!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

776 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