ایجاد TabControl عمودی در سی شارپ
برای ایجاد TabControl عمودی در سی شارپ ابتدا یک کنترل TebControl بر روی فرم قرار داده و سپس با زدن دکمه F7 به محیط کدنویسی رفته و کدهای زیر را بنویسید :
1: using System; 2: using System.Drawing; 3: using System.Windows.Forms; 4: 5: namespace WindowsFormsApp16 6: { 7: public partial class Form1 : Form 8: { 9: public Form1() 10: { 11: InitializeComponent(); 12: 13: tabControl1.Alignment = TabAlignment.Right; 14: tabControl1.SizeMode = TabSizeMode.FillToRight; 15: tabControl1.ItemSize = new Size(25, 100); 16: tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed; 17: 18: tabControl1.DrawItem += new DrawItemEventHandler(tabControl1_DrawItem); 19: } 20: 21: private void tabControl1_DrawItem(Object sender, System.Windows.Forms.DrawItemEventArgs e) 22: { 23: Graphics g = e.Graphics; 24: Brush _textBrush; 25: 26: // Get the item from the collection. 27: TabPage _tabPage = tabControl1.TabPages[e.Index]; 28: 29: // Get the real bounds for the tab rectangle. 30: Rectangle _tabBounds = tabControl1.GetTabRect(e.Index); 31: 32: if (e.State == DrawItemState.Selected) 33: { 34: 35: // Draw a different background color, and don't paint a focus rectangle. 36: _textBrush = new SolidBrush(Color.White); 37: g.FillRectangle(Brushes.LightGreen, e.Bounds); 38: } 39: else 40: { 41: _textBrush = new System.Drawing.SolidBrush(e.ForeColor); 42: e.DrawBackground(); 43: } 44: 45: // Use our own font. 46: Font _tabFont = new Font("Arial", (float)10.0, FontStyle.Bold, GraphicsUnit.Pixel); 47: 48: // Draw string. Center the text. 49: StringFormat _stringFlags = new StringFormat(); 50: _stringFlags.Alignment = StringAlignment.Center; 51: _stringFlags.LineAlignment = StringAlignment.Center; 52: g.DrawString(_tabPage.Text, _tabFont, _textBrush, _tabBounds, new StringFormat(_stringFlags)); 53: } 54: } 55: }
هر چند خطوط 18-13 کد بالا Tab ها را به صورت عمودی در می آورد ولی برای زیبا سازی Tab هاا می توانید کدهای خطوط 53-21 را هم اضافه کنید. برنامه را اجرا و نتیجه را مشاهده کنید: