NumericUpDown 失去焦点后如何保持其无效值?

发布于 2024-08-22 18:17:47 字数 911 浏览 3 评论 0原文

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

因此如果用户在NumericUpDown ctrl中输入200,则其值将在之后自动更改为100焦点更改为其他 ctrl,这对客户来说看起来有点好奇,因为他们可能在 NumericUpDown ctrl 中输入 200 后单击“确定”按钮,他们需要一个消息框来告诉他们输入的值不在范围内。

但问题是,如果输入的值超出其范围,则焦点更改后 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);
    }
}

In my project there is an UserControl which includes a NumericUpDown ctrl, and its valid value range is from 10 to 100,

so if user inputs 200 in NumericUpDown ctrl, then its value will changed to 100 automatically after the focus changed to other ctrl, it looks a little bit curious for customer, because they may click the OK button after input 200 in the NumericUpDown ctrl, they need a message box that tells them the value they input is not in the range.

But the question is the value for NumericUpDown will change automatically after the focus changed if the value input is out of its range.

So how to implement this?

Sameh Serag, this is the code I have tested. I have add a button on the form but did nothing. The result for me is after I input 200 and click the button, only a messagebox with value 100 is shown. After I input 200 and press the tab key, it will only show a messagebox with the value 200 and the text value in NumericUpDown is changed to 100. So curious :-) Anyway thank you very much for your help! BTW, the .Net framework version is 2.0 with sp2 for me.

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

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

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

发布评论

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

评论(1

小红帽 2024-08-29 18:17:48

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

完成方法如下:

创建一个虚拟表单并添加一个数字 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);
    }
}

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

The trick is to get the textbox embedded within the numeric updown control, and handle its Validating event.

Here is how to get it done:

Creat a dummy form and add a numeric updown control and some other controls, and when the numeric unpdown control looses the focus, the text of the form will be set to the value that the user entered.

Here it the code of what I have done:

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

EDIT:

@Carlos_Liu: Ok, I can see now the problem, you can achieve this with the TextChanged event, just save the value in a dummy variable and reuse it at txt_Validating, but be cautious, don't update this variable unless the textbox is focused.

Here is the new sample code:

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

EDIT#2

@Carlos_Liu: If you need the entered value to be kept, still there is a trick for doing so: @ the Validating event of the textbox, check the value, if it is not within the range, cancel loosing the focus!

Here is a new version of the code:

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