がSplitContainer
あり、その右側のパネルに がありForm
ます。TextBoxes
フォームの外側でパネルの内側にあるボタンをクリックしたときに、フォームの値を取得したいと考えています。どうやってするの?
1226 次
3 に答える
3
SplitContainer の右側のパネルに UserControl がある可能性があります。
そのuserControlクラスで、値を取得するパブリックメソッドを記述します。
public string GetValueOfTheTextBox()
{
return textBox.Text;
}
userControl を SplitContainer に追加します。
MyUserControl myUserControl = new MyUserControl();
//Add this to the splitContainer right panel.
MyUserControl クラスの外側から GetValueOfTheTextBox メソッドを呼び出すことができます。
string text = myUserControl.GetValueOfTheTextBox();
于 2011-09-27T12:44:13.123 に答える
2
他のフォームを参照する必要があります。Form1 と Form2 があるとします。Form2 にはすべてのテキスト ボックスがあります。
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.textBox コントロールを作成し、'textBox1' という名前を付けます。
2.textBox1 の参照を返すパブリック プロパティを作成します。
public string TextBox1
{
get
{
return textBox1.Text;
}
}
それで、私たちはここで何をしているのでしょうか?
- Form1.csから、 Form2.csの Public プロパティ 'TextBox1' を呼び出しています。
- Form2.csのPublicプロパティ
TextBox1
は、テキストが必要なコントロールであるForm2.textBox1コントロールからテキストを返します。
于 2011-09-27T12:33:05.043 に答える
1
Form2がパネル内のフォーム/ユーザーコントロールである場合は、パブリックプロパティを作成して各テキストボックスの値を「取得」し、親フォーム(Form1)でそれらのプロパティを参照します。
たとえば、Form2に名前と名前のテキストボックスがある場合は、プロパティを作成してそれらの値を取得します。
public string FirstName
{
get { return txtFirstName.Text; }
}
public string LastName
{
get { return txtLastName.Text; }
}
次に、Form1で、form2がパネルに挿入したForm2のインスタンスであると仮定すると、次のようなプロパティを参照できます。
string firstName = form2.FirstName;
string lastName = form2.LastName;
于 2011-09-27T12:41:29.347 に答える