我想对以下问题提供一些帮助:
我有一个mySQL / winforms应用程序,它是abbout客户端及其请求。在某些时候,我想创建一个Tabcontrole。此tabcontrole的选项卡是在运行时创建的。选项卡的数量取决于客户端的请求数量。在选项卡上,还会在运行时创建许多控件(文本框,按钮,e.a。)。
现在我到了被卡住的地步。如何访问选项卡上的控件以将其值存储在数据库中?
这是我用来创建控件的代码:
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.....
我希望我已经明确了问题是什么? 提前感谢您解决我的问题。
答案 0 :(得分:1)
您需要将每个控件都保存在列表中,以便以后可以访问它们。
首先创建一个usercontrol,其中包含需要从DB填充并稍后访问的所有控件。 为这些控件值创建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获取数据并将其发布到数据库简单地预先处理每个用户控件并从中获取数据
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
}