Click here to Skip to main content
14,091,011 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
Hi How to change value of textbox by changing the value of NumericUpDown?
I need the textbox to take the value of NumericUpDown when I assing some value to NumericUpDown
Posted
Comments
   
Why?!!
   
What, trying to find out by yourself was too hard?
--SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Use the property TextBox.Text, it is a read/write property.

You need not do anything else if you "assign some value" to NumericUpDown. It won't work if the user changes the value of NumericUpDown; in this case, handle the event NumericUpDown.ValueChanged; in the body of the handler, assign the value of ValueChanged.Value.ToString() to TextBox.Text.

NumericUpDown myUpDown = new NumericUpDown();
TextBox myTextBox = new TextBox();

//...

myUpDown.ValueChanged += (sender, eventArgs) => {
    myTextBox.Text = myUpDown.Value.ToString();
}


—SA
   
v4
Comments
KORCARI 31-May-11 3:21am
   
TextBox.Text = numericUpDownAlpha.ValueChanged???????????
   
This is crazy! Think a bit. See my updated answer...
--SA
Tarun.K.S 31-May-11 3:39am
   
Yes lambda expressions, short and precise. My 5.
   
Thank you, Tarun.
--SA
Kim Togo 31-May-11 3:48am
   
Nice and clean SA. My 5.
   
Thank you, Kim.
--SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Hi,

What you need to do is attach an event-handler to the value-changed event of the NumericUpDown control, and use that event to set the text property of the text-box:

numericUpDown1.ValueChanged += delegate
{
    this.textBox1.Text = numericUpDown1.Value.ToString();
};
   
Comments
Tarun.K.S 31-May-11 3:39am
   
Yep that will help. 5+
Kim Togo 31-May-11 3:48am
   
Nice and clean. My 5.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

try this code
using System;
using System.Windows.Forms;

namespace WindowsFormsApplication12
{
    public partial class Form1 : Form
    {
    public Form1()
    {
        InitializeComponent();
    }

    private void numericUpDown1_ValueChanged(object sender, EventArgs e)
    {
        // You can change other program parts when the value changes.
        this.textBox1.Text = "Value changed to " + numericUpDown1.Value.ToString();
    }
    }
}
   
v2
Comments
Tarun.K.S 31-May-11 3:40am
   
You mean textbox1.Text?! I will correct it soon. 5+
ambarishtv 31-May-11 3:44am
   
thank you :)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

On the ValueChanged event of the NumericUpDown control, capture the value and display it in the textbox.
This example[^] describes this and should be sufficient for you to get started.
   
Comments
Tarun.K.S 31-May-11 3:41am
   
Good example. 5+
Abhinav S 31-May-11 4:16am
   
Thanks Tarun.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

This may help you...
numericUpDown1.ValueChanged += (sender,e) => textBox1.Text = numericUpDown1.Value.ToString();
   
Comments
Tarun.K.S 31-May-11 3:42am
   
Yep that will work. 5+
Kim Togo 31-May-11 3:46am
   
Nice and clean. My 5.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 6

Use the ValueChanged event handler and in that function, update your textbox accordingly.
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            numericUpDown1.ValueChanged += new EventHandler(numericUpDown1_ValueChanged);
        }
        void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            textBox1.Text = numericUpDown1.Value.ToString();
        }
    }
   
v2
Comments
   
Good that setting up event handler is shown (many think that numericUpDown1_ValueChanged speaks by itself), not good is that the above name violates (good) Microsoft naming convention. All auto-generated names should be renamed. Not using Designer for setting events even better. My 5, for now.
--SA
Tarun.K.S 31-May-11 3:47am
   
Ohkay next time I will make sure I won't use auto-generated names. Thanks for the pointer. :)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 7

private void button1_Click(object sender, EventArgs e)
{
    int a = 100;
    a++;

    txtBox1.Text = a.ToString();
}
   
Comments
Marcin Kozub 22-Mar-14 7:30am
   
What is this??
This question is 3 years old...
CHill60 22-Mar-14 9:52am
   
The question is 3 years old and already had 6 answers. To top it off you have not used a numeric up down control - which was in the OP. Answering old, resolved questions usually only attracts downvoting and loss of reputation... it is best avoided.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web02 | 2.8.190518.1 | Last Updated 22 Mar 2014
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100