jeudi 31 janvier 2019

Values cleared after add dynamic textbox

i programmed a dynamic textbox button. When i click on the button, a dynamic textbox will created on my program. I have two more textboxes which are static on my program.

Now the problem: When i add a value in the two static textboxes, i can show me the values with a MessageBox.Show. After i click the the "add dynamic textbox" button, the values are NULL. So the values from the static textboxes are empty. The dynamic textboxes are not NULL and i can show me the values of them.

{

public partial class Form1 : Form static int i = 1;

    public Form1()
    {
        InitializeComponent();
    }

    private void maskedTextBox1_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
    {

    }


    private void button2_Click(object sender, EventArgs e)
    {
        if (i < 8)

        {
            InitializeComponent();
            TextBox tb = new TextBox();
            Label lb = new Label { };
            lb.Text = i + ". label";
            lb.Name = "label" + i;
            tb.Name = "textbox" + i ;
            tb.Width = 238;
            tb.Height = 35;
            lb.Width = 238;
            lb.Height = 35;
            tb.Font = new Font(tb.Font.FontFamily, 18);
            lb.Font = new Font(lb.Font.FontFamily, 18);

            Point p = new Point(223, 144 + (40 * i));
            Point p2 = new Point(50, 144 + (40 * i));

            lb.Location = p2;
            tb.Location = p;

            this.Controls.Add(tb);
            this.Controls.Add(lb);
            i++;
        }
        else
        {
            MessageBox.Show("Maximum reached");
        }
    }


    public void insert()
    {



        TextBox tb1 = this.Controls.Find("textbox1", false).FirstOrDefault() as TextBox;
        TextBox tb2 = this.Controls.Find("textbox2", false).FirstOrDefault() as TextBox;
        TextBox tb3 = this.Controls.Find("textbox3", false).FirstOrDefault() as TextBox;
        TextBox tb4 = this.Controls.Find("textbox4", false).FirstOrDefault() as TextBox;
        TextBox tb5 = this.Controls.Find("textbox5", false).FirstOrDefault() as TextBox;
        TextBox tb6 = this.Controls.Find("textbox6", false).FirstOrDefault() as TextBox;
        TextBox tb7 = this.Controls.Find("textbox7", false).FirstOrDefault() as TextBox;

  MessageBox.Show(textboxstatic1.ToString());

}

expected result: entered value result after add dynamic textbox: no value

Aucun commentaire:

Enregistrer un commentaire