locked
How to customize a control? RRS feed

  • Question

  • Hi!

    I want to know how can customize a control for example add a close button into a tab control like this pic :

    (excuse me if picture is not good)

    Thursday, December 5, 2013 8:01 AM

Answers

  • 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)

    • Edited by Rbie Thursday, December 5, 2013 8:42 AM
    • Proposed as answer by Carl Cai Friday, December 6, 2013 8:06 AM
    • Marked as answer by Carl Cai Saturday, December 14, 2013 3:23 AM
    Thursday, December 5, 2013 8:38 AM