C # tab (tabcontrol) dynamically add tabpage (get or set the current tab and its attributes)

Source: Internet
Author: User

Create a winform program and drag a tabcontrol (delete two default tabpages in the tabpages attribute) and three buttons (add, delete, and modify)

  1. Public partial class form1: Form
  2. {
  3. Private int Index = 0;
  4. Public form1 ()
  5. {
  6. Initializecomponent ();
  7. }
  8. Private void form1_load (Object sender, eventargs E)
  9. {
  10. // This. tabcontrol1.selectedindex = 1;
  11. }
  12. Private void btnadd_click (Object sender, eventargs E)
  13. {
  14. Tabpage page = new tabpage ();
  15. Page. Name = "page" + index. tostring ();
  16. Page. Text = "tabpage" + index. tostring ();
  17. Page. tabindex = index;
  18. This. tabcontrol1.controls. Add (PAGE );
  19.  
  20. # Region three methods to set a tab as the current tab
  21. // This. tabcontrol1.selectedindex = index;
  22. This. tabcontrol1.selectedtab = page;
  23. // This. tabcontrol1.selecttab ("page" + index. tostring ());
  24. # Endregion
  25. Index ++;
  26. }
  27. Private void btnremove_click (Object sender, eventargs E)
  28. {
  29. Bool first = true;
  30. If (index> 0)
  31. {
  32. # Region two ways to delete a tab
  33. This. tabcontrol1.controls. removeat (this. tabcontrol1.selectedindex );
  34. // This. tabcontrol1.controls. Remove (this. tabcontrol1.tabpages [This. tabcontrol1.tabpages. Count-1]);
  35. # Endregion
  36. }
  37. Else
  38. {
  39. Return;
  40. }
  41.  
  42. # Region is used to delete the last tabpage and set the last and second to the current tab.
  43. If (first)
  44. {
  45. This. tabcontrol1.selectedindex = -- index-1;
  46. First = false;
  47. }
  48. Else
  49. {
  50. This. tabcontrol1.selectedindex = index --;
  51. }
  52. # Endregion
  53. }
  54. Private void btnupdate_click (Object sender, eventargs E)
  55. {
  56. This. tabcontrol1.selectedtab. Text = "xyt"; // modify the attributes of the current tab.
  57. // This. tabcontrol1.selectedtab. Name = "";
  58. // This. tabcontrol1.selectedtab. Tag = "";
  59. // This. tabcontrol1.selectedtab. Select ();
  60. }
  61. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.