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
244 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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!
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

820 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