Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 636243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:29:38+00:00 2026-05-13T20:29:38+00:00

In my project there is an UserControl which includes a NumericUpDown ctrl, and its

  • 0

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);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T20:29:38+00:00Added an answer on May 13, 2026 at 8:29 pm

    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;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 341k
  • Answers 342k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If I do understand your problem, you want to reattach… May 14, 2026 at 5:05 am
  • Editorial Team
    Editorial Team added an answer You can use the Count() extension method - but it… May 14, 2026 at 5:05 am
  • Editorial Team
    Editorial Team added an answer Unfortunately the Wizard control was added in ASP.Net 2.0. You… May 14, 2026 at 5:05 am

Related Questions

I've got two projects, one is a control library and another is my main
After doing a project with WPF and getting very much attached to it's excellent
Summary Hi All, OK, further into my adventures with custom controls... In summary, here
I'm onto a real head scratcher here ... and it appears to be one
I have a little problem with the creation of a user control. Though I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.