Form1.cs: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms;namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { textBox1.Text = DateTime.Now.ToString(); //设置textBox1值 (new Form2(textBox1.Text)).Show();//调用非默认构造函数 } } } Form2.cs: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms;namespace WindowsFormsApplication1 { public partial class Form2 : Form { public Form2() : this("") { } /// <summary> /// 非默认构造函数 /// </summary> /// <param name="text"></param> public Form2(string text) { InitializeComponent(); label1.Text = text; } private void Form2_Load(object sender, EventArgs e) { } } }
1.使用构造函数Form1.cs: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms;namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { textBox1.Text = DateTime.Now.ToString(); //设置textBox1值 (new Form2(textBox1.Text)).Show();//调用非默认构造函数 } } } Form2.cs: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms;namespace WindowsFormsApplication1 { public partial class Form2 : Form { public Form2() : this("") { } /// <summary> /// 非默认构造函数 /// </summary> /// <param name="text"></param> public Form2(string text) { InitializeComponent(); label1.Text = text; } private void Form2_Load(object sender, EventArgs e) { } } }2.使用Show(IWin32Window owner)构造函数的缺点是传递是一次性的,你不能动态去改变,而下边的方法可以:Form1.cs: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms;namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { textBox1.Text = DateTime.Now.ToString();//设置textBox1值 (new Form2()).Show(this);//显示Form2 } public string BoxText { get { return textBox1.Text; } set { textBox1.Text = value; } } } } Form2.cs: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms;namespace WindowsFormsApplication1 { public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void Form2_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { //不能放在Form2_Load中执行,否则出现未将对象引用到对象实例 Form frm = this.Owner; if (frm is Form1) label1.Text = ((Form1)frm).BoxText; } } } 这种方法的缺点是不能在Form2一显示的时候获取并设置值,但两个方法可以放在一起使用-_-
using System;
using System.Collections.Generic;
using System.Text;namespace xiaopingguo
{
class change
{
private static string logevent;
public static string getevent
{
get
{
return logevent;
}
}
public static string setevent
{
set
{
logevent = value;
}
}
当主窗体的label1值变化时添加语句:change.setevent=label1.Text;
对话框调用时添加语句:textBox1.Text=change.getevent;
textBox1.Text=lable1.Text
Form2 form=new Form2(label.Text)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = DateTime.Now.ToString(); //设置textBox1值
(new Form2(textBox1.Text)).Show();//调用非默认构造函数
}
}
}
Form2.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
: this("")
{ } /// <summary>
/// 非默认构造函数
/// </summary>
/// <param name="text"></param>
public Form2(string text)
{
InitializeComponent();
label1.Text = text;
} private void Form2_Load(object sender, EventArgs e)
{ }
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = DateTime.Now.ToString(); //设置textBox1值
(new Form2(textBox1.Text)).Show();//调用非默认构造函数
}
}
}
Form2.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
: this("")
{ } /// <summary>
/// 非默认构造函数
/// </summary>
/// <param name="text"></param>
public Form2(string text)
{
InitializeComponent();
label1.Text = text;
} private void Form2_Load(object sender, EventArgs e)
{ }
}
}2.使用Show(IWin32Window owner)构造函数的缺点是传递是一次性的,你不能动态去改变,而下边的方法可以:Form1.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = DateTime.Now.ToString();//设置textBox1值
(new Form2()).Show(this);//显示Form2
} public string BoxText
{
get { return textBox1.Text; }
set { textBox1.Text = value; }
}
}
}
Form2.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
} private void Form2_Load(object sender, EventArgs e)
{ } private void button1_Click(object sender, EventArgs e)
{
//不能放在Form2_Load中执行,否则出现未将对象引用到对象实例
Form frm = this.Owner;
if (frm is Form1)
label1.Text = ((Form1)frm).BoxText;
}
}
}
这种方法的缺点是不能在Form2一显示的时候获取并设置值,但两个方法可以放在一起使用-_-
Form2 frm=new Form2;
frm.ShowDialog(mainFrm);
this.Textbox1.text= mainFrm.label1.text;//这是一种