שתף באמצעות


TabControl with close(x) button icon

Question

Wednesday, January 15, 2014 5:22 AM

Hi guys,

I aiming to create my project, so every form made of i, is dock at the tabcontrol, but i when i close the tab i use to visible=false, so probably i came make into close button besides the name like this:

Thanks

All replies (3)

Wednesday, January 15, 2014 8:50 AM ✅Answered

if windows forms

 then try

this is one way (set drawmode to Ownerdrawfixed)

 Private Sub TabControl1_DrawItem(sender As Object, e As DrawItemEventArgs) Handles TabControl1.DrawItem
        e.Graphics.DrawString("x", e.Font, Brushes.Black, e.Bounds.Right - 15, e.Bounds.Top + 4)
        e.Graphics.DrawString(TabControl1.TabPages(e.Index).Text, e.Font, Brushes.Black, e.Bounds.Left + 12, e.Bounds.Top + 4)
        e.DrawFocusRectangle()
    End Sub

    Private Sub TabControl1_MouseDown(sender As Object, e As MouseEventArgs) Handles TabControl1.MouseDown
        For i As Integer = 0 To TabControl1.TabPages.Count - 1
            Dim r As Rectangle = TabControl1.GetTabRect(i)
            Dim closeButton As Rectangle = New Rectangle(r.Right - 15, r.Top + 4, 9, 7)
            If closeButton.Contains(e.Location) Then
                If MessageBox.Show("Close form?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
                    TabControl1.TabPages.RemoveAt(i)
                    Exit Sub
                End If
            End If
        Next
    End Sub

if you want a picture instead of a string x, replace the drawstring with drawimage (you might need to adjust the location a little in that case)

      

        e.Graphics.DrawImage(yourimage, e.Bounds.Right - 15, e.Bounds.Top + 4)


Wednesday, January 15, 2014 8:34 AM

You didn't mention the framework you're using. If it's WPF, here's a link to a solution:

http://social.msdn.microsoft.com/Forums/vstudio/en-us/75a2c1e8-5148-45fb-bd37-21d120dac60d/tabitem-with-text-and-close-button?forum=wpf

Thomas Claudius Huber

"If you can't make your app run faster, make it at least look & feel extremly fast"

My latest app: The "Womanizer" :-)
twitter: @thomasclaudiush
homepage: www.thomasclaudiushuber.com
author of: ultimate Windows Store Apps handbook | ultimate WPF handbook | ultimate Silverlight handbook


Thursday, January 16, 2014 12:41 AM

*Thomas - I'm using vb .net winform