C#把一个窗体中文本框的值传到另一个窗体标签中
5个回答
展开全部
一下是本人的一点总结:说的不对还请指教。。。。呵呵(可能有点多慢慢看有帮助的!)
下面以FormMain 和FormChild为例
方法一:
使用owner属性
在FormMain 中
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
FormChild form2 = new FormChild();
form2.Owner = this;
form2.ShowDialog();
form2.Dispose();
}
}在FormChild中
public partial class FormChild: Form
{
FormMain form1;
public FormChild()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
form1 = (FormMain)this.Owner;
string s = form1.button1.Text; //注意这里的代码最好写在Load事件中,否则 FormChild form2 = new FormChild();
执行时将执行上面的两条语句将使owner属性为空null,此外还要将button1的private改一下
}
}
方法二
使用构造方法
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
FormChild form2 = new FormChild(this);
form2.ShowDialog();
form2.Dispose();
}
}
public partial class FormChild : Form
{
FormMain form1;
public FormChild(FormMain form1)
{
InitializeComponent();
this.form1 = form1;
}
private void Form2_Load(object sender, EventArgs e)
{
string s = form1.button1.Text;
}
}
注意:为了安全起见可以不改变button1的访问性而用属性
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
public Button Button1
{
get
{
return button1;
}
}
private void button1_Click(object sender, EventArgs e)
{
FormChild form2 = new FormChild(this);
form2.ShowDialog();
form2.Dispose();
}
}
public partial class FormChild : Form
{
FormMain form1;
public FormChild(FormMain form1)
{
InitializeComponent();
this.form1 = form1;
}
private void Form2_Load(object sender, EventArgs e)
{
string s = form1.Button1.Text;
}
}
方法三
FormMain 中直接利用show()方法和showDialog()方法将owner传递过去
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
FormChild form2 = new FormChild();
form2.ShowDialog(this);
form2.Dispose();
}
}
在FormChild 中
public partial class FormChild : Form
{
public FormChild()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
FormMain form1;
form1 = (FormMain)this.Owner;
string s = form1.button1.Text;//再次提醒这里form_load中的代码不能在初始化时候写
}
}
方法四
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
public Button Button1
{
get
{
return button1;
}
}
private void button1_Click(object sender, EventArgs e)
{
FormChild form2 = new FormChild();
form2.ShowDialog();
form2.Dispose();
}
}
public partial class FormChild : Form
{
public FormChild()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
FormMain form1=new FormMain ();
string s = form1.Button1.Text;
}
}
//方法四有个缺点那就是如果你给前面的属性赋值当子窗口关闭的时候赋的值就没有了
除非是给静态的字段赋值(建议不要用这种方法)
其他:
为避免方法一前面说的子窗口中的有些代码必须写在load中可以这样处理
在FormMain 中
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
FormChild form2 = new FormChild();
form2.Owner = this;
form2.ShowDialog();
form2.Dispose();
}
}在FormChild中
public partial class FormChild: Form
{
FormMain form1;
public FormChild()
{
InitializeComponent();
}
public FormChild(FormMain form1)//构造方法的重载
{
this.form1=form1;
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
form1 = (FormMain)this.Owner;
string s = form1.button1.Text; //注意这里的代码最好写在Load事件中,否则 FormChild form2 = new FormChild();
执行时将执行上面的两条语句将使owner属性为空null,此外还要将button1的private改一下
}
}
下面以FormMain 和FormChild为例
方法一:
使用owner属性
在FormMain 中
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
FormChild form2 = new FormChild();
form2.Owner = this;
form2.ShowDialog();
form2.Dispose();
}
}在FormChild中
public partial class FormChild: Form
{
FormMain form1;
public FormChild()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
form1 = (FormMain)this.Owner;
string s = form1.button1.Text; //注意这里的代码最好写在Load事件中,否则 FormChild form2 = new FormChild();
执行时将执行上面的两条语句将使owner属性为空null,此外还要将button1的private改一下
}
}
方法二
使用构造方法
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
FormChild form2 = new FormChild(this);
form2.ShowDialog();
form2.Dispose();
}
}
public partial class FormChild : Form
{
FormMain form1;
public FormChild(FormMain form1)
{
InitializeComponent();
this.form1 = form1;
}
private void Form2_Load(object sender, EventArgs e)
{
string s = form1.button1.Text;
}
}
注意:为了安全起见可以不改变button1的访问性而用属性
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
public Button Button1
{
get
{
return button1;
}
}
private void button1_Click(object sender, EventArgs e)
{
FormChild form2 = new FormChild(this);
form2.ShowDialog();
form2.Dispose();
}
}
public partial class FormChild : Form
{
FormMain form1;
public FormChild(FormMain form1)
{
InitializeComponent();
this.form1 = form1;
}
private void Form2_Load(object sender, EventArgs e)
{
string s = form1.Button1.Text;
}
}
方法三
FormMain 中直接利用show()方法和showDialog()方法将owner传递过去
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
FormChild form2 = new FormChild();
form2.ShowDialog(this);
form2.Dispose();
}
}
在FormChild 中
public partial class FormChild : Form
{
public FormChild()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
FormMain form1;
form1 = (FormMain)this.Owner;
string s = form1.button1.Text;//再次提醒这里form_load中的代码不能在初始化时候写
}
}
方法四
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
public Button Button1
{
get
{
return button1;
}
}
private void button1_Click(object sender, EventArgs e)
{
FormChild form2 = new FormChild();
form2.ShowDialog();
form2.Dispose();
}
}
public partial class FormChild : Form
{
public FormChild()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
FormMain form1=new FormMain ();
string s = form1.Button1.Text;
}
}
//方法四有个缺点那就是如果你给前面的属性赋值当子窗口关闭的时候赋的值就没有了
除非是给静态的字段赋值(建议不要用这种方法)
其他:
为避免方法一前面说的子窗口中的有些代码必须写在load中可以这样处理
在FormMain 中
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
FormChild form2 = new FormChild();
form2.Owner = this;
form2.ShowDialog();
form2.Dispose();
}
}在FormChild中
public partial class FormChild: Form
{
FormMain form1;
public FormChild()
{
InitializeComponent();
}
public FormChild(FormMain form1)//构造方法的重载
{
this.form1=form1;
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
form1 = (FormMain)this.Owner;
string s = form1.button1.Text; //注意这里的代码最好写在Load事件中,否则 FormChild form2 = new FormChild();
执行时将执行上面的两条语句将使owner属性为空null,此外还要将button1的private改一下
}
}
展开全部
一下是本人的一点总结:说的不对还请指教。。。。呵呵(可能有点多慢慢看有帮助的!)
下面以FormMain
和FormChild为例
方法一:
使用owner属性
在FormMain
中
public
partial
class
FormMain
:
Form
{
public
FormMain()
{
InitializeComponent();
}
private
void
button1_Click(object
sender,
EventArgs
e)
{
FormChild
form2
=
new
FormChild();
form2.Owner
=
this;
form2.ShowDialog();
form2.Dispose();
}
}在FormChild中
public
partial
class
FormChild:
Form
{
FormMain
form1;
public
FormChild()
{
InitializeComponent();
}
private
void
Form2_Load(object
sender,
EventArgs
e)
{
form1
=
(FormMain)this.Owner;
string
s
=
form1.button1.Text;
//注意这里的代码最好写在Load事件中,否则
FormChild
form2
=
new
FormChild();
执行时将执行上面的两条语句将使owner属性为空null,此外还要将button1的private改一下
}
}
方法二
使用构造方法
public
partial
class
FormMain
:
Form
{
public
FormMain()
{
InitializeComponent();
}
private
void
button1_Click(object
sender,
EventArgs
e)
{
FormChild
form2
=
new
FormChild(this);
form2.ShowDialog();
form2.Dispose();
}
}
public
partial
class
FormChild
:
Form
{
FormMain
form1;
public
FormChild(FormMain
form1)
{
InitializeComponent();
this.form1
=
form1;
}
private
void
Form2_Load(object
sender,
EventArgs
e)
{
string
s
=
form1.button1.Text;
}
}
注意:为了安全起见可以不改变button1的访问性而用属性
public
partial
class
FormMain
:
Form
{
public
FormMain()
{
InitializeComponent();
}
public
Button
Button1
{
get
{
return
button1;
}
}
private
void
button1_Click(object
sender,
EventArgs
e)
{
FormChild
form2
=
new
FormChild(this);
form2.ShowDialog();
form2.Dispose();
}
}
public
partial
class
FormChild
:
Form
{
FormMain
form1;
public
FormChild(FormMain
form1)
{
InitializeComponent();
this.form1
=
form1;
}
private
void
Form2_Load(object
sender,
EventArgs
e)
{
string
s
=
form1.Button1.Text;
}
}
方法三
FormMain
中直接利用show()方法和showDialog()方法将owner传递过去
public
partial
class
FormMain
:
Form
{
public
FormMain()
{
InitializeComponent();
}
private
void
button1_Click(object
sender,
EventArgs
e)
{
FormChild
form2
=
new
FormChild();
form2.ShowDialog(this);
form2.Dispose();
}
}
在FormChild
中
public
partial
class
FormChild
:
Form
{
public
FormChild()
{
InitializeComponent();
}
private
void
Form2_Load(object
sender,
EventArgs
e)
{
FormMain
form1;
form1
=
(FormMain)this.Owner;
string
s
=
form1.button1.Text;//再次提醒这里form_load中的代码不能在初始化时候写
}
}
方法四
public
partial
class
FormMain
:
Form
{
public
FormMain()
{
InitializeComponent();
}
public
Button
Button1
{
get
{
return
button1;
}
}
private
void
button1_Click(object
sender,
EventArgs
e)
{
FormChild
form2
=
new
FormChild();
form2.ShowDialog();
form2.Dispose();
}
}
public
partial
class
FormChild
:
Form
{
public
FormChild()
{
InitializeComponent();
}
private
void
Form2_Load(object
sender,
EventArgs
e)
{
FormMain
form1=new
FormMain
();
string
s
=
form1.Button1.Text;
}
}
//方法四有个缺点那就是如果你给前面的属性赋值当子窗口关闭的时候赋的值就没有了
除非是给静态的字段赋值(建议不要用这种方法)
其他:
为避免方法一前面说的子窗口中的有些代码必须写在load中可以这样处理
在FormMain
中
public
partial
class
FormMain
:
Form
{
public
FormMain()
{
InitializeComponent();
}
private
void
button1_Click(object
sender,
EventArgs
e)
{
FormChild
form2
=
new
FormChild();
form2.Owner
=
this;
form2.ShowDialog();
form2.Dispose();
}
}在FormChild中
public
partial
class
FormChild:
Form
{
FormMain
form1;
public
FormChild()
{
InitializeComponent();
}
public
FormChild(FormMain
form1)//构造方法的重载
{
this.form1=form1;
InitializeComponent();
}
private
void
Form2_Load(object
sender,
EventArgs
e)
{
form1
=
(FormMain)this.Owner;
string
s
=
form1.button1.Text;
//注意这里的代码最好写在Load事件中,否则
FormChild
form2
=
new
FormChild();
执行时将执行上面的两条语句将使owner属性为空null,此外还要将button1的private改一下
}
}
下面以FormMain
和FormChild为例
方法一:
使用owner属性
在FormMain
中
public
partial
class
FormMain
:
Form
{
public
FormMain()
{
InitializeComponent();
}
private
void
button1_Click(object
sender,
EventArgs
e)
{
FormChild
form2
=
new
FormChild();
form2.Owner
=
this;
form2.ShowDialog();
form2.Dispose();
}
}在FormChild中
public
partial
class
FormChild:
Form
{
FormMain
form1;
public
FormChild()
{
InitializeComponent();
}
private
void
Form2_Load(object
sender,
EventArgs
e)
{
form1
=
(FormMain)this.Owner;
string
s
=
form1.button1.Text;
//注意这里的代码最好写在Load事件中,否则
FormChild
form2
=
new
FormChild();
执行时将执行上面的两条语句将使owner属性为空null,此外还要将button1的private改一下
}
}
方法二
使用构造方法
public
partial
class
FormMain
:
Form
{
public
FormMain()
{
InitializeComponent();
}
private
void
button1_Click(object
sender,
EventArgs
e)
{
FormChild
form2
=
new
FormChild(this);
form2.ShowDialog();
form2.Dispose();
}
}
public
partial
class
FormChild
:
Form
{
FormMain
form1;
public
FormChild(FormMain
form1)
{
InitializeComponent();
this.form1
=
form1;
}
private
void
Form2_Load(object
sender,
EventArgs
e)
{
string
s
=
form1.button1.Text;
}
}
注意:为了安全起见可以不改变button1的访问性而用属性
public
partial
class
FormMain
:
Form
{
public
FormMain()
{
InitializeComponent();
}
public
Button
Button1
{
get
{
return
button1;
}
}
private
void
button1_Click(object
sender,
EventArgs
e)
{
FormChild
form2
=
new
FormChild(this);
form2.ShowDialog();
form2.Dispose();
}
}
public
partial
class
FormChild
:
Form
{
FormMain
form1;
public
FormChild(FormMain
form1)
{
InitializeComponent();
this.form1
=
form1;
}
private
void
Form2_Load(object
sender,
EventArgs
e)
{
string
s
=
form1.Button1.Text;
}
}
方法三
FormMain
中直接利用show()方法和showDialog()方法将owner传递过去
public
partial
class
FormMain
:
Form
{
public
FormMain()
{
InitializeComponent();
}
private
void
button1_Click(object
sender,
EventArgs
e)
{
FormChild
form2
=
new
FormChild();
form2.ShowDialog(this);
form2.Dispose();
}
}
在FormChild
中
public
partial
class
FormChild
:
Form
{
public
FormChild()
{
InitializeComponent();
}
private
void
Form2_Load(object
sender,
EventArgs
e)
{
FormMain
form1;
form1
=
(FormMain)this.Owner;
string
s
=
form1.button1.Text;//再次提醒这里form_load中的代码不能在初始化时候写
}
}
方法四
public
partial
class
FormMain
:
Form
{
public
FormMain()
{
InitializeComponent();
}
public
Button
Button1
{
get
{
return
button1;
}
}
private
void
button1_Click(object
sender,
EventArgs
e)
{
FormChild
form2
=
new
FormChild();
form2.ShowDialog();
form2.Dispose();
}
}
public
partial
class
FormChild
:
Form
{
public
FormChild()
{
InitializeComponent();
}
private
void
Form2_Load(object
sender,
EventArgs
e)
{
FormMain
form1=new
FormMain
();
string
s
=
form1.Button1.Text;
}
}
//方法四有个缺点那就是如果你给前面的属性赋值当子窗口关闭的时候赋的值就没有了
除非是给静态的字段赋值(建议不要用这种方法)
其他:
为避免方法一前面说的子窗口中的有些代码必须写在load中可以这样处理
在FormMain
中
public
partial
class
FormMain
:
Form
{
public
FormMain()
{
InitializeComponent();
}
private
void
button1_Click(object
sender,
EventArgs
e)
{
FormChild
form2
=
new
FormChild();
form2.Owner
=
this;
form2.ShowDialog();
form2.Dispose();
}
}在FormChild中
public
partial
class
FormChild:
Form
{
FormMain
form1;
public
FormChild()
{
InitializeComponent();
}
public
FormChild(FormMain
form1)//构造方法的重载
{
this.form1=form1;
InitializeComponent();
}
private
void
Form2_Load(object
sender,
EventArgs
e)
{
form1
=
(FormMain)this.Owner;
string
s
=
form1.button1.Text;
//注意这里的代码最好写在Load事件中,否则
FormChild
form2
=
new
FormChild();
执行时将执行上面的两条语句将使owner属性为空null,此外还要将button1的private改一下
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
写一个类,里面一个变量存textbox中的内容,在另一个窗体中调用这个类,得到这个变量,给label赋值。!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
label1.text=textbox1.text
写在某一事件里
写在某一事件里
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用session传值!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |