代码之家  ›  专栏  ›  技术社区  ›  Developer

如何在FormLoad上从类文件加载组合框中的项

  •  0
  • Developer  · 技术社区  · 14 年前

    我想加载在类文件中声明的项,当窗体加载时,任何人都可以给我一个主意

    我的类文件代码如下

    namespace ACHDAL
    {
       public class TansactionCode
       {
        string[] strTransactionCodes ={"20","21","22","23","24","25","26","27","28","29","30","31","32","33","34",
            "35","36","37","38","39","41","42","43","44","46","47","48","49","51","52","53","54","55","56","80",
            "81","82","83","84","85","86"};
    
    }
    }
    

    如果需要在此代码中执行任何操作,请通知我,当表单加载时,希望将所有这些内容加载到组合框中。

    2 回复  |  直到 14 年前
        1
  •  2
  •   Iain Ward    14 年前

    通过设置组合框 DataSource 财产 (see here for more details on what you can bind to them) .

    要做到这一点,您需要首先公开列表,所以将其放入一个属性中。这是窗体在创建类的新实例后访问它的方式。

    public string[] TransactionCodes
    {
        get { return strTransactionCodes; }
        set { strTransactionCodes = value; }
    }
    

    然后在FormLoad事件上执行此操作

    private void Form1_Load(object sender, EventArgs e)
    {
        TansactionCode trans = new TansactionCode();    // Create new instance
        combobox.DataSource = trans.TransactionCodes;   // Access the list property
    }
    
        2
  •  0
  •   RKh    14 年前

    我不确定,但我认为Linq也会有帮助。检查 this 链接。您需要相应地修改: