Guest User

Untitled

a guest
Aug 20th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. c# Dynamic textbox control with CheckBox and List
  2. private List<MyControls> _myControls = new List<MyControls>();
  3.  
  4. class MyControls
  5. {
  6. int x=5;
  7. int y=30;
  8. public CheckBox cb = new CheckBox();
  9. public TextBox tb1 = new TextBox();
  10.  
  11. public TextBox tbSpecs = new TextBox();
  12. public TextBox tb3 = new TextBox();
  13. public TextBox tb4 = new TextBox();
  14.  
  15.  
  16.  
  17. public void initElements(String name)
  18. {
  19. cb.Width = 10;
  20. cb.Height = 10;
  21. cb.Name = "cb_" + name;
  22. cb.Location = new Point(x, y+5);
  23. Form.ActiveForm.Controls.Add(cb);
  24. x += 15;
  25.  
  26. tb1.Width = 50;
  27. tb1.Height = 20;
  28. tb1.Location = new Point(x, y);
  29. tb1.Name = "tb1_" + name;
  30. Form.ActiveForm.Controls.Add(tb1);
  31. x += 60;
  32.  
  33. tbSpecs.Width = 150;
  34. tbSpecs.Height = 20;
  35. tbSpecs.Name = "tb2_" + name;
  36. tbSpecs.Location = new Point(x, y);
  37. Form.ActiveForm.Controls.Add(tbSpecs);
  38. x += 160;
  39.  
  40. tb3.Width = 40;
  41. tb3.Height = 20;
  42. tb3.Name = "tb3_" + name;
  43. tb3.Location = new Point(x, y);
  44. Form.ActiveForm.Controls.Add(tb3);
  45. x += 50;
  46.  
  47. tb4.Width = 450;
  48. tb4.Height = 20;
  49. tb4.Name = "tb4_" + name;
  50. tb4.Location = new Point(x, y);
  51. Form.ActiveForm.Controls.Add(tb4);
  52.  
  53. x = 0;
  54. }
  55.  
  56. public int SetX(int i)
  57. {
  58. x = i;
  59. return x;
  60. }
  61.  
  62. public int SetY(int Y)
  63. {
  64. y = Y;
  65. return y;
  66. }
  67.  
  68. }
  69.  
  70. public void CreateFormElements()
  71. {
  72. ProductForm form2 = new ProductForm();
  73. form2.Visible = true;
  74. form2.Activate();
  75.  
  76. MyControls mc = new MyControls();
  77.  
  78. _myControls.Add(mc);
  79. _myControls[0].initElements("1");
  80. mc = new MyControls();
  81. _myControls.Add(mc);
  82.  
  83. mc.SetY(55);
  84. _myControls[1].initElements("2");
  85.  
  86.  
  87.  
  88. }
  89.  
  90. cb.CheckedChanged += cb_CheckedChanged;
  91.  
  92. private void cb_CheckedChanged(Object sender, EventArgs e) {
  93. string NameSet = (sender as CheckBox).Name.Split(new char[]{'_'})[1];
  94. Form.ActiveForm.Controls.Remove("ch_" + NameSet);
  95. Form.ActiveForm.Controls.Remove("tb1_" + NameSet);
  96. Form.ActiveForm.Controls.Remove("tb2_" + NameSet);
  97. Form.ActiveForm.Controls.Remove("tb3_" + NameSet);
  98. Form.ActiveForm.Controls.Remove("tb4_" + NameSet);
  99.  
  100.  
  101. }
Add Comment
Please, Sign In to add comment