Hi,

how can I read value of dynamic MaskedTextBox created in GroupBox?

private void add_groupbox(int x, int y, int id)
{
            GroupBox gb1 = new GroupBox();
            MaskedTextBox mtb1 = new MaskedTextBox();
            MaskedTextBox mtb2 = new MaskedTextBox();
            MaskedTextBox mtb3 = new MaskedTextBox();

            mtb1.Location = new Point(46, 18);
            mtb1.Size = new Size(53, 20);
            int id_3 = id * 3;
            label1.Name = "maskedTextBox" + (id_3-2).ToString();

            mtb2.Location = new Point(148, 18);
            mtb2.Size = new Size(53, 20);
            label1.Name = "maskedTextBox" + (id_3-1).ToString();

            mtb3.Location = new Point(328, 18);
            mtb3.Size = new Size(53, 20);
            label1.Name = "maskedTextBox" + id_3.ToString();

            gb1.FlatStyle = FlatStyle.Flat;        
            gb1.Controls.Add(mtb1);
            gb1.Controls.Add(mtb2);
            gb1.Controls.Add(mtb3);

            gb1.Size = new Size(394, 44);
            gb1.Location = new Point(x, y);
            gb1.Text = id.ToString();
            gb1.Name = "groupBox" + id.ToString();
            Controls.Add(gb1);
}

You could read the values when the user presses a button.
Or, you could capture the TextChanged event of MaskedTextBox controls.

private void add_groupbox(int x, int y, int id)
        {
            GroupBox gb1 = new GroupBox();
            MaskedTextBox mtb1 = new MaskedTextBox();
            MaskedTextBox mtb2 = new MaskedTextBox();
            MaskedTextBox mtb3 = new MaskedTextBox();
            //...
            int id_3 = id * 3;
            //...
            mtb1.Tag = id_3 - 2;
            mtb1.TextChanged += new EventHandler(mtb_TextChanged);
            mtb2.Tag = id_3 - 1;
            mtb2.TextChanged += new EventHandler(mtb_TextChanged);
            mtb3.Tag = id_3;
            mtb3.TextChanged += new EventHandler(mtb_TextChanged);

            Controls.Add(gb1);
        }

        private void mtb_TextChanged(object sender, EventArgs e)
        {
            MaskedTextBox mtb = sender as MaskedTextBox;
            if (mtb != null)
            {
                // do something - using mtb.Tag to identify mtb
            }
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.