TabControl.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. namespace System.Windows.Forms
  2. {
  3. public partial class TabControl : Control
  4. {
  5. protected new System.Windows.Controls.TabControl control { get; }
  6. public TabPageCollection TabPages { get; }
  7. private TabAppearance appearance;
  8. public TabAppearance Appearance
  9. {
  10. get => appearance;
  11. set
  12. {
  13. appearance = value;
  14. control.BorderThickness = new System.Windows.Thickness(value == TabAppearance.Normal ? 1 : 0);
  15. if (value != TabAppearance.Normal)
  16. control.Background = null;
  17. }
  18. }
  19. private System.Drawing.Size itemSize;
  20. public System.Drawing.Size ItemSize
  21. {
  22. get => itemSize;
  23. set
  24. {
  25. itemSize = value;
  26. for (int i = 0; i < control.Items.Count; i++)
  27. {
  28. var item = (System.Windows.Controls.TabItem)control.Items[i];
  29. item.Width = value.Width;
  30. item.Height = value.Height;
  31. }
  32. }
  33. }
  34. public bool Multiline { get; set; } // TODO? it's multiline by default
  35. public TabSizeMode SizeMode { get; set; }
  36. public int SelectedIndex
  37. {
  38. get => control.SelectedIndex;
  39. set => control.SelectedIndex = value;
  40. }
  41. public TabPage SelectedTab
  42. {
  43. get => ((System.Windows.Controls.TabItem)control.SelectedItem)?.Tag as TabPage;
  44. set => control.SelectedItem = value.control;
  45. }
  46. public event TabControlCancelEventHandler Selecting;
  47. protected virtual void OnSelecting(TabControlCancelEventArgs e) => Selecting?.Invoke(this, e);
  48. internal override void AddChild(Control child)
  49. {
  50. if (child is not TabPage)
  51. throw new ArgumentException("child must be of TabPage type");
  52. control.Items.Add(child.control);
  53. }
  54. internal override void SetChildIndex(Control child, int index)
  55. {
  56. control.Items.Insert(index, child.control);
  57. }
  58. internal override void RemoveChild(Control child)
  59. {
  60. control.Items.Remove(child.control);
  61. }
  62. public TabControl()
  63. {
  64. control = new();
  65. SetControl(control);
  66. TabPages = new TabPageCollection(Controls);
  67. int prevIndex = 0;
  68. control.SelectionChanged += (sender, e) =>
  69. {
  70. var args = new TabControlCancelEventArgs(SelectedTab, SelectedIndex, false, TabControlAction.Selecting);
  71. OnSelecting(args);
  72. if (args.Cancel)
  73. {
  74. if (SelectedIndex != prevIndex)
  75. SelectedIndex = prevIndex;
  76. e.Handled = true;
  77. }
  78. else
  79. {
  80. prevIndex = SelectedIndex;
  81. }
  82. };
  83. BackColor = System.Drawing.SystemColors.ControlLightLight;
  84. }
  85. }
  86. }