如何将文本框文本传递到事件处理程序?

时间:2019-08-19 07:09:50

标签: c# winforms text event-handling

我创建了一个FlowLayoutPanel以具有一些文本框系列。我想说我有3个“ Ecoles”,并要为这3个“ ecole”写“ Adresse”,“ Telephone”...。 FlowLayoutPanel添加一些验证按钮以将其保存到数据库中。 我为按钮验证创建了EventHandler,但无法访问文本框文本以保存它。

如何将文本框值添加到此EventHandler

public void comboBoxNbrEcole_SelectedIndexChanged(object sender, EventArgs e)
{
    try
    {
        string nbre = comboBoxNbrEcole.SelectedItem.ToString();
        int nbre2 = int.Parse(nbre);
        MessageBox.Show(nbre);
        comboBoxNbrEcole.Hide();
        label10.Hide();
        label1.Show();
        label2.Show();
        label3.Show();
        label4.Show();
        label5.Show();
        label6.Show();
        label7.Show();
        label8.Show();
        label9.Show();

        flowLayoutPanelEcole.Controls.Clear();
        flowLayoutPanelAdresse.Controls.Clear();
        flowLayoutPanelTelephone.Controls.Clear();
        flowLayoutPanelMail.Controls.Clear();
        flowLayoutPanelCE.Controls.Clear();
        flowLayoutPanelNE.Controls.Clear();
        flowLayoutPanelAnnee.Controls.Clear();
        flowLayoutPanelNC.Controls.Clear();
        flowLayoutPanelTemps.Controls.Clear();
        flowLayoutPanelValider.Controls.Clear();

        for (int i = 1; i <= nbre2 ; i++)
        {
            TextBox Ecole = new TextBox ();
            Ecole.Name = "Ecole" + i.ToString();
            Ecole.Text = Ecole.Name;
            TextBox Adresse = new TextBox();
            Adresse.Name = "TextBox" + i.ToString();
            TextBox Telephone = new TextBox();
            Telephone.Name = "TextBox" + i.ToString();
            TextBox Mail = new TextBox();
            Mail.Name = "TextBox" + i.ToString();
            TextBox CE = new TextBox();
            CE.Name = "TextBox" + i.ToString();
            TextBox NE = new TextBox();
            NE.Name = "TextBox" + i.ToString();
            TextBox Annee = new TextBox();
            Annee.Name = "TextBox" + i.ToString();
            TextBox NC = new TextBox();
            NC.Name = "TextBox" + i.ToString();
            TextBox Temps = new TextBox();
            Temps.Name = "TextBox" + i.ToString();
            Button Valider = new Button();
            Valider.Name = "Valider" + i.ToString();
            Valider.Text = "Valider" + i.ToString();
            Valider.Click += new EventHandler(Valider_Click);

            flowLayoutPanelEcole.Controls.Add(Ecole);
            flowLayoutPanelAdresse.Controls.Add(Adresse);
            flowLayoutPanelTelephone.Controls.Add(Telephone);
            flowLayoutPanelMail.Controls.Add(Mail);
            flowLayoutPanelCE.Controls.Add(CE);
            flowLayoutPanelNE.Controls.Add(NE);
            flowLayoutPanelAnnee.Controls.Add(Annee);
            flowLayoutPanelNC.Controls.Add(NC);
            flowLayoutPanelTemps.Controls.Add(Temps);
            flowLayoutPanelValider.Controls.Add(Valider);

            if (Ecole.Name == "Ecole1")
            {
                MessageBox.Show(Ecole.Text);
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("Erreur" + ex);
    }
}

public void Valider_Click(object sender, EventArgs e)
{
    MessageBox.Show("OK");
    try
    {
        ABC.Open();
        OleDbCommand command = new OleDbCommand();
        command.Connection = ABC;
        command.CommandText = "insert into 2019_Ecole (Ecole) Values ('" + Ecole + "')";
        ABC.Close();
    }
    catch(Exception ex)
    {
        MessageBox.Show("Erreur" + ex);
    }
}

0 个答案:

没有答案