Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- c# Dynamic textbox control with CheckBox and List
- private List<MyControls> _myControls = new List<MyControls>();
- class MyControls
- {
- int x=5;
- int y=30;
- public CheckBox cb = new CheckBox();
- public TextBox tb1 = new TextBox();
- public TextBox tbSpecs = new TextBox();
- public TextBox tb3 = new TextBox();
- public TextBox tb4 = new TextBox();
- public void initElements(String name)
- {
- cb.Width = 10;
- cb.Height = 10;
- cb.Name = "cb_" + name;
- cb.Location = new Point(x, y+5);
- Form.ActiveForm.Controls.Add(cb);
- x += 15;
- tb1.Width = 50;
- tb1.Height = 20;
- tb1.Location = new Point(x, y);
- tb1.Name = "tb1_" + name;
- Form.ActiveForm.Controls.Add(tb1);
- x += 60;
- tbSpecs.Width = 150;
- tbSpecs.Height = 20;
- tbSpecs.Name = "tb2_" + name;
- tbSpecs.Location = new Point(x, y);
- Form.ActiveForm.Controls.Add(tbSpecs);
- x += 160;
- tb3.Width = 40;
- tb3.Height = 20;
- tb3.Name = "tb3_" + name;
- tb3.Location = new Point(x, y);
- Form.ActiveForm.Controls.Add(tb3);
- x += 50;
- tb4.Width = 450;
- tb4.Height = 20;
- tb4.Name = "tb4_" + name;
- tb4.Location = new Point(x, y);
- Form.ActiveForm.Controls.Add(tb4);
- x = 0;
- }
- public int SetX(int i)
- {
- x = i;
- return x;
- }
- public int SetY(int Y)
- {
- y = Y;
- return y;
- }
- }
- public void CreateFormElements()
- {
- ProductForm form2 = new ProductForm();
- form2.Visible = true;
- form2.Activate();
- MyControls mc = new MyControls();
- _myControls.Add(mc);
- _myControls[0].initElements("1");
- mc = new MyControls();
- _myControls.Add(mc);
- mc.SetY(55);
- _myControls[1].initElements("2");
- }
- cb.CheckedChanged += cb_CheckedChanged;
- private void cb_CheckedChanged(Object sender, EventArgs e) {
- string NameSet = (sender as CheckBox).Name.Split(new char[]{'_'})[1];
- Form.ActiveForm.Controls.Remove("ch_" + NameSet);
- Form.ActiveForm.Controls.Remove("tb1_" + NameSet);
- Form.ActiveForm.Controls.Remove("tb2_" + NameSet);
- Form.ActiveForm.Controls.Remove("tb3_" + NameSet);
- Form.ActiveForm.Controls.Remove("tb4_" + NameSet);
- }
Add Comment
Please, Sign In to add comment