我从另一个子窗体(form1)打开一个子窗体(form2),并将MDI设置为父窗体。
以下是我如何从MainForm
打开form1作为MainForm的子窗体public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
form1 f1 = new form1();
f1.MdiParent = this;
f1.Show();
}
}
以下是form2作为MainForm的另一个子表单的方式,来自form1,它也是MainForm的子表单
public partial class form1 : Form
{
public form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
form2 f2 = new form2();
f2.MdiParent = this.ParentForm;
f2.Show();
}
}
我如何将值从form1传递给form2?我尝试了将值传递给模态形式但我没做的
Form1中
public partial class form1 : Form
{
public form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
form2 = new form2();
f2.MdiParent = this.ParentForm;
f2.name = textBox1.Text;
f2.Show();
}
}
窗口2
public partial class form2 : Form
{
public form2()
{
InitializeComponent();
}
private string NAME
public string name
{
get { return NAME; }
set { NAME = value; textBox1.Text = NAME; }
}
}
答案 0 :(得分:1)
我认为主要的问题是你用于按钮1点击Form1
的事件处理程序,因为你在Form1
中为button1发布了两个事件处理程序,但我尝试了这个并且它有效。将它放在按钮1的事件处理程序中,单击Form1
private void button1_Click(object sender, EventArgs e)
{
//Create a new Instance of Form2
Form2 f2 = new Form2();
//Sets the MDI Property
f2.MdiParent = this.ParentForm;
//Shows the Form
f2.Show();
//Open the already created instance
Form2 f = (Form2)Application.OpenForms["Form2"];
//Update the Property
f.name = textBox1.Text;
}
答案 1 :(得分:0)
这对我有用,你没有InitializeComponent
次调用的构造函数,而你的第二个代码发布没有分配给调用。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.MdiParent = this;
form2.tb = textBox1.Text;
form2.Show();
}
}
public partial class Form2 : Form
{
private string v;
public Form2()
{
InitializeComponent();
}
public string tb
{
get { return textBox1.Text; }
set { v = value; textBox1.Text = v; }
}
}
答案 2 :(得分:0)
当我将值从一个表单传递给另一个表单时,我通常会创建一个参数对象
public class FormParams
{
public string Name {get; set;}
}
以接收参数的形式
public class Form2
{
public FormParams Parameters {get; set;}
...
}
在进行通话的形式中,您可以执行类似
的操作FormParams frmParams = new FormParams();
frmParams.Name = "KarlX"
Form2 form = new Form2();
form.Parameters = frmParams;
form.MdiParent = this;
form.Show();
如果要使用传递给表单的值,请使用
txtStackOverFlowName.Text = Parameters.Name;
答案 3 :(得分:0)
您可以设置MdiParent
,也可以拨打MdiParent
public partial class MainForm : Form // IsMdiContainer = true in Properties
{
private void button1_Click(object sender, EventArgs e)
{
Form1 form1 = new Form1();
form1.MdiParent = this;
form1.Show();
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.MdiParent = this.MdiParent;
form2.tb = textBox1.Text;
form2.Show();
}
}
public partial class Form2 : Form
{
private string v;
public Form2()
{
InitializeComponent();
}
public string tb
{
get { return textBox1.Text; }
set { v = value; textBox1.Text = v; }
}
}