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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

863 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

27 Experts available now in Live!

Get 1:1 Help Now