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
249 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 86

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

Get Actionable Data from Your Monitoring Solution

Your communication platform is only as good as the relevance of the information you send. Ensure your alerts get to the right people every time with actionable responses. Create escalation rules that ensure everyone follows the process and nothing is left to chance.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

717 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