c# 如何从一个窗体的TextBox访问另一个窗体的TextBox?

xingtaineiqiu 2009-10-11 06:56:25
比如有一个窗体Form1,里面有个文本框TextBox1;另外一个窗体Form2,里面有TextBox2;如何把Form1中的TextBox1的内容赋给Form2中的TextBox2?
...全文
1178 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
threenewbee 2009-10-12
  • 打赏
  • 举报
回复
窗体传递值应该单向传递。避免代码耦合。

参照 FileOpenDialog 的做法。

可以给form增加一个属性。当窗体确定关闭的时候,把值传给这个属性。

然后另一个窗体读取这个属性。
zhu4139365 2009-10-12
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 xray2005 的回复:]
把你要访问的TextBox设置属性
如:
public string Text1
{
get{return this.textBox1.Text;}
set{ this.textBox1.Text=value;}
}


再访问属性就是了
[/Quote]
snfeng 2009-10-12
  • 打赏
  • 举报
回复
学习
magician0089 2009-10-12
  • 打赏
  • 举报
回复
你可以在窗体构造函数InitializeComponent方法中把声明的private textbox1 改成 public 就可以直接调用了
sz_xd 2009-10-12
  • 打赏
  • 举报
回复
實用,我頂!
ginni215 2009-10-12
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 sohighthesky 的回复:]
定义public Form2(string str) {
InitlizeCompolent();
this.TextBox2.Text=str;
}
form1中

Form2 f2=new Form2(text1.text);
f2.show()
[/Quote]

推荐~~
peterb 2009-10-12
  • 打赏
  • 举报
回复
龙宜坡 2009-10-12
  • 打赏
  • 举报
回复
1.静态变量
2.传参
3.委托
ykspj88 2009-10-12
  • 打赏
  • 举报
回复
还可以在主界面(就是你一直打开的那个界面,假设是FormMain里放个变量)
string savetext=null;

在Form1中令 _main.savetext=TextBox1.Text;
在Form2中令 TextBox2.Text=_main.savetext;

bob_china 2009-10-12
  • 打赏
  • 举报
回复

public static class HandlerManager
{
public static delegate TextChangedHandler(string value);

public static event TextChanged;

public static void TextChange(string value)
{
if(TextChanged != null)
{
TextChanged(value);
}
}
}

//Form2 加载事件监听

HandlerManager.TextChanged += delegate(string value)
{
this.TextBox2.Text=value;
};

//Form1 触发事件
HandlerManager.TextChange("文本框值改变了,你响应一下!");


N_ccw 2009-10-11
  • 打赏
  • 举报
回复
GET
SET
用属性访问
狼王_ 2009-10-11
  • 打赏
  • 举报
回复
学习
name_hq 2009-10-11
  • 打赏
  • 举报
回复
两种方法:
1:直接将控件的Modifiers属性设置成Public,form2.TextBox2.Text = form1.TextBox1.Text;
2: 自己定义方法,让外部访问
public string GetText
{
return this.textBox1.Text;
}

form2.TextBox2.Text = form1.GetText();
推荐用第2种方法
sohighthesky 2009-10-11
  • 打赏
  • 举报
回复 3
定义public Form2(string str) {
InitlizeCompolent();
this.TextBox2.Text=str;
}
form1中

Form2 f2=new Form2(text1.text);
f2.show()
xray2005 2009-10-11
  • 打赏
  • 举报
回复
把你要访问的TextBox设置属性
如:
public string Text1
{
get{return this.textBox1.Text;}
set{ this.textBox1.Text=value;}
}

再访问属性就是了

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧