【问题标题】:How to hold the invalid value for NumericUpDown after it loses focus?失去焦点后如何保持 NumericUpDown 的无效值?
【发布时间】:2010-02-23 04:08:47
【问题描述】:

在我的项目中有一个UserControl,它包含一个NumericUpDown ctrl,它的有效值范围是从10到100

所以如果用户在 NumericUpDown ctrl 中输入 200,那么在焦点切换到其他 ctrl 后它的值会自动变为 100,这对客户来说看起来有点好奇,因为他们可能会在 NumericUpDown 中输入 200 后点击 OK 按钮ctrl,他们需要一个消息框,告诉他们输入的值不在范围内。

但问题是,如果输入的值超出其范围,则 NumericUpDown 的值会在焦点更改后自动更改。

那么如何实现呢?

Sameh Serag,这是我测试过的代码。我在表单上添加了一个按钮,但什么也没做。我的结果是在我输入 200 并单击按钮后,只显示一个值为 100 的消息框。在我输入 200 并按下 tab 键后,它只会显示一个值为 200 的消息框,并且 NumericUpDown 中的文本值更改为 100。很好奇 :-) 无论如何非常感谢您的帮助!顺便说一句,.Net 框架版本是 2.0 和 sp2 对我来说。

public partial class Form1 : Form
{
    private TextBox txt;

    public Form1()
    {
        InitializeComponent();

        txt = (TextBox)numericUpDown1.Controls[1];
        txt.Validating += new CancelEventHandler(txt_Validating);
    }

    void txt_Validating(object sender, CancelEventArgs e)
    {
        MessageBox.Show(txt.Text);
    }
}

【问题讨论】:

  • 我不相信这是可能的,但是 +1 因为我过去也遇到过这个问题。我最终切换回普通的旧文本框。
  • +1 给马特。 NumericUpDown 控件是有史以来最恶心的控件。我从未观察到用户实际点击这些小按钮或使用箭头键。他们只是打字。如果您使用的是平板电脑,上帝会帮助您。
  • @Carlos_Liu:在您修改问题后,我在下面更新了我的答案。顺便说一句:它在你上面描述的所有场景中都对我有用。试试看,让我们知道结果;-)
  • 不要在问题正文中对特定用户发表评论

标签: c# .net winforms .net-2.0


【解决方案1】:

诀窍是让文本框嵌入到数字 updown 控件中,并处理其验证事件。

以下是如何完成它:

创建一个虚拟表单并添加一个数字updown控件和一些其他控件,当数字unpdown控件失去焦点时,表单的文本将设置为用户输入的值。

这里是我所做的代码:

public partial class Form1 : Form
    {
        TextBox txt;
        public Form1()
        {
            InitializeComponent();
            txt = (TextBox)numericUpDown1.Controls[1];//notice the textbox is the 2nd control in the numericupdown control
            txt.Validating += new CancelEventHandler(txt_Validating);
        }
        void txt_Validating(object sender, CancelEventArgs e)
        {
            this.Text = txt.Text;
        }
    }

编辑:

@Carlos_Liu:好的,我现在可以看到问题了,你可以通过 TextChanged 事件来实现,只需将值保存在一个虚拟变量中并在 txt_Validating 重用它,但要小心,不要更新这个变量,除非文本框被聚焦

这是新的示例代码:

public partial class Form1 : Form
{
    TextBox txt;
    string val;
    public Form1()
    {
        InitializeComponent();
        txt = (TextBox)numericUpDown1.Controls[1];//notice the textbox is the 2nd control in the numericupdown control
        txt.TextChanged += new EventHandler(txt_TextChanged);
        txt.Validating += new CancelEventHandler(txt_Validating);
    }

    void txt_TextChanged(object sender, EventArgs e)
    {
        if (txt.Focused) //don't save the value unless the textbox is focused, this is the new trick
            val = txt.Text;
    }
    void txt_Validating(object sender, CancelEventArgs e)
    {
        MessageBox.Show("Val: " + val);
    }
}

编辑#2

@Carlos_Liu:如果需要保留输入的值,还是有窍门的:@文本框的Validating事件,检查值,如果不在范围内,取消失去焦点!

这是一个新版本的代码:

public partial class Form1 : Form
{
    TextBox txt;
    string val;
    public Form1()
    {
        InitializeComponent();
        txt = (TextBox)numericUpDown1.Controls[1];
        txt.TextChanged += new EventHandler(txt_TextChanged);
        txt.Validating += new CancelEventHandler(txt_Validating);
    }

    void txt_TextChanged(object sender, EventArgs e)
    {
        if (txt.Focused)
            val = txt.Text;
    }
    void txt_Validating(object sender, CancelEventArgs e)
    {
        int enteredVal = 0;
        int.TryParse(val, out enteredVal);
        if (enteredVal > numericUpDown1.Maximum || enteredVal < numericUpDown1.Minimum)
        {
            txt.Text = val;
            e.Cancel = true;
        }
    }
}

【讨论】:

  • 好像不行,因为当输入文本框的验证事件时,该值已根据NumericUpDown的最小值/最大值被截断
  • 不,它对我有用。确保您处理的是 Validating 而不是 Validated 事件。单独尝试,然后将其嵌入到您的项目中。
  • @Carlos_Liu:也许问题是您在验证事件期间弹出了一个消息框,导致控件失去焦点(当它失去焦点时,它会破坏您的条目)?跨度>
  • 如果是消息框导致控件失去焦点,请尝试显示工具提示
  • 很棒的把戏。我能够删除一个讨厌的 NumericUpDown 子类,以便在用户使用这几行代码输入文本框时进行捕获。
猜你喜欢
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
  • 2012-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多