簡體   English   中英

如何將combobox1項目從form1傳遞到form2中的combobox2

[英]How to pass combobox1 items from form1 to combobox2 in form2

我有兩個 combobox(表單 1 中的組合框 1 和表單 2 中的組合框 2)。 我需要導入一個 excel 文件,combobox1 獲取工作表名稱。 然后在form2中(我不想再次導入同一個文件)我有combobox2,它也具有與combobox1相同的工作表名稱。

我試過這個,但我得到了 NULLFUNCTION 表達式的錯誤(附加信息:object 引用未設置為對象的實例)。

 using (OpenFileDialog openFileDialog = new OpenFileDialog() { Filter = "Excel 97-2003 Workbook|*.xls|Excel Workbook|*.xlsx |fichiers Textes (*.txt)|*.txt|fichiers CSV (*.csv)|*.csv|tous les fichiers (*.*)|*.*" })
            {
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    TextBox_Emplacement.Text = openFileDialog.FileName;
                    using (var stream = File.Open(openFileDialog.FileName, FileMode.Open, FileAccess.Read))
                    {
                        using (IExcelDataReader reader = ExcelReaderFactory.CreateReader(stream))
                        {
                            DataSet result = reader.AsDataSet(new ExcelDataSetConfiguration()
                            {
                                ConfigureDataTable = (_) => new ExcelDataTableConfiguration() { UseHeaderRow = true }
                            });
                            dataTableCollection = result.Tables; 
                            dataTableCollection1 = result.Tables;
                            sheet.Items.Clear();
                            //cp.radDropDownList1.Items.Clear();
                            foreach (DataTable table in dataTableCollection)
                                sheet.Items.Add(table.TableName);//add sheet to combobox

                           **Form_Copie cp1 = (Form_Copie)Application.OpenForms["Form_Copie"];
                           foreach (DataTable table in dataTableCollection)
                              cp1.radDropDownList1.Items.Add(table.TableName);//add sheet to combobox**

如果您嘗試傳遞的項目只是 combobox 字符串值可以在加載新表單時傳遞該值

{
string sheet = comboBox1.Text;
Form frm2 = new Form2(sheet);
frm2.show();
}
Public Form2(string sheet)
{
Initialize();
comboBox2.Text = sheet;
}

有幾種方法可以做到這一點。 重載構造函數是一種好方法,但對每個控件執行此操作很快就會變得令人生畏和混亂。 如果您想對多個控件執行此操作,我建議使用包含綁定屬性的 model(例如 System.ComponentModel.BindingList 和/或 System.Forms.Binding)作為每個表單中的字段。 然后重載構造函數並通過 model class 傳入綁定。

public class BindingModel {
    public property BindingList<string> ComboBoxBinding {get;set;}
    ... other binding/non-binding properties ...
}

public class Form1{
    private BindingModel _bindingModel = new _bindingModel();

    private void Form1_Shown(object sender, EventArgs e) {
        this.ComboBox1.DataSource = _bindingModel.ComboBoxBinding;
        this.ComboBox1.DataMember = "";

        // the add would go in your excel method
        _bindingModel.ComboBoxBinding.Add("Item 1");
    }

    private void btnOpenForm2_Click(object sender, EventArgs e) {
        var frm = new Form2(_bindingModel);
        frm.Show();
        this.Close();
    }
}

public class Form2{
    private BindingModel _bindingModel = new _bindingModel();
    
    public void Form2(BindingModel bindingModel){
        this._bindingModel = bindingModel;
    }

    // Once the form is shown bind the ComboBox and it will populate the values
    private void Form2_Shown(object sender, EventArgs e){
        this.ComboBox1.DataSource = _bindingModel.ComboBoxBinding;
        this.ComboBox1.DataMember = "";
    }
}

或者,可以在調用表單中設置控件項:

public class Form1{
    private void btnOpenForm2_Click(object sender, EventArgs e) {
        var frm = new Form2();
        for(int i = 0; i < this.ComboBox1.Count - 1; i++){
            frm.ComboBox1.Items.Add(ComboBox1.Items[i]);
        }
        frm.Show();
        this.Close();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM