在文本框中将fom1的comboBox值显示为另一个表单

时间:2014-09-08 03:35:17

标签: c# wpf winforms combobox

我有三种形式Form1,Form2和Form3。我在form1中有一个comboBox,我希望它在表单3中显示。 问题:所以我想要的是当我在form1中选择comboBox的值并单击按钮时,它应该打开form2并在form2中再次单击该按钮时,它应该在Form3的文本框中显示comboBox选择的值。 / p>

    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();
    }

2 个答案:

答案 0 :(得分:1)

<强>解决方法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();
}

答案 1 :(得分:0)

从form1到form2的事件

Form2 obj = new Form2(ComboBox1.SelectedValue);
this.Hide();
Form2.Show();

在form2中:

string someName="";
public form2(string name)
{
InitializeComponent();
someName=name;
}