从 C# 中的另一个窗体获取组合框值
Get comboBox value from another form in C#
我有2个表格,在Form1中有一个按钮可以显示Form2。在 Form2 中我有一个组合框。从组合框中选择一个项目后,用户可以单击按钮将组合框值发送到 Form1,Form2 将关闭。
这是我的代码:
表格 1:
private void Button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.ShowDialog();
}
表格 2:
private void Button1_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
frm1.textBox1.Text = Convert.ToString(comboBox1.SelectedValue);
this.DialogResult = DialogResult.OK;
}
但是我的组合框值没有出现在 Form1 的文本框中saas
您正试图在新表单的组合框中设置一个值,因为您在此处创建它:
Form1 frm1 = new Form1();
您应该将对 Form1 实例的引用传递给 Form2(通过构造函数或成员字段)。
正确的做法是在Form2中添加一个Form1类型的私有成员字段class,在Form2的构造函数中添加一个参数,并在构造函数调用时初始化:
var form2 = new Form2(this);
然后引用成员字段。
您应该将此对象的引用发送到新表单。
表格 1:
private void Button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2(this);
frm2.ShowDialog();
}
来自 2:
Form _parentForm;
public Form2(Form frm)
{
_parentForm = frm;
}
private void Button1_Click(object sender, EventArgs e)
{
_parentForm.textBox1.Text = Convert.ToString(comboBox1.SelectedValue);
this.DialogResult = DialogResult.OK;
}
在你的按钮点击事件上试试这个:
TextBox txt = (Form1)this.Owner.Textbox1;
txt.Text = combobox1.Text;
this.Close();
您可以在文本框中设置修饰符 属性。这个属性应该是public。
表格 2:
`
public Form2()`
{
InitializeComponent();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Form1 frm = (Form1)Application.OpenForms["Form1"];
frm.textBox1.Text = comboBox1.Text; this.Close();
}
试试这个代码。
表格 1:
public void SetValue(string str)
{
textBox1.Text = str;
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2(this);
frm2.Show();
}
表格 2 :
只读 Form1 _ownerForm;
public Form2(Form1 ownerForm)
{
InitializeComponent();
this._ownerForm = ownerForm;
}
private void button1_Click(object sender, EventArgs e)
{
string selectedText = comboBox1.SelectedItem.ToString();
this._ownerForm.SetValue(selectedText);
this.Close();
}
我有2个表格,在Form1中有一个按钮可以显示Form2。在 Form2 中我有一个组合框。从组合框中选择一个项目后,用户可以单击按钮将组合框值发送到 Form1,Form2 将关闭。
这是我的代码:
表格 1:
private void Button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.ShowDialog();
}
表格 2:
private void Button1_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
frm1.textBox1.Text = Convert.ToString(comboBox1.SelectedValue);
this.DialogResult = DialogResult.OK;
}
但是我的组合框值没有出现在 Form1 的文本框中saas
您正试图在新表单的组合框中设置一个值,因为您在此处创建它:
Form1 frm1 = new Form1();
您应该将对 Form1 实例的引用传递给 Form2(通过构造函数或成员字段)。
正确的做法是在Form2中添加一个Form1类型的私有成员字段class,在Form2的构造函数中添加一个参数,并在构造函数调用时初始化:
var form2 = new Form2(this);
然后引用成员字段。
您应该将此对象的引用发送到新表单。
表格 1:
private void Button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2(this);
frm2.ShowDialog();
}
来自 2:
Form _parentForm;
public Form2(Form frm)
{
_parentForm = frm;
}
private void Button1_Click(object sender, EventArgs e)
{
_parentForm.textBox1.Text = Convert.ToString(comboBox1.SelectedValue);
this.DialogResult = DialogResult.OK;
}
在你的按钮点击事件上试试这个:
TextBox txt = (Form1)this.Owner.Textbox1;
txt.Text = combobox1.Text;
this.Close();
您可以在文本框中设置修饰符 属性。这个属性应该是public。
表格 2: `
public Form2()`
{
InitializeComponent();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Form1 frm = (Form1)Application.OpenForms["Form1"];
frm.textBox1.Text = comboBox1.Text; this.Close();
}
试试这个代码。
表格 1:
public void SetValue(string str)
{
textBox1.Text = str;
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2(this);
frm2.Show();
}
表格 2 :
只读 Form1 _ownerForm;
public Form2(Form1 ownerForm)
{
InitializeComponent();
this._ownerForm = ownerForm;
}
private void button1_Click(object sender, EventArgs e)
{
string selectedText = comboBox1.SelectedItem.ToString();
this._ownerForm.SetValue(selectedText);
this.Close();
}