MainTest.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. namespace IntelligentControlForsx.Test
  12. {
  13. public partial class MainTest : Form
  14. {
  15. private bool isAllLoad = false;
  16. public MainTest()
  17. {
  18. InitializeComponent();
  19. for (int i = 0; i < 5; i++)
  20. {
  21. Button bt = new Button();
  22. bt.Name = "bt";
  23. bt.Text = "btn" + i;
  24. bt.Dock = DockStyle.Left;
  25. plStructure.Controls.Add(bt);
  26. }
  27. }
  28. private void MainTest_Load(object sender, EventArgs e)
  29. {
  30. }
  31. private void MainTest_Shown(object sender, EventArgs e)
  32. {
  33. }
  34. private void ThreadTest()
  35. {
  36. ControlAddDelegate del = new ControlAddDelegate(ControlAdd);
  37. this.plLoad.Invoke(del);
  38. }
  39. private void ControlAdd()
  40. {
  41. Thread.Sleep(5000);
  42. for (int i = 0; i < 5; i++)
  43. {
  44. Button bt = new Button();
  45. bt.Name = "bt" + i;
  46. bt.Text = "btn" + i;
  47. bt.Dock = DockStyle.Left;
  48. plLoad.Controls.Add(bt);
  49. }
  50. for (int i = 5; i < 10; i++)
  51. {
  52. Button bt = new Button();
  53. bt.Name = "bt" + i;
  54. bt.Text = "btn" + i;
  55. bt.Dock = DockStyle.Left;
  56. plOther.Controls.Add(bt);
  57. }
  58. plLoad.Hide();
  59. plOther.Hide();
  60. isAllLoad = true;
  61. Console.WriteLine("线程加载完毕====================================");
  62. }
  63. public delegate void ControlAddDelegate();
  64. private void timer1_Tick(object sender, EventArgs e)
  65. {
  66. if (isAllLoad == false)
  67. {
  68. Console.WriteLine("定时器执行(未加载完毕)");
  69. Thread t = new Thread(ThreadTest);
  70. t.Start();
  71. Console.WriteLine("form加载完毕====================================");
  72. }
  73. else
  74. {
  75. Console.WriteLine("定时器执行(已经加载完毕)");
  76. timer1.Enabled = false;
  77. }
  78. }
  79. private void btnShow1_Click(object sender, EventArgs e)
  80. {
  81. plStructure.Show();
  82. plLoad.Hide();
  83. plOther.Hide();
  84. }
  85. private void btnShow2_Click(object sender, EventArgs e)
  86. {
  87. plStructure.Hide();
  88. plLoad.Show();
  89. plOther.Hide();
  90. }
  91. private void btnShow3_Click(object sender, EventArgs e)
  92. {
  93. plStructure.Hide();
  94. plLoad.Hide();
  95. plOther.Show();
  96. }
  97. private void btnGetAllControl_Click(object sender, EventArgs e)
  98. {
  99. foreach (Control ctrl in this.plStructure.Controls)
  100. {
  101. Console.WriteLine(ctrl.Name);
  102. }
  103. }
  104. }
  105. }