0

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);
}
2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by nick.crane
0

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
            }
        }
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.