技术开发 频道

三种方法实现多窗体内容显示

 【IT168技术文档】代码说明:共有两个窗体,为From1与From2,窗体一中有一个checkelistbox里面有若干个电话号码,另有一Button按纽,然后在窗体二中有一个lable,我想在运行后在窗体一选取一个号码后,点按纽,然后会弹出窗体二,并在窗体二的lable上显示出所选取的号码。

 方法1、

 C# code

 Panel saleSlip = new Panel();

 //获取模板Panel

 foreach (Control item in newSalesSlip.Controls)

 {

 if (item.GetType().ToString() == "System.Windows.Forms.Panel")

 {

 saleSlip = (Panel)item;

 break;

 }

 }

 Form tempForm = new Form();

 tempForm.Controls.Add(saleSlip);

 方法2

 Form1中:

 private void button1_Click(object sender, EventArgs e)

 {

 Form2 f=new Form2();

 string text=null;

 if (this.listBox1.SelectedIndex!=-1)

 {

 text=this.listBox1.Items[this.listBox1.SelectedIndex].ToString();

 }

 f.LabelText=text;

 f.ShowDialog(this);

 }

 Form2中:

 public string LabelText

 {

 get{return this.label1.Text;}

 set {this.label1.Text=value;}

 }

 方法三、
    还有个简单的办法,利用构建函数传值:

 string 电话号码;

 if (form2 == null)

 {

 form2  = new Form2(电话号码);

 }

 在Form2中定义一个Form2(string)的构建函数
 

0
相关文章