All pastes #526801 Raw Edit

lord2800

public text v1 · immutable
#526801 ·published 2007-05-31 23:50 UTC
rendered paste body
Index: DraggableTabControl.cs
===================================================================
--- DraggableTabControl.cs	(revision 24)
+++ DraggableTabControl.cs	(working copy)
@@ -12,20 +12,40 @@
 	/// 
 	public class DraggableTabControl : System.Windows.Forms.TabControl
 	{
-		///  
+		/// <summary>
 		/// Required designer variable.
-		/// 
+		/// </summary>
 		private System.ComponentModel.Container components = null;
+		public event CancelEventHandler TabClosingEventHandler;
+		private TabPage closingTab;
+		public TabPage ClosingTab
+		{
+			get { return closingTab; }
+		}
+		protected void OnTabClosing()
+		{
+			CancelEventArgs e = new CancelEventArgs();
+			if(TabClosingEventHandler != null)
+				try {
+					TabClosingEventHandler.BeginInvoke(this, e, null, null);
+				} catch(Exception) {
+					throw;
+				}
+			if(!e.Cancel)
+				CloseTab();
+		}
 
-		public DraggableTabControl()
+		public DraggableTabControl() : base()
 		{
 			// This call is required by the Windows.Forms Form Designer.
 			InitializeComponent();
+			DrawMode = TabDrawMode.OwnerDrawFixed;
+			Padding = new Point(17,3);
 		}
 
-		///  
+		/// <summary>
 		/// Clean up any resources being used.
-		/// 
+		/// </summary>
 		protected override void Dispose(bool disposing)
 		{
 			if(disposing)
@@ -39,10 +59,10 @@
 		}
 
 		#region Component Designer generated code
-		///  
+		/// <summary>
 		/// Required method for Designer support - do not modify 
 		/// the contents of this method with the code editor.
-		/// 
+		/// </summary>
 		private void InitializeComponent()
 		{
 		}
@@ -104,7 +124,6 @@
                 e.Effect = DragDropEffects.None;
             }
         }
-
 		protected override void OnMouseDown(MouseEventArgs e)
 		{
 			base.OnMouseDown(e);
@@ -117,12 +136,37 @@
 				DoDragDrop(tp, DragDropEffects.All);
 			}
 		}
+		protected override void OnDrawItem(DrawItemEventArgs e)
+		{
+			e.Graphics.DrawLine(new Pen(Brushes.Black, 2f), new PointF(e.Bounds.Right-15, e.Bounds.Y-4), new PointF(e.Bounds.Right-15, e.Bounds.Bottom-4));
+			e.Graphics.DrawLine(new Pen(Brushes.Black, 2f), new PointF(e.Bounds.Right-15, e.Bounds.Bottom-4), new PointF(e.Bounds.Right-15, e.Bounds.Y-4));
+			e.Graphics.DrawString(TabPages[e.Index].Text, Font, Brushes.Black, new PointF(e.Bounds.X+4, e.Bounds.Y+2));
+			base.OnDrawItem(e);
+		}
+		protected override void OnMouseClick(MouseEventArgs e)
+		{
+			for(int i = 0; i < TabPages.Count; i++)
+			{
+				Rectangle r = GetTabRect(i);
+				if(r.Contains(e.Location))
+				{
+					closingTab = TabPages[i];
+					OnTabClosing();
+					closingTab = null;
+				}
+			}
+			base.OnMouseClick(e);
+		}
 
-		/// 
+		private void CloseTab()
+		{
+			TabPages.Remove(ClosingTab);
+		}
+		/// <summary>
 		/// Finds the TabPage whose tab is contains the given point.
-		/// 
-		/// The point (given in client coordinates) to look for a TabPage.
-		/// The TabPage whose tab is at the given point (null if there isn't one).
+		/// </summary>
+		/// <param name="pt">The point (given in client coordinates) to look for a TabPage.</param>
+		/// <returns>The TabPage whose tab is at the given point (null if there isn't one).</returns>
 		private TabPage GetTabPageByTab(Point pt)
 		{
 			TabPage tp =
@@ -142,11 +186,11 @@
 			return tp;
 		}
 
-		/// 
+		/// <summary>
 		/// Loops over all the TabPages to find the index of the given TabPage.
-		/// 
-		/// The TabPage we want the index for.
-		/// The index of the given TabPage(-1 if it isn't found.)
+		/// </summary>
+		/// <param name="page">The TabPage we want the index for.</param>
+		/// <returns>The index of the given TabPage(-1 if it isn't found.)</returns>
 		private int FindIndex(TabPage page)
 		{
 			for(int i = 0; i < TabPages.Count; i++)