Symbianize Forum

Most of our features and services are available only to members, so we encourage you to login or register a new account. Registration is free, fast and simple. You only need to provide a valid email. Being a member you'll gain access to all member forums and features, post a message to ask question or provide answer, and share or find resources related to mobile phones, tablets, computers, game consoles, and multimedia.

All that and more, so what are you waiting for, click the register button and join us now! Ito ang website na ginawa ng pinoy para sa pinoy!

[TUTORIAL] TabControl with Close button in VB.NET

CodingSource

The Loyalist
Advanced Member
Messages
514
Reaction score
0
Points
26
Maraming pinaggagamitan ang tab. Pero alam mo ba na posible ang pagdagdag ang mga features nito para mas may pakinabang (kaysa pupunta ka pa sa Google para mag-download ng theme)?

Puwes, kung ang nasa isip mo ay gagawin mo ito para sa paggawa ng browser (o kahit ano pa man) puwedeng-puwede ito. Hindi ito isang sagabal kundi makakatulong pa para mas mag-mukhang advanced ang program mo.

SKILL LEVEL: Intermediate
HOW LONG: 5 minutes
PROGRAM: Visual Basic 2008 (pero puwede mo itong gamitin sa mga higher version)
TEMPLATE: Windows Application

INSTRUCTIONS:
1. Sa Solution Explorer, i-right click ang pangalan ng project -> Add -> Class.
2. I-pangalan mo ito ng kahit ano. Pinangalan ko itong "TabControlYZ".
3. I-copy paste mo ang code na nailagay ko rito.
4. Balik ulit sa Solution Explorer, i-right click ang pangalan ng project -> Rebuild.
5. Punta ka sa Toolbox panel at i-drag and drop ang control.
6. Pagkatapos na ginawa mo na ang gusto mo, press F5 to run the project.

Code:
Option Strict On
Imports System.ComponentModel
 
<ToolboxBitmap(GetType(System.Windows.Forms.TabControl))> _
Public Class TabControlEx
    Inherits TabControl
 
    Private Declare Auto Function SetParent Lib "user32" (ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As IntPtr
    Protected CloseButtonCollection As New Dictionary(Of Button, TabPage)
    Private _ShowCloseButtonOnTabs As Boolean = True
 
    <Browsable(True), DefaultValue(True), Category("Behavior"), Description("Indicates whether a close button should be shown on each TabPage")> _
    Public Property ShowCloseButtonOnTabs() As Boolean
        Get
            Return _ShowCloseButtonOnTabs
        End Get
        Set(ByVal value As Boolean)
            _ShowCloseButtonOnTabs = value
            For Each btn In CloseButtonCollection.Keys
                btn.Visible = _ShowCloseButtonOnTabs
            Next
            RePositionCloseButtons()
        End Set
    End Property
 
    Protected Overrides Sub OnCreateControl()
        MyBase.OnCreateControl()
        RePositionCloseButtons()
    End Sub
 
    Protected Overrides Sub OnControlAdded(ByVal e As System.Windows.Forms.ControlEventArgs)
        MyBase.OnControlAdded(e)
        Dim tp As TabPage = DirectCast(e.Control, TabPage)
        Dim rect As Rectangle = Me.GetTabRect(Me.TabPages.IndexOf(tp))
        Dim btn As Button = AddCloseButton(tp)
        btn.Size = New Size(rect.Height - 1, rect.Height - 1)
        btn.Location = New Point(rect.X + rect.Width - rect.Height - 1, rect.Y + 1)
        SetParent(btn.Handle, Me.Handle)
        AddHandler btn.Click, AddressOf OnCloseButtonClick
        CloseButtonCollection.Add(btn, tp)
    End Sub
 
    Protected Overrides Sub OnControlRemoved(ByVal e As System.Windows.Forms.ControlEventArgs)
        Dim btn As Button = CloseButtonOfTabPage(DirectCast(e.Control, TabPage))
        RemoveHandler btn.Click, AddressOf OnCloseButtonClick
        CloseButtonCollection.Remove(btn)
        SetParent(btn.Handle, Nothing)
        btn.Dispose()
        MyBase.OnControlRemoved(e)
    End Sub
 
    Protected Overrides Sub OnLayout(ByVal levent As System.Windows.Forms.LayoutEventArgs)
        MyBase.OnLayout(levent)
        RePositionCloseButtons()
    End Sub
 
    Public Event CloseButtonClick As CancelEventHandler
    Protected Overridable Sub OnCloseButtonClick(ByVal sender As Object, ByVal e As EventArgs)
        If Not DesignMode Then
            Dim btn As Button = DirectCast(sender, Button)
            Dim tp As TabPage = CloseButtonCollection(btn)
            Dim ee As New CancelEventArgs
            RaiseEvent CloseButtonClick(sender, ee)
            If Not ee.Cancel Then
                Me.TabPages.Remove(tp)
                RePositionCloseButtons()
            End If
        End If
    End Sub
 
    Protected Overridable Function AddCloseButton(ByVal tp As TabPage) As Button
        Dim closeButton As New Button
        With closeButton
            '' TODO: Give a good visual appearance to the Close button, maybe by assigning images etc.
            ''       Here I have not used images to keep things simple.
            .Text = "X"
            .FlatStyle = FlatStyle.Flat
            .BackColor = Color.FromKnownColor(KnownColor.ButtonFace)
            .ForeColor = Color.White
            .Font = New Font("Microsoft Sans Serif", 6, FontStyle.Bold)
        End With
        Return closeButton
    End Function
 
    Public Sub RePositionCloseButtons()
        For Each item In CloseButtonCollection
            RePositionCloseButtons(item.Value)
        Next
    End Sub
 
    Public Sub RePositionCloseButtons(ByVal tp As TabPage)
        Dim btn As Button = CloseButtonOfTabPage(tp)
        If btn IsNot Nothing Then
            Dim tpIndex As Integer = Me.TabPages.IndexOf(tp)
            If tpIndex >= 0 Then
                Dim rect As Rectangle = Me.GetTabRect(tpIndex)
                If Me.SelectedTab Is tp Then
                    btn.BackColor = Color.Red
                    btn.Size = New Size(rect.Height - 1, rect.Height - 1)
                    btn.Location = New Point(rect.X + rect.Width - rect.Height, rect.Y + 1)
                Else
                    btn.BackColor = Color.FromKnownColor(KnownColor.ButtonFace)
                    btn.Size = New Size(rect.Height - 3, rect.Height - 3)
                    btn.Location = New Point(rect.X + rect.Width - rect.Height - 1, rect.Y + 1)
                End If
                btn.Visible = ShowCloseButtonOnTabs
                btn.BringToFront()
            End If
        End If
    End Sub
 
    Protected Function CloseButtonOfTabPage(ByVal tp As TabPage) As Button
        Return (From item In CloseButtonCollection Where item.Value Is tp Select item.Key).FirstOrDefault
    End Function
End Class

- Ang pagkakaiba nito sa TabControl ng VB.NET ay meron siyang ShowCloseButtonOnTabs kung saan puwede mong piliin kung ipapakita ba ang close button o hindi. Naka-default naman ito sa True na ibig sabihin ay makikita mo ang close button.
- Kapag i-cloclose mo na ang tab gamit ang close button nito, ginagamit mo ang CloseButtonClick event. Sa code nagsasara siya ng tab, pero puwede mo i-set ang e.Cancel sa True
Code:
e.Cancel = True
para hindi mo ma-close ang tab kapag pinipindut ito.
- Kapag hindi nagustuhan ang close button, i-modify ninyo ang "AddCloseButton" procedure para bigyan ito ng taste na gusto niyo.
- Kapag tinatakpan ng mga button ang text, puwede niyong subukan itong code na ito:
Code:
Protected Overrides Sub OnCreateControl()

        MyBase.OnCreateControl()

        For Each item In CloseButtonCollection

            item.Value.Text = item.Value.Text.Trim & Space(4)

        Next


        RePositionCloseButtons()

    End Sub
 
Give credits to source...

Well hindi ko naman sinabi na ako ang gumawa. Pinag-isipan ko lang na ipost dito ang tutorial na ito. Kaya ginawa ko ito para malaman naman ng mga ka-SB natin about this kasi hindi lahat sila gumagamit ng Google para mag-search ng mga bagay na ito. I didn't just simply "copy and paste" the article, per se. I created a little twist para ma-irelate ito sa mga ka-SB natin ;). I searched in the internet about this because I badly need it for the browser I'm building now, but I thought to re-post it here para makatulong naman sa ibang ka-SB natin.

The original link: http://www.vbforums.com/showthread....button-on-TabPages-(with-Design-Time-support)
 
Last edited:
Back
Top Bottom