Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine If Changed Event Occurred from User Input Or Not

Tags:

c#

.net

In C#, the Changed event for a control (say, a numericupdown) gets fired whether the value was change directly by the user or if it was changed programatically as the result of some other event.

Is there a way to determine whether the event occurred as a result of user input? For example, both manually changing the value of numericUpDown1 and clicking on button1 will display "value changed". What if I only wanted to display "value changed" if it was changed through the user clicking on the up/down arrows in the control and not as a result of clicking on button1?

    private void numericUpDown1_ValueChanged(object sender, EventArgs e)
    {
        MessageBox.Show("value changed");
    }

    private void button1_Click_1(object sender, EventArgs e)
    {
        numericUpDown1.Value = 3;
    }
like image 279
Dave Avatar asked Mar 16 '09 15:03

Dave


People also ask

Which event tells you when a user has changed the value of a text input?

The input event triggers every time after a value is modified by the user. Unlike keyboard events, it triggers on any value change, even those that does not involve keyboard actions: pasting with a mouse or using speech recognition to dictate the text.

What is the difference between input and change event?

Note: The input event is fired every time the value of the element changes. This is unlike the change event, which only fires when the value is committed, such as by pressing the enter key, selecting a value from a list of options, and the like.

What event is fired when a change to an input select or textarea value is committed by the user?

The change event is fired for input , select , and textarea elements when an alteration to the element's value is committed by the user.

Which event is triggered when a form field is changed?

Whenever the value of a form field changes, it fires a "change" event.


2 Answers

There is no nice way to do it. You can find workarounds for specific cases, e.g.

  • listen to MouseDown or something instead of valueChanged on the numeric drop down.

  • Set a flag in button click event handler that will prohibit the message box from showing.

    In general you should try to organize your form in a way that it doesn't matter where the value got changed.

like image 113
Grzenio Avatar answered Sep 21 '22 06:09

Grzenio


You could check to see if the numericUpDown is the ActiveControl. When you set the value of the numericUpDown during the button click, button1 should be the ActiveControl. When the user changes the value via the numericUpDown, then the numericUpDown should be the ActiveControl.

if(numericUpDown1 == this.ActiveControl)
{
    MessageBox.Show("value changed");
}
like image 38
firedfly Avatar answered Sep 20 '22 06:09

firedfly