我想在飞行中创建第二个表单&将主窗体中的变量显示为第二个窗体上的标签。请问我该怎么做?感谢
答案 0 :(得分:2)
我觉得这里有几个问题。
如何打开新表单:在Form1
的某种方法中,添加以下代码:
Form2 form2 = new Form2();
form2.ShowDialog();
如何将值从Form1
传递到Form2
:在调用Form2
构造函数时传递它们:
Form2 form2 = new Form2(String value, Boolean isDone);
Form2
构造函数应如下所示:
public Form2(String form1Value, Boolean form1IsDone) {
label1.Text = form1Value;
label2.Text = form1IsDone;
}
答案 1 :(得分:1)
你可以这样做:
var form = new Form
{
Owner = this
};
var label = new Label
{
Text = "Hello"
};
form.Controls.Add(label);
form.Show();
当然,您需要为布局做更多的工作,但这仅仅是“如何进行”的示例。
答案 2 :(得分:1)
Form newForm = new Form();
TextBox tb = new TextBox();
tb.ReadOnly = true;
tb.Multiline = true;
tb.Dock = DockStyle.Fill;
tb.Text = "One and" + Environment.NewLine + "two lines of text";
newForm.Controls.Add(tb);
newForm.ShowDialog();
将TextBox
替换为Label
或您喜欢的任何内容。
但是,如果您不需要每次在Visual Studio设计器中创建新表单并打开该表单时动态生成新表单的布局,请将内容属性传递给该表单。
Form2 form2 = new Form2();
form2.SetContent("One and" + Environment.NewLine + "two lines of text");
form2.ShowDialog();
其中SetContent
是您在Form2
类中创建的公共方法。 SetContent
方法应该标记相应标签的文本等。