I want to retrieve the sub form values
I have a SplitContainer
, and in its right panel I have a Form
. I want to retrieve the values of the Text开发者_开发问答Boxes
of the form when I click on a button outside the Form, but inside the panel. How to do it?
May be you are having a UserControl in the Right panel of the SplitContainer.
In you that userControl class write a public method to get values.
public string GetValueOfTheTextBox()
{
return textBox.Text;
}
Add the userControl the SplitContainer.
MyUserControl myUserControl = new MyUserControl();
//Add this to the splitContainer right panel.
From out side of the MyUserControl class you can call GetValueOfTheTextBox method.
string text = myUserControl.GetValueOfTheTextBox();
You need to reference the other form. Let's say you have Form1 and Form2. Form2 has all of the text boxes on it.
Form1.cs - Button1_Click():
// Create an instance of Form2 (the form containing the textBox controls).
Form2 frm2 = new Form2();
// Make a call to the public property which will return the textBox's text.
textBox1.Text = frm2.TextBox1;
Form2.cs:
1.Make a textBox control and name it 'textBox1'.
2.Create a public property that will return an reference of textBox1.
public string TextBox1
{
get
{
return textBox1.Text;
}
}
So, what exactly are we doing here?
- From Form1.cs we are making a call to the Public property 'TextBox1' in Form2.cs.
- The Public property
TextBox1
in Form2.cs returns the text from the Form2.textBox1 control - which is the control you want the text of.
If Form2 is the form / user control inside the panel, create public properties to "get" the value of each textbox, and then refer to those properties in the parent form (Form1).
For instance, if Form2 has textboxes for first name and last name, create properties to get their value:
public string FirstName
{
get { return txtFirstName.Text; }
}
public string LastName
{
get { return txtLastName.Text; }
}
Then in Form1, assuming form2 is the instance of Form2 that you inserted into the panel, you can refer to those properties like this:
string firstName = form2.FirstName;
string lastName = form2.LastName;
精彩评论