Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i have code for a form and textbox and one button now i want to take inputs from that text box to a variable of my base form
Posted
Comments
phil.o 15-Sep-15 12:57pm    
And? What have you tried? Where are you stuck?
Please improve your question and show us the relevant part of your code that is causing you troubles.

1 solution

When you add a textbox via the VS designer, it creates a variable of the same name to "point" to the textbox - the same thing applies to a button.
If you want to create the textbox and button manually via your code, then you need to do something equivalent in your code to "track" the controls.
It's easy to do, if it's only one textbox and one button you are creating:
C#
private TextBox myTextBox = new TextBox();
private myButton = new Button();
...
   Controls.Add(myTextBox);
   Controls.Add(myButton);
It gets more complex if you are adding a number of them as pairs and want the button to reference it's matching Tetxbox, but even then the framework makes it easy:
C#
   TextBox tb = new TextBox();
   b = new Button();
...
   b.Tag = tb;
   Controls.Add(tb);
   Controls.Add(b);
...
private void MyButton_Click(object sender, EventArgs e)
   {
   Button b = sender as Button;
   if (b != null)
      {
      TextBox tb = b.Tag as TextBox;
      if (tb != null)
         {
         myStringVariable = tb.Text;
         ...
         }
      }
   }
 
Share this answer
 

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