Did "MouseUp" event change value of NumericUpDown?

I need to determine if the value of a NumericUpDown control was changed by a mouseUp event.

I need to call an expensive function when the value of a numericupdown has changed. I can't just use "ValueChanged", I need to use MouseUp and KeyUp events.

Basically, I need to know:

Did the value of the numericUpDown change when the user let go of the mouse? If any area which is not highlighted in red is clicked, the answer is no. I need to IGNORE the mouse up event, when ANYWHERE but the red area is clicked.

How can I determine this by code? I find events a little confusing.

Answers


This will fire when the user releases the mouse button. You might want to investigate which mousebutton was released.

EDIT

    decimal numvalue = 0;
    private void numericUpDown1_MouseUp(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left && numvalue != numericUpDown1.Value)
        {
            //expensive routines
            MessageBox.Show(numericUpDown1.Value.ToString());
        }

        numvalue = numericUpDown1.Value;
    }

EDIT 2 This will determine if the left mousebutton is still down, if it is exit before performing expensive routine, doesn't help with keyboard button down.

    private void numericUpDown1_ValueChanged(object sender, EventArgs e)
    {
        if ((Control.MouseButtons & MouseButtons.Left) == MouseButtons.Left)
        {
            return;
        }
        //expensive routines


    }

Edit 3

How to detect the currently pressed key?

Will help solve the Any key down, Though I think the only ones that matter are the arrow keys


Problem - I need to IGNORE the mouse up event, when ANYWHERE but the red area is clicked.

Derive a custom numeric control as shown below. Get the TextArea of the Numeric Control and ignore the KeyUp.

class UpDownLabel : NumericUpDown
{
    private Label mLabel;
    private TextBox mBox;

    public UpDownLabel()
    {
        mBox = this.Controls[1] as TextBox;
        mBox.Enabled = false;
        mLabel = new Label();
        mLabel.Location = mBox.Location;
        mLabel.Size = mBox.Size;
        this.Controls.Add(mLabel);
        mLabel.BringToFront();
        mLabel.MouseUp += new MouseEventHandler(mLabel_MouseUp);
    }


    // ignore the KeyUp event in the textarea
    void mLabel_MouseUp(object sender, MouseEventArgs e)
    {
        return;
    }

    protected override void UpdateEditText()
    {
        base.UpdateEditText();
        if (mLabel != null) mLabel.Text = mBox.Text;
    }
}

In the MainForm, update your designer with this control i.e. UpDownLabel:-

private void numericUpDown1_MouseUp(object sender, MouseEventArgs e)
{
    MessageBox.Show("From Up/Down");
}

Referred from - https://stackoverflow.com/a/4059473/763026 & handled the MouseUp event.

Now, use this control instead of the standard one and hook on the KeyUp event. You will always get the KeyUp event from the Up/Down button only i.e. RED AREA when you click the spinner [Up/Down button, which is again a different control derived from UpDownBase].


I think you should use Leave event that when the focus of NumericUpDown control gone, it would called.

    int x = 0;
    private void numericUpDown1_Leave(object sender, EventArgs e)
    {
        x++;
        label1.Text = x.ToString();
    }

Need Your Help

How can I change from Fragment to Fragment with an ImageButton?

java android xml android-fragments mobile

Im trying to go from MainFragment to AddPostFragment with an ImageButton.