2013-09-25 59 views
0

我从另一个childform(form1)打开一个childform(form2)并将MDI设置为父窗体。从childform传递值到另一个childform

这是我如何打开Form从MainForm的

的MainForm的childform
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(); 
    } 
} 

下面是窗口2从Form1中的MainForm的另一childform这也是MainForm的

的childform
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

会发生什么?错误,什么都没有? – sprinter252

+0

这是一个很奇怪的私人/公共命名约定你有 – Jonesopolis

+0

@sprinter - 值没有通过 –

回答

1

我认为主要的问题是您使用的是Button1的Click Form1,因为您发布为Button1两个事件处理程序中Form1事件处理程序但我试过这个,它的工作。把它放在事件处理程序为Button1单击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; 
} 
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; } 
    } 
} 
+0

中的buttonclick?form2.MdiParent = this;'必须是'form2.MdiParent = this。ParentForm;' –

+0

因此,'MainForm'是一个MdiParent,一个按钮在Mdi空格内打开'Form1','Form1'上的一个按钮在属于'MainForm'的相同Mdi空格中打开'Form2'? - 如果是这种情况,请参阅我的新答案。 –

0

当我通过从一种形式值以一个其它我通常创建一个参数对象

public class FormParams 
{ 
    public string Name {get; set;} 
} 

在形式将接收paramater

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; 
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; } 
    } 
} 
相关问题