gpt4 book ai didi

c# - 如何从运行时创建的控件中获取值

转载 作者:行者123 更新时间:2023-11-29 13:31:44 26 4
gpt4 key购买 nike

我需要有关以下问题的帮助:

我有一个 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.....

我希望我已经弄清楚问题是什么?预先感谢您解决我的问题。

最佳答案

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

首先创建一个用户控件,其中包含需要从数据库填充并稍后访问的所有控件。为这些控制值创建 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

}

关于c# - 如何从运行时创建的控件中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19361938/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com