【发布时间】:2019-08-02 08:25:06
【问题描述】:
我正在尝试将信息从表单 B 传输到表单 A。基本上,表单 A 有一个打开新表单(表单 B)的按钮,然后表单 B 有一个文本框,您可以在其中输入文本。然后,当您关闭表单 B(通过关闭按钮)时,表单 A 中的文本框应使用表单 B 中输入的文本进行更新。
但是它不起作用,表单 A 中的文本框没有接收到表单 B 中输入的文本,它给了我一个空值。
//main class of Form A (the one that has to recieve into from Form B)
public partial class FormManager : Form
{
//creating an instance of Form B
FormContact contactForm;
public string a;
public FormManager()
{
InitializeComponent();
ControlsDisabled();
}
private void btnAdd_Click(object sender, EventArgs e)
{
//trying to fill in a textbox from form B into form A
contactForm = new FormContact();
contactForm.Show();
this.Refresh();
txtFname.Text = contactForm.fname;
//^^^the main problem, this value is null
}
private void btnEdit_Click(object sender, EventArgs e)
{
contactForm = new FormContact(txtFname.Text, txtLname.Text);
contactForm.Show();
}
//main class of form B(the form that has to give info to form A)
public partial class FormContact : Form
{
public string fname;
public string lname;
public FormContact()
{
InitializeComponent();
}
private void btnSave_Click(object sender, EventArgs e)
{
fname = txtFnameA.Text;
lname = txtLname.Text;
this.Refresh();
this.Close();
}
public FormContact(string Fname, string Lname)
{
InitializeComponent();
txtFnameA.Text = Fname;
txtLname.Text = Lname;
}
}
【问题讨论】:
-
stackoverflow.com/questions/4587952/passing-data-between-forms - 当您使用
.Close()时,您正在扼杀数据源。尝试改用this.Hide();。