當前位置:係統粉 > 電腦問答 > 其他問答 > 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 係統粉 版權聲明 最新發布內容 網站導航