我有两个Windows窗体(MyApp,Generator),我需要从MyApp调用Generator
Form gen = new Generator();
gen.Show();
string result = gen.IDontKnowWhatToDoHere();
Run Code Online (Sandbox Code Playgroud)
我的Generator.cs表单有三个TextBox和一个Button Ok,所以当用户在三个TextBox中键入一些文本时,单击Ok我想在这三个TextBox中输入Text.
你有什么想法我能做到这一点吗?
谢谢.
我通常使用这种模式:
TResult result = Generator.AskResult();
class Generator : Form
{
// set this result appropriately
private TResult Result { get; set; }
public static TResult AskResult()
{
var form = new Generator();
form.ShowDialog();
return form.Result; // or fetch a control's value directly
}
}
Run Code Online (Sandbox Code Playgroud)
这使它在单个语句中工作,类似于MessageBox,并且不需要公开任何控件.您还可以将任何其他数据作为参数包含在静态方法中,并将它们传递给表单的构造函数或相应地设置属性.
另一个好处包括,如果需要,可以重用表单的实例.
class Generator : Form
{
public string Text1 { get; private set; }
void ok_Click (object sender, EventArgs)
{
this.Text1 = txt1.Text;
...
this.Close();
}
}
Form gen = new Generator();
gen.ShowDialog();
string text1 = gen.Text1;
...
Run Code Online (Sandbox Code Playgroud)
class TextInfo
{
public string Text1 { get; set; }
...
}
class Generator : Form
{
public TextInfo Textinfo { get; private set; }
public Generator(TextInfo info)
{
this.TextInfo = info;
}
void ok_Click (object sender, EventArgs)
{
this.TextInfo.Text1 = txt1.Text;
...
this.Close();
}
}
TextInfo info = new TextInfo();
Form gen = new Generator(info);
gen.ShowDialog();
string text1 = info.Text1;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14171 次 |
| 最近记录: |