窗口传值问题。。

the0c 2008-05-28 11:54:46
做了个登陆界面,Form1为login ,Form2为主界面。。想把Form1 的 textBox 值传给Form2 的textBox 值(用全局变量) 但是效果并不如自己所想。。。Form2得不到值。万望指点!!!!

附主要源码:
Program.cs:

static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 f1 = new Form1();
if (f1.ShowDialog() == DialogResult.OK)
{

Form2 f2 = new Form2();
f2._myText = f1.myText;
f1.Close();
Application.Run(new Form2());//打开主窗口
}

Form1.cs :

public string myText;
private void button1_Click(object sender, EventArgs e)
{
在 button1_Click()中判断textBox1.Text是否等密码,如相等则:
myText = this.textBox1.Text;
this.DialogResult = DialogResult.OK; }

Form2.cs :
public string _myText;
private void Form2_Load(object sender, EventArgs e)
{

this.textBox1.Text = _myText;
}
...全文
105 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaoyu19039 2008-05-28
  • 打赏
  • 举报
回复
form1是启动页面的话,直接用构造函数传

form2是启动页面的话在 form2的Activated事件里赋值
mansea_nee 2008-05-28
  • 打赏
  • 举报
回复
如果感觉不行,用委托,自我感觉不错!
贫僧又回来了 2008-05-28
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 lovefootball 的回复:]
Form2 f2 = new Form2();
Application.Run(new Form2());

根本就不是一个实例~~~~~

Application.Run(f2);
[/Quote]
你都已经实例了,Application.Run(f2);
the0c 2008-05-28
  • 打赏
  • 举报
回复
。。。。。。。。。。。。。兄弟,,我弄了半天没搞出来,你就这么一句话,,差距啊!!
Tll_W 2008-05-28
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 lovefootball 的回复:]
Form2 f2 = new Form2();
Application.Run(new Form2());

根本就不是一个实例~~~~~

Application.Run(f2);
[/Quote]

这是一个问题啊。。。
lovefootball 2008-05-28
  • 打赏
  • 举报
回复
Form2 f2 = new Form2();
Application.Run(new Form2());

根本就不是一个实例~~~~~

Application.Run(f2);
xiaoqiuwyh 2008-05-28
  • 打赏
  • 举报
回复
根据你所描述的情况,只需要从Form1往Form2里进行传值,那么直接在生成这个Form2的新实例时传这个参数,然后在Form2里的构造函数里直接接收就可以了.
Form1 cs:
Form2 f2 = new Form2(myText)
f2.ShowDialog();

Form2 cs:
stirng _myText;
public Form2(string myText)
{
this._myText = myText;
}
这里肯定没有问题.
wang2034 2008-05-28
  • 打赏
  • 举报
回复
你在 Program.cs 类中,加一个静态变量,这样Form1和Form2就都可以访问了
c863123 2008-05-28
  • 打赏
  • 举报
回复
Form2 f2 = new Form2();
f2._myText = f1.myText;
f1.Close();
f2.showdialog();//打开主窗口
virusswb 2008-05-28
  • 打赏
  • 举报
回复
我有一个系统教程,你可以看看,就是窗体传值的,很有帮助的

窗体传值,子窗体,父窗体,反射,reflection,windows,组策略,gpedit.msc,动态创建窗体,谢谢,系列<三>
http://www.cnblogs.com/virusswb/archive/2008/05/26/1207989.html
子窗体,主窗体,互相传值,组策略,子窗体文本选择,select,length,反射系列<二>
http://www.cnblogs.com/virusswb/archive/2008/05/27/1208109.html
窗体传值,子窗体,父窗体,反射,reflection,windows,组策略,gpedit.msc,动态创建窗体,谢谢
http://www.cnblogs.com/virusswb/archive/2008/05/26/1207989.html
baihe_591 2008-05-28
  • 打赏
  • 举报
回复
Form2中:
this.textBox1.Text=((TextBox)Application.OpenForms["Form1"].Controls["textBox1"]).Text;
SuperTyro 2008-05-28
  • 打赏
  • 举报
回复
Form2 f2 = new Form2();
f2._myText = f1.myText;
f1.Close();
Application.Run(new Form2());//打开主窗口

你这里打开错了

应该是 Application.Run(f2);//打开主窗口
yata_soft 2008-05-28
  • 打赏
  • 举报
回复


我写过一篇页面传值的文章,希望能帮到你
http://blog.csdn.net/yata_soft/archive/2008/03/11/2166650.aspx
wjw6200 2008-05-28
  • 打赏
  • 举报
回复


public string _myText;
public From2(string myText)
: this()
{
_myText = myText;
}


可以使用构造函数。

110,539

社区成员

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

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

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