I want to take new values by populating a new Form. And i want to access those values of contro开发者_Go百科ls present in the New WinForm. And update values in Current WinForm ?
Can any one help me out of this problem.
Thanku!!!!!!!!!!
Change the Modifier property to "Public" or "Internal" (Friend in VB). Then you can access it as a public field.
As you get more Windows Forms experience you can look into using data binding and models behind the form so you don't have to access the controls directly but this is much more involved.
The simplest way (in my opinion) is to expose this through properties.
Example:
In Form2
public string MyTextBoxValue
{
get
{
return myTextBox.Text;
}
set
{
myTextBox.Text = value;
}
}
In Form1 (when displaying the other form):
Form2 form2 = new Form2();
form2.MyTextBoxValue = "whatever it should be";
form2.Show();
..and then, when you want to read the value from the other form:
string someVariable = form2.MyTextBoxValue;
This way Form1
does not need to be aware of which kind of control that is used (if any) to represent the value in Form2
, which means that this can change in Form2
without any need for changes in the code of Form1
. It also makes it possible to perform validation on the value before assigning it to the TextBox
control in Form2
, in case there might be restrictions on valid values.
you can assign the value of textbox to some static variable and then access that variable on the target form.
精彩评论