Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
2.25/5 (4 votes)
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
Sergey Alexandrovich Kryukov 31-May-11 3:18am    
Why?!!
Sergey Alexandrovich Kryukov 31-May-11 3:20am    
What, trying to find out by yourself was too hard?
--SA

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.

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

//...

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


—SA
 
Share this answer
 
v4
Comments
KORCARI 31-May-11 3:21am    
TextBox.Text = numericUpDownAlpha.ValueChanged???????????
Sergey Alexandrovich Kryukov 31-May-11 3:25am    
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.
Sergey Alexandrovich Kryukov 31-May-11 3:42am    
Thank you, Tarun.
--SA
Kim Togo 31-May-11 3:48am    
Nice and clean SA. My 5.
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:

C#
numericUpDown1.ValueChanged += delegate
{
    this.textBox1.Text = numericUpDown1.Value.ToString();
};
 
Share this answer
 
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.
try this code
C#
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();
    }
    }
}
 
Share this answer
 
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 :)
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.
 
Share this answer
 
Comments
Tarun.K.S 31-May-11 3:41am    
Good example. 5+
Abhinav S 31-May-11 4:16am    
Thanks Tarun.
This may help you...
numericUpDown1.ValueChanged += (sender,e) => textBox1.Text = numericUpDown1.Value.ToString();
 
Share this answer
 
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.
Use the ValueChanged event handler and in that function, update your textbox accordingly.
C#
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();
        }
    }
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 31-May-11 3:45am    
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. :)
C#
private void button1_Click(object sender, EventArgs e)
{
    int a = 100;
    a++;

    txtBox1.Text = a.ToString();
}
 
Share this answer
 
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900