如何在验证 NumericUpDown 控件之前访问其文本?

发布于 2024-12-21 18:37:35 字数 311 浏览 1 评论 0原文

我有一个带有 System.Windows.Forms.NumericUpDown 控件的表单。

假设范围是从 0 到 100,当前值(通过微调器获得)是 100。我可以输入超出允许范围的数字(例如 567),但是当我单击表单上的“确定”时重置该值,它只是默默地将超出范围的值设置为 100 并关闭表单。

客户希望得到明确的消息,表明该号码超出范围。因此,我查看了关闭表单上的 NumericUpDown.Text 属性,但该属性返回的是“100”而不是“567”。

我在哪里(或如何)可以“捕获”控件中出现的文本是“567”的事实?

I have a form with a System.Windows.Forms.NumericUpDown control.

Say that the range is from 0 to 100, and the current value (arrived at via the spinner) is 100. I can type in a number that is outside of the allowable range (say 567) but when I click OK on the form to reset the value, it just silently sets the out of range value to 100 and closes the form.

The customer wants an explicit message that the number is out of range. So, I looked at checking the NumericUpDown.Text property on the form close, but that property gives me back "100" not "567".

Where (or how) can I "catch" the fact that the text appearing in the control is "567"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

糖粟与秋泊 2024-12-28 18:37:35

您可以使用 这个问题通过获取对 NumericUpDown 内的 TextBox 的引用并处理其来捕获无效值验证事件。在您的处理程序中,TextBox.Text 属性将具有要测试的无效值。在 .NET 2.0 Winforms 中为我工作。

You can use the answer from this question to capture the invalid value by getting a reference to the TextBox inside the NumericUpDown and handling its Validating event. In your handler, the TextBox.Text property will have the invalid value to test against. Works for me in .NET 2.0 Winforms.

苍景流年 2024-12-28 18:37:35

你可以尝试这个,唯一的是该值仍然会重置为 100,但用户仍然会收到超出范围值的通知:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            int val = Convert.ToInt32(((UpDownBase)numericUpDown1).Text);

            if (val > 100)
            {
                MessageBox.Show("The value " + ((UpDownBase)numericUpDown1).Text + 
                " is out of range", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                e.Cancel = true;
            }
        }

You could try this, the only thing is that the value will still be reset to 100, but the user will still be notified of the out of range value:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            int val = Convert.ToInt32(((UpDownBase)numericUpDown1).Text);

            if (val > 100)
            {
                MessageBox.Show("The value " + ((UpDownBase)numericUpDown1).Text + 
                " is out of range", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                e.Cancel = true;
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文