简体   繁体   中英

Why can't I see my dynamically created controls?

When I click on the button just nothing happens!

public void cmdButton1_OnClick(object sender, EventArgs e)
        {
            Panel myPanel = new Panel();
            myPanel.ID = "Panel1";

            for (int i = 0; i < numtourist; i++)
            {
                Label myLabel = new Label();
                myLabel.ID = "lblNameL" + i.ToString();
                myLabel.Text = "Трите имена на латиница ";
                TextBox myTextBox1 = new TextBox();
                myTextBox1.ID = "txtNameL" + i.ToString();
                myPanel.Controls.Add(myLabel);
                myPanel.Controls.Add(myTextBox1);

                Label mylabel2 = new Label();
                mylabel2.ID = "lblNameK" + i.ToString();
                mylabel2.Text = "Трите имена на кирилица";
                TextBox myTextBox2 = new TextBox();
                myTextBox2.ID = "txtNameK" + i.ToString();
                myPanel.Controls.Add(mylabel2);
                myPanel.Controls.Add(myTextBox2);

                Label myLabel3 = new Label();
                myLabel3.ID = "lblEGN" + i.ToString();
                myLabel3.Text = "EГН";
                TextBox myTextBox3 = new TextBox();
                myTextBox3.ID = "txtEGN" + i.ToString();
                myPanel.Controls.Add(myLabel3);
                myPanel.Controls.Add(myTextBox3);

                Label myLabel4 = new Label();
                myLabel4.ID = "lblPersonalCardNum" + i.ToString();
                myLabel4.Text = "Номер на лична карта";
                TextBox myTextBox4 = new TextBox();
                myTextBox4.ID = "txtPersonalCardNum" + i.ToString();
                myPanel.Controls.Add(myLabel4);
                myPanel.Controls.Add(myTextBox4);


                Label myLabel5 = new Label();
                myLabel5.ID = "lblDateOfIssuePC " + i.ToString();
                myLabel5.Text = "Дата на издаане на лична карта:";
                TextBox myTextBox5 = new TextBox();
                myTextBox5.ID = "txtDateOfIssuePC" + i.ToString();
                myPanel.Controls.Add(myLabel5);
                myPanel.Controls.Add(myTextBox5);

                Label myLabel6 = new Label();
                myLabel6.ID = "lblDateOfExpiryPC " + i.ToString();
                myLabel6.Text = "Дата на валидност на лична карта:";
                TextBox myTextBox6 = new TextBox();
                myTextBox6.ID = "txtDateOfExpiryPC" + i.ToString();
                myPanel.Controls.Add(myLabel6);
                myPanel.Controls.Add(myTextBox6);

                Label mylabel6_1 = new Label();
                mylabel6_1.ID = "lblIssuedFrom" + i.ToString();
                mylabel6_1.Text = "Издадеа от";
                TextBox myTextBox6_1 = new TextBox();
                myTextBox6_1.ID = "txtIssuedFrom" + i.ToString();
                myPanel.Controls.Add(mylabel6_1);
                myPanel.Controls.Add(myTextBox6_1);


                Label myLabel7 = new Label();
                myLabel7.ID = "lblDateOfIssuePass " + i.ToString();
                myLabel7.Text = "Дата на издаване на международен паспорт:";
                TextBox myTextBox7 = new TextBox();
                myTextBox7.ID = "txtDateOfIssuePass" + i.ToString();
                myPanel.Controls.Add(myLabel7);
                myPanel.Controls.Add(myTextBox7);


                Label myLabel7_1 = new Label();
                myLabel7_1.ID = "lblPassportNum" + i.ToString();
                myLabel7_1.Text = "Номер на паспорт:";
                TextBox myTextBox7_1 = new TextBox();
                myTextBox7_1.ID = "txtPassportNum" + i.ToString();
                myPanel.Controls.Add(myLabel7_1);
                myPanel.Controls.Add(myTextBox7_1);


                Label myLabel8 = new Label();
                myLabel8.ID = "lblDateOfExpiryPass " + i.ToString();
                myLabel8.Text = "Дата на валидност на международен паспорт:";
                TextBox myTextBox8 = new TextBox();
                myTextBox8.ID = "txtDateOfExpiryPass" + i.ToString();
                myPanel.Controls.Add(myLabel8);
                myPanel.Controls.Add(myTextBox8);

                Label myLabel9 = new Label();
                myLabel9.ID = "lblHomeContact" + i.ToString();
                myLabel9.Text = "Домашен телефон";
                TextBox myTextBox9 = new TextBox();
                myTextBox9.ID = "txtHomeContact" + i.ToString();
                myPanel.Controls.Add(myLabel9);
                myPanel.Controls.Add(myTextBox9);

                Label myLabel10 = new Label();
                myLabel10.ID = "lblMobContact" + i.ToString();
                myLabel10.Text = "Мобилен телефон";
                TextBox myTextBox10 = new TextBox();
                myTextBox10.ID = "txtMobContact" + i.ToString();
                myPanel.Controls.Add(myLabel10);
                myPanel.Controls.Add(myTextBox10);

                Label myLabel11 = new Label();
                myLabel11.ID = "lblEmail" + i.ToString();
                myLabel11.Text = "E-mail адрес";
                TextBox myTextBox11 = new TextBox();
                myTextBox11.ID = "txtEmail" + i.ToString();
                myPanel.Controls.Add(myLabel11);
                myPanel.Controls.Add(myTextBox11);


                Label myLabel12 = new Label();
                myLabel12.ID = "lblAddress" + i.ToString();
                mylabel2.Text = "Адрес";
                TextBox myTextBox12 = new TextBox();
                myTextBox12.ID = "txbAddress" + i.ToString();
                myPanel.Controls.Add(myLabel12);
                myPanel.Controls.Add(myTextBox12);

            }
        }

You are missing one more step. Now add your panel control to another control container or at least your page control.

You have created a panel and addet the others controls on it, but you forgot to add this created panel (myPanel) to your form .

Youre missing the following line of code:

myForm.Controls.Add(myPanel);

Change myForm to the name of your form.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM