如何将comboBox数据源从form1链接到form2 comboBox? [英] How to link comboBox data source from form1 to form2 comboBox?

查看:74
本文介绍了如何将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 = form1.comboBox1;
}

问题有所不同,因为我想传输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
        this.comboBox1.Items.AddRange(new string[] { "1", "2", "3" });
        // reroute the content
        this.comboTransfer = 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
        this.comboBox1.DataSource = Form_1.comboTransfer.Items;
    }
}

这只是这样做的一种方法(首先在我脑海中浮现)

this is just one way of doing it (first that came to my mind)

这篇关于如何将comboBox数据源从form1链接到form2 comboBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆