当前位置:系统粉 > 电脑问答 > 其他问答 > winform子窗体传值给父窗体

winform子窗体传值给父窗体

提问者:btp023162ez  |  浏览 次  |  提问时间:2017-03-22  |  回答数量:1

winform子窗体传值给父窗体 我想做一个父窗体文本框传值给子窗体的文本框,然后在子窗体的文本框中进行修改后再传回给父窗体。前一半很好写代码,后一半却怎么也传不回来。。。父窗体隐藏后怎么让其显示出来,不知道怎么回事? 代码如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Login { public partial class Form1 : Form { public Form1() { InitializeComponent(); this.texName.Text = s1; this.texPassword.Text = s2; } private void textBox2_TextChanged(object sender, EventArgs e) { } pu

已有1条答案
一罐咖啡

一罐咖啡

回答数:148  |  被采纳数:49

2017-03-22 23:31:04
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Login
{
public partial class Form1 : Form
{
public static string s1;
public static string s2;
public Form1()
{
InitializeComponent();


}

private void textBox2_TextChanged(object sender, EventArgs e)
{

}

private void btnOK_Click(object sender, EventArgs e)
{

s1 = this.texName.Text;
s2 = this.texPassword.Text;
//this.Hide();
Form2 frm2 = new Form2();
frm2.ShowDialog();
this.texName.Text = s1;
this.texPassword.Text = s2;


}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{

e.Cancel = true;
if (MessageBox.Show("是否退出", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
e.Cancel = false;
}
}


}
}




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Login
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
this.txtName.Text = Form1.s1;
this.txtPassword.Text = Form1.s2;
}


private void btnUpdate_Click(object sender, EventArgs e)//这个事件用来让子窗体文本框中的值传回给父窗体中的变量
{

Form1 frm1 = new Form1();//初始化父窗体,让其文本中的值发生改变
Form1.s1 = this.txtName.Text;
Form1.s2 = this.txtPassword.Text;
this.Close();//关闭子窗体




}


}
}
赞 14
解决方法
版权信息

Copyright @ 2011 系统粉 版权声明 最新发布内容 网站导航