You will have to be extra careful when you need to display the value, entered by you in the ‘Text Box’ to a ‘Label” when you press a ‘Button’. This happens when you are working upon the form and want to display or add some text or numeric digit right upon the same form.
This is common practice in the Windows Form when you are working upon the Windows Form Application environment. If you want to display some text or the numeric digit upon any part of the form, what you need is to use command buttons:
- Text Box
For the procedure, follow the tutorial:
1: Add ‘Text Box, Button and Label from toolbox upon the form1.
2: Before start working upon the button, press the button, drag it upon upon the form and the rename the button from the ‘Properties’, placed at the bottom right corner. I named the button as ‘Get’.
3: We need the label to show the values from textbox. So, we don’t need it to display static values. For this purpose, remove the name of label from the ‘Properties’ window.
4: Now, create click event upon the button by double clicking on the button. Alternatively, you can look for ‘Click’ option under ‘Events’ in ‘Properties’ window and double click to create a click event.
5: The code behind file of the form will open and the ‘button_click’ would be generated automatically.
5: Here you have to add the following code for the whole process to work.
- Name of the label i.e. ‘Label1.Text’
- Insert assignment operator i.e. ‘=’ after ‘Label1.Text’
- Now, after the assignment operator, add ‘Textbox1.Text’
Note: Don’t forget to add semi-colon i.e. ‘;’ after the ‘Textbox1.Text’ or your application will throw errors.
- The whole command will look like this:
Your operation should work now. But, I have added a pop up message box to show success when the value of textbox is showed on label.
- You can simply add ‘MessageBox.Show(“Success”);’ where the Success is the string that would be showed on the message box.
Note: Note: Don’t forget to add semi-colon i.e. ‘;’ after the ‘Textbox1.Text’ or your application will throw errors.
- Start the application by pressing ‘Start’ button and your whole application will run. You will be able to add value in textbox and when you press the button, the entered value will be showed in label.
- That’s all!
You may visit our Windows Form Development Tips for useful tips, tricks and instructions.