Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
498 views
in Technique[技术] by (71.8m points)

c# - How to hold the invalid value for NumericUpDown after it loses focus?

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);
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...