Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to distinguish in a C# event if a change was made from code or by the user?

Tags:

c#

winforms

I have a simple TextBox that is empty in the beginning. I have a simple event, _TextChanged, to know when the user changed anything in that TextBox. However, the event fires if I do anything with it myself from within code. Like setting textbox.Text = "Test"; or similar.

    private void textNazwa_TextChanged(object sender, EventArgs e) {         changesToClient = true;     } 

How do I make the event only fire on user interaction and not code changes?

like image 214
MadBoy Avatar asked Nov 27 '11 19:11

MadBoy


People also ask

Is C or C+ Better?

C is still in use because it is slightly faster and smaller than C++. For most people, C++ is the better choice. It has more features and more applications, which allow you to explore various roles. For most people, learning C++ is also easier especially if you are familiar with object-oriented programming.

Is C+ the same as C?

The main difference between C and C++ is that C++ is a younger, more abstract language. C and C++ are both general-purpose languages with a solid community. C is a lightweight procedural language without a lot of abstraction. C++ is an object-oriented language that provides more abstraction and higher-level features.

How does a main () function in C++ differ from main () in C?

Main function of C may be void, when returns nothing. In C++ main can not be void. It will return int value. In c main function, we do declare all variables together in beginning of the program.


1 Answers

I've been using this process, and it seems to work well. If the event fires and the focus is not in the textbox, then I ignore the request, so when I set the text the focus is elsewhere, but when the user is typing in the textbox, it has the focus, so I acknowledge the changes.

private void textNazwa_TextCanged(object sender, EventArgs e) {     if ( !textNazwa.Focused)          return;  } 
like image 85
rwg Avatar answered Sep 21 '22 16:09

rwg