如何保持无效值在NumericUpDown它失去焦点后?焦点、NumericUpDown

2023-09-04 00:16:08 作者:小青春丶灬无限坑

在我的项目有一个用户控件,它包括一个的NumericUpDown CTRL,其有效值范围为从10到100

因此​​,如果用户输入200的NumericUpDown Ctrl键,然后将其值改为100后自动对焦更改为其他CTRL,它看起来有点好奇的客户,因为他们可能在的NumericUpDown点击输入200后确定按钮CTRL,他们需要告诉他们的输入不在范围内的值的信息框。

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

那么如何实现这一点?

萨迈赫Serag,这是code我已经测试。我有加表单上的按钮,但什么也没做。其结果对我来说是我输入200,然后点击按钮,只有具有价值100消息框显示后。当我输入200和preSS TAB键,它只会显示一个消息框,其值为200和的NumericUpDown文本值更改为100。因此,好奇的:-)反正非常感谢你的帮助!顺便说一句,.NET Framework版本2.0是带有SP2的我。

 公共部分类Form1中:形态
{
    私人TXT文本框;

    公共Form1中()
    {
        的InitializeComponent();

        的txt =(文本框)numericUpDown1.Controls [1];
        txt.Validating + =新CancelEventHandler(txt_Validating);
    }

    无效txt_Validating(对象发件人,CancelEventArgs E)
    {
        的MessageBox.show(txt.Text);
    }
}
 

解决方案

诀窍是让嵌入式数字上下按钮控件中的文本框,并处理其验证事件。

C NumericUpDown控件属性详解

下面是如何完成它:

创造一个虚拟的形式,并添加数字上下按钮控制和其他一些控制,而当数字unpdown控制失去焦点,窗体的文本将被设置为用户输入的值。

这是我做了code:

 公共部分类Form1中:形态
    {
        TXT文本框;
        公共Form1中()
        {
            的InitializeComponent();
            TXT =(文本框)numericUpDown1.Controls [1]; //注意,TextBox是第二控制NumericUpDown控件
            txt.Validating + =新CancelEventHandler(txt_Validating);
        }
        无效txt_Validating(对象发件人,CancelEventArgs E)
        {
            this.Text = txt.Text;
        }
    }
 

编辑:

@Carlos_Liu:好吧,我现在看到这个问题,你可以用TextChanged事件实现这一点,只需保存价值的虚拟变量,并在txt_Validating重新使用它,但要谨慎,不更新此变量,除非该文本的重点是

下面是新样本code:

 公共部分类Form1中:形态
{
    TXT文本框;
    串VAL;
    公共Form1中()
    {
        的InitializeComponent();
        TXT =(文本框)numericUpDown1.Controls [1]; //注意,TextBox是第二控制NumericUpDown控件
        txt.TextChanged + =新的EventHandler(txt_TextChanged);
        txt.Validating + =新CancelEventHandler(txt_Validating);
    }

    无效txt_TextChanged(对象发件人,EventArgs的)
    {
        如果(txt.Focused)//没有保存的价值,除非该文本的重点是,这是新把戏
            VAL = txt.Text;
    }
    无效txt_Validating(对象发件人,CancelEventArgs E)
    {
        的MessageBox.show(瓦尔:+ VAL);
    }
}
 

编辑#2

@Carlos_Liu:如果您需要输入的值必须保持,仍然有一个小窍门这样做:@文本框的Validating事件,检查值,如果不在此范围内,取消松动的焦点!

这里是code的新版本:

 公共部分类Form1中:形态
{
    TXT文本框;
    串VAL;
    公共Form1中()
    {
        的InitializeComponent();
        的txt =(文本框)numericUpDown1.Controls [1];
        txt.TextChanged + =新的EventHandler(txt_TextChanged);
        txt.Validating + =新CancelEventHandler(txt_Validating);
    }

    无效txt_TextChanged(对象发件人,EventArgs的)
    {
        如果(txt.Focused)
            VAL = txt.Text;
    }
    无效txt_Validating(对象发件人,CancelEventArgs E)
    {
        INT enteredVal = 0;
        int.TryParse(VAL,出enteredVal);
        如果(enteredVal> numericUpDown1.Maximum || enteredVal< numericUpDown1.Minimum)
        {
            txt.Text = VAL;
            e.Cancel =真;
        }
    }
}
 

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

解决方案

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

 
精彩推荐
图片推荐