创建的控制,我想一些帮助了以下问题:如何获得的值在运行时
我有一个MySQL/WinForms应用程序是abbout客户和他们的请求。在某个时候,我想创建一个Tabcontrole。此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);
}
}
在我involke的方法读取器 “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.....
我希望我已经明确的问题是什么? 在此先感谢您解决我的问题。
您必须将控制保持到一个列表(最好使用Tab中的所有控件创建一个用户控件),并使用本地列表访问它们(列表)。如果你想用硬性的方法...使用foreach this.Controls并按名称搜索。 –
感谢您的回复,但我不明白该怎么办?你能否给我多些提示? –