0

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

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

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

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

 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.....

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

4

1 回答 1

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 > 

稍后,在列表中创建 UserControlwhile (myReader.Read())并在列表中添加该控件后将其传递给函数以将其放置在 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

}

于 2013-10-14T15:23:29.410 回答