Category / Section
How to show tooltip for TabPageAdv header in WinForms TabControlAdv?
4 mins read
Show tooltip for TabPageAdv header
We can draw the custom TabPageAdv header for customizing its appearance. Here we have drawn the custom close button and custom caption image based on TabPageAdv bounds. TabPageAdv header text is more than TabPageAdv bounds, it leads to overlap the custom close button. So, we have trimmed the header text and displayed the full text of the TabItem’s header in the Tooltip. The following code demonstrates the same.
C#
//Initialzie the Close button rectangle Rectangle closebutton; //Initializes the LinearGradientBrush. LinearGradientBrush gradientBrush; //To draw the custom Tabs this.tabControlAdv1.DrawItem += new DrawTabEventHandler(tabControlAdv1_DrawItem); //Set ActiveTabColor this.tabControlAdv1.ActiveTabColor = Color.FromArgb(192,255,192); //Set ActiveTabForeColor this.tabControlAdv1.ActiveTabForeColor = System.Drawing.Color.Black; //Set InActiveTabColor this.tabControlAdv1.InactiveTabColor = Color.FromArgb(255,128,128); //Set InActiveTabForeColor this.tabControlAdv1.InActiveTabForeColor = System.Drawing.Color.White; //For Close Button Click this.tabControlAdv1.MouseClick += TabControlAdv1_MouseClick; //For ToolTip this.tabControlAdv1.MouseMove += TabControlAdv1_MouseMove; //ToolTip private void TabControlAdv1_MouseMove(object sender, MouseEventArgs e) { for (int i = 0; i < this.tabControlAdv1.TabPages.Count; i++) { Rectangle r = this.tabControlAdv1.GetTabRect(i); if(r.Contains(e.Location)) { //Set Tooltip text when hovering the tabs toolTip1.SetToolTip(tabControlAdv1, this.tabControlAdv1.TabPages[i].Text); break; } } return; } //Close the Tab Page when clicking the close Button private void TabControlAdv1_MouseClick(object sender, MouseEventArgs e) { for (int i = 0; i < this.tabControlAdv1.TabPages.Count; i++) { Rectangle r = closebutton; if(closebutton.Contains(e.Location)) { //Close the Tab Page when clicking the close Button tabControlAdv1.TabPages.Remove(tabControlAdv1.SelectedTab); break; } } } void tabControlAdv1_DrawItem(object sender, DrawTabEventArgs drawItemInfo) { // The function GetTabRect helps to get the rectangle of the tab item. if(drawItemInfo.Index != this.tabControlAdv1.SelectedIndex) { // For the non-selected tabs. gradientBrush = new System.Drawing.Drawing2D.LinearGradientBrush(this.tabControlAdv1.GetTabRect(drawItemInfo.Index), tabControlAdv1.InactiveTabColor, tabControlAdv1.InactiveTabColor, LinearGradientMode.Horizontal); } else { // For the selected tab. gradientBrush = new System.Drawing.Drawing2D.LinearGradientBrush(this.tabControlAdv1.GetTabRect(drawItemInfo.Index), tabControlAdv1.ActiveTabColor, tabControlAdv1.ActiveTabColor, LinearGradientMode.Horizontal); } float[] positions = { 0.0f, 0.05f, 0.95f, 1.0f }; float[] factors = { 0.4f, 1.0f, 0.05f, 0.04f }; // Blends settings. Blend blend = new Blend(); blend.Factors = factors; blend.Positions = positions; gradientBrush.Blend = blend; drawItemInfo.Graphics.FillRectangle(gradientBrush, this.tabControlAdv1.GetTabRect(drawItemInfo.Index)); gradientBrush.Dispose(); // Draw the default borders and interior (text and image) drawItemInfo.DrawBorders(); // To draw Image drawItemInfo.Graphics.DrawImage(this.tabControlAdv1.TabPages[drawItemInfo.Index].Image, new Rectangle(drawItemInfo.Bounds.X + 5, drawItemInfo.Bounds.Y + 5, this.tabControlAdv1.TabPages[drawItemInfo.Index].ImageSize.Width, this.tabControlAdv1.TabPages[drawItemInfo.Index].ImageSize.Height)); //To draw the text of the Tab item if(drawItemInfo.Index != this.tabControlAdv1.SelectedIndex) { // For the non-selected tabs. Size size = TextRenderer.MeasureText(this.tabControlAdv1.TabPages[drawItemInfo.Index].Text, tabControlAdv1.ActiveTabFont); StringFormat format = new StringFormat(); format.Trimming = StringTrimming.EllipsisCharacter; TextRenderer.DrawText(drawItemInfo.Graphics, this.tabControlAdv1.TabPages[drawItemInfo.Index].Text, this.tabControlAdv1.TabPages[drawItemInfo.Index].Font , new Rectangle(drawItemInfo.Bounds.X + 25, drawItemInfo.Bounds.Y + 7, drawItemInfo.Bounds.Width - 40, drawItemInfo.Bounds.Height), Color.Black, TextFormatFlags.EndEllipsis); } else { // For the selected tab. Size size = TextRenderer.MeasureText(this.tabControlAdv1.TabPages[drawItemInfo.Index].Text, tabControlAdv1.ActiveTabFont); StringFormat format = new StringFormat(); format.Trimming = StringTrimming.Character; TextRenderer.DrawText(drawItemInfo.Graphics, this.tabControlAdv1.TabPages[drawItemInfo.Index].Text, this.tabControlAdv1.TabPages[drawItemInfo.Index].Font , new Rectangle(drawItemInfo.Bounds.X + 25, drawItemInfo.Bounds.Y + 7, drawItemInfo.Bounds.Width - 40, drawItemInfo.Bounds.Height), Color.Black, TextFormatFlags.EndEllipsis); } //For Close Button closebutton = new Rectangle(drawItemInfo.Bounds.X + drawItemInfo.Bounds.Width - 20, drawItemInfo.Bounds.Y + 7, 13, 13); //To draw the close button rectangle drawItemInfo.Graphics.FillRectangle(Brushes.Red, closebutton); // To draw "X" in Red color rectangle drawItemInfo.Graphics.DrawLine(new Pen(Brushes.White), new Point(closebutton.X + 4, closebutton.Y + 4), new Point(closebutton.X + closebutton.Width - 4, closebutton.Y + closebutton.Height - 4)); drawItemInfo.Graphics.DrawLine(new Pen(Brushes.White), new Point(closebutton.X + 4, closebutton.Y - 4 + closebutton.Height), new Point(closebutton.X + closebutton.Width - 4, closebutton.Y + 4)); }
VB
'Initialzie the Close button rectangle Private closebutton As Rectangle 'Initializes the LinearGradientBrush. Private gradientBrush As LinearGradientBrush 'To draw the custom Tabs AddHandler tabControlAdv1.DrawItem, AddressOf tabControlAdv1_DrawItem 'Set ActiveTabColor Me.tabControlAdv1.ActiveTabColor = Color.FromArgb(192,255,192) 'Set ActiveTabForeColor Me.tabControlAdv1.ActiveTabForeColor = System.Drawing.Color.Black 'Set InActiveTabColor Me.tabControlAdv1.InactiveTabColor = Color.FromArgb(255,128,128) 'Set InActiveTabForeColor Me.tabControlAdv1.InActiveTabForeColor = System.Drawing.Color.White 'Set TabStyle Me.tabControlAdv1.TabStyle = GetType(Syncfusion.Windows.Forms.Tools.TabRendererIE7) 'For Close Button Click AddHandler Me.tabControlAdv1.MouseClick, AddressOf TabControlAdv1_MouseClick 'For ToolTip AddHandler Me.tabControlAdv1.MouseMove, AddressOf TabControlAdv1_MouseMove 'ToolTip Private Sub TabControlAdv1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) For i As Integer = 0 To Me.tabControlAdv1.TabPages.Count – 1 Dim r As Rectangle = Me.tabControlAdv1.GetTabRect(i) If r.Contains(e.Location) Then 'Set Toolip when hovering the tabs toolTip1.SetToolTip(tabControlAdv1, Me.tabControlAdv1.TabPages(i).Text) Exit For End If Next i Return End Sub 'Close the Tab Page when clicking the close Button Private Sub TabControlAdv1_MouseClick(ByVal sender As Object, ByVal e As MouseEventArgs) For i As Integer = 0 To Me.tabControlAdv1.TabPages.Count – 1 Dim r As Rectangle = closebutton If closebutton.Contains(e.Location) Then 'Close the Tab Page when clicking the close Button tabControlAdv1.TabPages.Remove(tabControlAdv1.SelectedTab) Exit For End If Next i End Sub Private Sub tabControlAdv1_DrawItem(ByVal sender As Object, ByVal drawItemInfo As DrawTabEventArgs) ' The function GetTabRect helps to get the rectangle of the tab item. If drawItemInfo.Index <> Me.tabControlAdv1.SelectedIndex Then ' For the non-selected tabs. gradientBrush = New System.Drawing.Drawing2D.LinearGradientBrush(Me.tabControlAdv1.GetTabRect(drawItemInfo.Index), tabControlAdv1.InactiveTabColor, tabControlAdv1.InactiveTabColor, LinearGradientMode.Horizontal) Else ' For the selected tab. gradientBrush = New System.Drawing.Drawing2D.LinearGradientBrush(Me.tabControlAdv1.GetTabRect(drawItemInfo.Index), tabControlAdv1.ActiveTabColor, tabControlAdv1.ActiveTabColor, LinearGradientMode.Horizontal) End If Dim positions() As Single = { 0.0F, 0.05F, 0.95F, 1.0F } Dim factors() As Single = { 0.4F, 1.0F, 0.05F, 0.04F } ' Blends settings. Dim blend As New Blend() blend.Factors = factors blend.Positions = positions gradientBrush.Blend = blend drawItemInfo.Graphics.FillRectangle(gradientBrush, Me.tabControlAdv1.GetTabRect(drawItemInfo.Index)) gradientBrush.Dispose() ' Draw the default borders and interior (text and image) drawItemInfo.DrawBorders() ' To draw Image drawItemInfo.Graphics.DrawImage(Me.tabControlAdv1.TabPages(drawItemInfo.Index).Image, New Rectangle(drawItemInfo.Bounds.X + 5, drawItemInfo.Bounds.Y + 5, Me.tabControlAdv1.TabPages(drawItemInfo.Index).ImageSize.Width, Me.tabControlAdv1.TabPages(drawItemInfo.Index).ImageSize.Height)) 'To draw the text of the Tab item If drawItemInfo.Index <> Me.tabControlAdv1.SelectedIndex Then ' For the non-selected tabs. Dim size_Renamed As Size = TextRenderer.MeasureText(Me.tabControlAdv1.TabPages(drawItemInfo.Index).Text, tabControlAdv1.ActiveTabFont) Dim format As New StringFormat() format.Trimming = StringTrimming.EllipsisCharacter TextRenderer.DrawText(drawItemInfo.Graphics, Me.tabControlAdv1.TabPages(drawItemInfo.Index).Text, Me.tabControlAdv1.TabPages(drawItemInfo.Index).Font, New Rectangle(drawItemInfo.Bounds.X + 25, drawItemInfo.Bounds.Y + 7, drawItemInfo.Bounds.Width - 40, drawItemInfo.Bounds.Height), Color.Black, TextFormatFlags.EndEllipsis) Else ' For the selected tab. Dim size_Renamed As Size = TextRenderer.MeasureText(Me.tabControlAdv1.TabPages(drawItemInfo.Index).Text, tabControlAdv1.ActiveTabFont) Dim format As New StringFormat() format.Trimming = StringTrimming.Character TextRenderer.DrawText(drawItemInfo.Graphics, Me.tabControlAdv1.TabPages(drawItemInfo.Index).Text, Me.tabControlAdv1.TabPages(drawItemInfo.Index).Font, New Rectangle(drawItemInfo.Bounds.X + 25, drawItemInfo.Bounds.Y + 7, drawItemInfo.Bounds.Width - 40, drawItemInfo.Bounds.Height), Color.Black, TextFormatFlags.EndEllipsis) End If 'For Close Button closebutton = New Rectangle(drawItemInfo.Bounds.X + drawItemInfo.Bounds.Width - 20, drawItemInfo.Bounds.Y + 7, 13, 13) 'To draw the close button rectangle drawItemInfo.Graphics.FillRectangle(Brushes.Red, closebutton) ' To draw "X" in Red color rectangle drawItemInfo.Graphics.DrawLine(New Pen(Brushes.White), New Point(closebutton.X + 4, closebutton.Y + 4), New Point(closebutton.X + closebutton.Width - 4, closebutton.Y + closebutton.Height - 4)) drawItemInfo.Graphics.DrawLine(New Pen(Brushes.White), New Point(closebutton.X + 4, closebutton.Y - 4 + closebutton.Height), New Point(closebutton.X + closebutton.Width - 4, closebutton.Y + 4)) End Sub
Screenshot
Samples: