将fom1的comboBox值显示为Textbox中的另一个窗体
本文关键字:Textbox 另一个 窗体 显示 fom1 comboBox | 更新日期: 2023-09-27 18:20:55
我有三种形式Form1、Form2和Form3。我在表格1中有一个组合框,我希望它显示在表格3中。问题:所以我想要的是,当我在表格1中选择comboBox的值并单击按钮时,它应该打开表格2,当我再次单击按钮时在表格2中,它应该在表格3的文本框中显示comboBox选择的值。
private void button1_Click(object sender, EventArgs e) //form1
{
Form2 f2 = new Form2(); // don't know what to put in this parameter
f2.ShowDialog();
}
ComboBox CB1;
public Form2(ComboBox cb1)
{
InitializeComponent();
CB1= cb1;
cb1.Text.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
Form3 f3 = new Form3(CB1);
f3.ShowDialog();
}
public partial class Form3 : Form
{
ComboBox Combo;
public Form3(ComboBox combo)
{
InitializeComponent();
Combo = combo;
Combo.Text = combo.ToString();
}
解决方案1:
一个简单的解决方案是在您的第一个窗口中为所选项目定义一个静态属性
public static string SelectedItem;
public MainWindow()
{
InitializeComponent();
}
在Window3中,您可以分配这样的值,
public Window3()
{
InitializeComponent();
txtresult.Text = MainWindow.SelectedItem;
}
解决方案2:
If you have to pass the value through the constructor just pass the selectedItem as a String through each window, something like this,
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(combobox.SelectedItem.ToString());
f2.ShowDialog();
}
string result = null;
public Form2(String selected)
{
InitializeComponent();
result = selected;
}
在形式3中,
public Form3(string result)
{
InitializeComponent();
Combo.Text = result();
}
在从form1到form2的事件中
Form2 obj = new Form2(ComboBox1.SelectedValue);
this.Hide();
Form2.Show();
形式2:
string someName="";
public form2(string name)
{
InitializeComponent();
someName=name;
}