【问题标题】:How to get value from control created at runtime如何从运行时创建的控件中获取价值
【发布时间】:2013-10-14 13:57:03
【问题描述】:

我想要一些关于以下问题的帮助:

我有一个关于客户及其请求的 mySQL/winforms 应用程序。在某些时候,我想创建一个 Tabcontrole。此选项卡控件的选项卡是在运行时创建的。选项卡的数量取决于客户端的请求数量。在选项卡上,许多控件(文本框、按钮等)也在运行时创建。

现在我到了卡住的地步。如何访问选项卡上的控件以将其值存储在数据库中?

这是我用来创建控件的代码:

 private void GetAllrequestsForSameClient(string client)
    {
        MySqlConnection MijnConnectie = new MySqlConnection(Constanten.DATABASECONNSTRING);
        string query = "select * from gedeeldeNotepadDB.requests WHERE requestsForeClient = '" + client + "';";
        MySqlCommand mysqlcommand = new MySqlCommand(query, MijnConnectie);
        MySqlDataReader myReader;

        try
        {
            MijnConnectie.Open();
            myReader = mysqlcommand.ExecuteReader();
            while (myReader.Read())
            {
               string onderwerp = myReader.GetString("onderwerpBijstandAanvraag");
               NieweTab(tabControl1, onderwerp);

            }
            MijnConnectie.Close();

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

    }

在阅读器中,我调用了一个方法“NieweTab(tabControl1, onderwerp);” 这是代码:

public void NieweTab(TabControl tabControl1, string onderwerp)
    {
        TabPage tabPage1 = new System.Windows.Forms.TabPage();
        Label lblvan = new System.Windows.Forms.Label();
        Label lblPeriode = new System.Windows.Forms.Label();
        Label lblTot = new System.Windows.Forms.Label();
        MaskedTextBox txtPeriodeTot = new System.Windows.Forms.MaskedTextBox();
        MaskedTextBox txtPeriodeVan = new System.Windows.Forms.MaskedTextBox();
        Label lblDraagkracht = new System.Windows.Forms.Label();
        TextBox textBox1 = new System.Windows.Forms.TextBox();
        Button btnTabIsKlaar = new System.Windows.Forms.Button();
        btnTabIsKlaar.Click += new System.EventHandler(MyButtonHandler);



        tabControl1.Controls.Add(tabPage1);
        tabControl1.Location = new System.Drawing.Point(12, 111);
        tabControl1.Name = "tabControl1";
        tabControl1.SelectedIndex = 0;
        tabControl1.Size = new System.Drawing.Size(533, 209);
        tabControl1.TabIndex = 38;
        //followed by a lot of layout code.....

我希望我已经明确了问题是什么? 提前感谢您解决我的问题。

【问题讨论】:

  • 您必须将控件保存在一个列表中(最好创建一个包含选项卡中所有控件的用户控件)并使用本地列表(list)访问它们。如果您想要困难的方式...使用 foreach this.Controls 并按名称搜索。
  • 感谢您的回复,但我不明白该怎么办?你能给我更多的提示吗?

标签: c# mysql winforms runtime


【解决方案1】:

您需要将每个控件保存在一个列表中,以便以后访问它们。

首先创建一个用户控件,其中包含需要从数据库中填充并稍后访问的所有控件。 为这些控制值创建 Getter 和 setter。 您必须能够像这样使用控件

ucDBControl uc1 = new ucDBControl()
uc1.PeriodeTot = myReader.GetString("PeriodeTot");
uc1.Onderwerp = myReader.GetString("onderwerpBijstandAanvraag");

MySQLParameter onderwerpParam = new MySQLParameter("onderwerp", uc1.Onderwerp, NVarChar,20);
MySQLParameter PeriodeTotParam = new MySQLParameter("PeriodeTot", uc1.PeriodeTot, NVarChar,20);

下一步是创建一个列表来保存类中的用户控件以供将来参考

List< ucDBControl > myListControl = new  List< ucDBControl > 

稍后,在 while (myReader.Read()) 中创建 UserControl,然后将该控件添加到列表中,然后将其传递给函数以将其放置在 newTab 中

    MijnConnectie.Open();
    myReader = mysqlcommand.ExecuteReader();
    while (myReader.Read())
    {
        var ucTemp = new ucDBControl();

        //create and initialize the usercontrol
        string onderwerp = myReader.GetString("onderwerpBijstandAanvraag");
        string PeriodeTot = myReader.GetString("PeriodeTot");
        ucTemp.Onderwerp = onderwerp;
        ucTemp.PeriodeTot = PeriodeTot ;

        //hold it in the list
       myListControl.Add(ucTemp);

        //and add it in the interface
        NieweTab(tabControl1, ucTemp);

    }

那么你必须实现 NieweTab 才能将控件添加到选项卡。

当您想从 UI 获取数据以将它们发布到 DB 时,对于每个用户控件,简单的 foreach 并从中获取数据

foreach(var uc in myListControl){
  //uc.Onderwerp must get the data from the text box
  //and use it in a MySQLParameter.
  MySQLParameter onderwerpParam = new MySQLParameter("onderwerp", uc1.Onderwerp, NVarChar,20);
MySQLParameter PeriodeTotParam = new MySQLParameter("PeriodeTot", uc1.PeriodeTot, NVarChar,20);
 //  Exec sql using the parameters out of the usercontrol

}

【讨论】:

  • 好的,谢谢您的回复。这就是我以后需要的..(-;首先我还需要将数据从 tabcontrolTabs 放入数据库中?
  • 您的问题到底是什么?如何从用户控件中获取数据或如何将数据插入数据库?
  • 我想从用户控件中获取数据并插入到数据库中。我认为您的立场是错误的,因为选项卡的数量(名称)是从数据库中检索的。我刚刚尝试使用“foreach 循环”将 controls.text 放入列表,但似乎只有最后一个选项卡中的控件被添加到列表中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-24
  • 2021-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多