简体   繁体   English

“ MouseUp”事件是否更改了NumericUpDown的值?

[英]Did “MouseUp” event change value of NumericUpDown?

I need to determine if the value of a NumericUpDown control was changed by a mouseUp event . 我需要确定mouseUp事件是否更改了NumericUpDown控件的值。

I need to call an expensive function when the value of a numericupdown has changed. 当numericupdown的值已更改时,我需要调用一个昂贵的函数。 I can't just use "ValueChanged", I need to use MouseUp and KeyUp events. 我不能只使用“ ValueChanged”,我需要使用MouseUp和KeyUp事件。

在此处输入图片说明

Basically, I need to know: 基本上,我需要知道:

Did the value of the numericUpDown change when the user let go of the mouse? 用户放开鼠标时,numericUpDown的值是否发生了变化? 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. 我发现事件有点令人困惑。

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. 编辑2这将确定鼠标左键是否仍在按下,如果在执行昂贵的例行程序之前退出鼠标,则无法按下键盘。

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


    }

Edit 3 编辑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 这将有助于解决Any键问题,尽管我认为唯一重要的是箭头键

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. 获取数值控件的TextArea并忽略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 ie UpDownLabel :- 在MainForm中,使用此控件(即UpDownLabel更新设计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. 从被称为- https://stackoverflow.com/a/4059473/763026及处理MouseUp事件。

Now, use this control instead of the standard one and hook on the KeyUp event. 现在,使用此控件代替标准控件,并挂接KeyUp事件。 You will always get the KeyUp event from the Up/Down button only ie RED AREA when you click the spinner [Up/Down button, which is again a different control derived from UpDownBase]. 当您单击微调器[Up / Down按钮,这又是一个从UpDownBase派生的控件]时,将始终仅从Up / Down按钮(即红色区域)中获取KeyUp事件

I think you should use Leave event that when the focus of NumericUpDown control gone, it would called. 我认为您应该使用Leave事件,当NumericUpDown控件的焦点消失时,它将被调用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM