wpf 将 fom1 的组合框值显示到文本框中的另一个表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25717113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
display comboBox value of fom1 to another form in Textbox
提问by djinc
I have three forms Form1, Form2 and Form3. I have a comboBox in form1 which I want it to be displayed in Form 3. Question: So what I want is that when I select the value of comboBox in form1 and click the button, it should open form2 and in form2 when I click the button again, It should show the comboBox selected value in a textbox in Form3.
我有三个表单 Form1、Form2 和 Form3。我在form1中有一个comboBox,我希望它显示在Form 3中。 问题:所以我想要的是,当我在form1中选择comboBox的值并单击按钮时,它应该在我单击时打开form2和form2再次按钮,它应该在 Form3 的文本框中显示组合框选定的值。
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();
}
回答by Sajeetharan
SOLUTION1:
解决方案1:
One simple solution is defining a static property in your first window for the selected item,
一个简单的解决方案是在您的第一个窗口中为所选项目定义一个静态属性,
public static string SelectedItem;
public MainWindow()
{
InitializeComponent();
}
And in Window3, you can assign the value like this,
在 Window3 中,您可以像这样分配值,
public Window3()
{
InitializeComponent();
txtresult.Text = MainWindow.SelectedItem;
}
SOLUTION2:
解决方案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;
}
In form 3,
在表格 3 中,
public Form3(string result)
{
InitializeComponent();
Combo.Text = result();
}
回答by SomeUser
In the event from form1 to form2
从form1到form2的事件
Form2 obj = new Form2(ComboBox1.SelectedValue);
this.Hide();
Form2.Show();
In form2:
在表格 2 中:
string someName="";
public form2(string name)
{
InitializeComponent();
someName=name;
}

