本文介绍了如何将comboBox数据源从form1链接到form2 comboBox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何显示来自form1 comboBox的form2中comboBox中的成员?
how can I display members in comboBox in form2 from form1 comboBox?
Form form1 = Application.OpenForms["Form1"]; private void BookSetupForm_Load(object sender, EventArgs e) { comboBox1.DataSource = form1boBox1; }问题有所不同,因为我想传输comboBox值而不是文本框值和用法
The question is different as I want to transfer comboBox values not textboxes values and uses a different approach.
推荐答案因为表格1上的 ComboBox 是不是 public ,您需要一个额外的字段或属性作为传输对象:
Since the ComboBox on your Form 1 is not public you would need an extra field or property as transfer object:
public partial class Form1 : Form { public ComboBox comboTransfer; public Form1() { InitializeComponent(); // example combobox thisboBox1.Items.AddRange(new string[] { "1", "2", "3" }); // reroute the content thisboTransfer = comboBox1; } }在表格2中,您可以访问该转移对象
In Form 2 you could access then this transfer object
public partial class Form2 : Form { Form1 Form_1; public Form2() { InitializeComponent(); Form_1 = Application.OpenForms["Form1"] as Form1; } private void BookSetupForm_Load(object sender, EventArgs e) { // access here your transfer object thisboBox1.DataSource = Form_1boTransfer.Items; } }这只是这样做的一种方法(首先在我脑海中浮现)
this is just one way of doing it (first that came to my mind)
更多推荐
如何将comboBox数据源从form1链接到form2 comboBox?
发布评论