【问题标题】:Did "MouseUp" event change value of NumericUpDown?“MouseUp”事件是否改变了 NumericUpDown 的值?
【发布时间】:2012-06-11 13:12:36
【问题描述】:

我需要确定 NumericUpDown 控件的值是否被 mouseUp 事件更改。

当 numericupdown 的值发生变化时,我需要调用一个昂贵的函数。我不能只使用“ValueChanged”,我需要使用 MouseUp 和 KeyUp 事件。

基本上,我需要知道:

当用户放手时 numericUpDown 的值是否发生了变化 老鼠? 如果单击任何未以红色突出显示的区域,则 答案是否定的。我需要在任何地方但点击红色区域时忽略鼠标向上事件。

如何通过代码确定这一点?我觉得事件有点混乱。

【问题讨论】:

  • "我不能只使用 "ValueChanged",我需要使用 MouseUp 和 KeyUp 事件。" - 为什么?
  • "如果点击了任何没有以红色突出显示的区域,答案是否定的" WHAT?考虑到我们不知道您的程序做什么或应该做什么,您可能想重新陈述您的问题。红色适合在哪里?你通常如何让你的 numericupdown 改变?
  • @TomW 我说,我需要调用一个耗费大量时间的昂贵的 3D 渲染函数。如果我每次调用它,值都会发生变化,这会导致 UI 非常滞后。我已经使用带计时器的 ValueChanged 来尝试解决这个问题,但是当用户放开鼠标时,我只是希望它立即再次渲染。
  • 当用户“放开鼠标”点击某物时,鼠标在做什么?
  • @K'Leg 试试这个:获取 NumericUpDown 控件。每次值更改时都打印“ValueChanged”。单击并按住向上/向下箭头。它被调用了多少次?大概每秒30次以上。这是我无法接受的。但是,当用户放开鼠标(MouseUp)时,我需要它再次渲染。不过,我应该只重新渲染 - 如果这个数字 up down 的值发生了变化。

标签: c# .net winforms events


【解决方案1】:

这将在用户释放鼠标按钮时触发。您可能想调查释放了哪个鼠标按钮。

编辑

    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;
    }

编辑 2 这将确定鼠标左键是否仍然按下,如果它在执行昂贵的例程之前退出,对键盘按钮按下没有帮助。

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


    }

编辑 3

How to detect the currently pressed key?

将有助于解决 Any 键按下,虽然我认为唯一重要的是箭头键

【讨论】:

  • 编辑以反映需求
【解决方案2】:

问题 - 我需要在任何地方但点击红色区域时忽略鼠标向上事件。

派生一个自定义数值控件,如下所示。获取 Numeric Control 的 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;
    }
}

在 MainForm 中,使用此控件更新您的设计器,即 UpDownLabel:-

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

引用自 - https://stackoverflow.com/a/4059473/763026 并处理了 MouseUp 事件。

现在,使用这个控件而不是标准控件并挂上 KeyUp 事件。当您单击 spinner [上/下按钮,这又是一个不同的控件派生 来自UpDownBase]。

【讨论】:

    【解决方案3】:

    我认为你应该使用Leave事件,当NumericUpDown控件的焦点消失时,它会调用。

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

    【讨论】:

    • 谢谢,但不是我需要的。我需要在单击时立即触发事件,而不是在焦点消失时触发:(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多