Today in a contact management of C # design, with this problem, I need to upload the values from the TextBox in the parent form to the subform and query the database, and I used the new parent form (). TextBox.Text; To carry out the value transfer, but without eggs, after many experiments, A simpler solution was found:
A, subform calls a static variable of the parent form
Parent form: Logout
Child form: Affirm
Parent form text box: Tb_logout_username
public partial class Logout:form
{
//define a static variable that holds the value of the text box in the parent form public
static string tb_logoutname;
An event that instantiates a subform
private void Btt_logout_click (object sender, EventArgs e)
{
//Gets the value of the text box in the parent form
tb_ Logoutname = Tb_logout_username.text;
Affirm aff = new affirm ();
Aff. Show ();
}
Next you can call it in the subform directly: the parent form. Variable
namely: Logout.tb_logoutname
This method is more tricky, it should be found first to get the parent form method, and then operate on it.
Second, pass the parent form as a property to a subform
Define the parent form field for public in your subform, such as:
public class Affirm:form
{public
Logout mylogout;
}
You can then set its value in the parent form, such as:
public partial class Logout:form
{
//define a static variable that holds the value of the text box in the parent form public
static string tb_logoutname;
An event that instantiates a subform
private void Btt_logout_click (object sender, EventArgs e)
{
//Gets the value of the text box in the parent form
//tb_ Logoutname = Tb_logout_username.text;
Affirm aff = new affirm ();
Aff. Mylogout=this;
Aff. Show ();
}
In this way, the members that are exposed in the parent form can be accessed in the subform, using.