Click here to Skip to main content
15,881,413 members
Articles / Desktop Programming / Windows Forms

ZipTrack

Rate me:
Please Sign up or sign in to vote.
4.86/5 (28 votes)
2 Dec 2009CPOL5 min read 58.9K   781   78  
A comprehensive way to manage all of your downloaded zip files.
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Windows.Forms.Design

Imports ZipTrack.Gui.Components

Namespace Gui.Designers

    Public Class TabStripDesigner
        Inherits ParentControlDesigner
        Private changeService As IComponentChangeService

        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            RemoveHandler changeService.ComponentRemoving, AddressOf OnRemoving
            MyBase.Dispose(disposing)
        End Sub

        Protected Overrides Function GetHitTest(ByVal point As Point) As Boolean
            Dim result As HitTestResult = Me.Control.HitTest(point)
            If (result <> HitTestResult.CloseButton) AndAlso (result <> HitTestResult.MenuGlyph) Then
                Return False
            End If
            Return True
        End Function

        Public Overrides Sub Initialize(ByVal component As IComponent)
            MyBase.Initialize(component)
            Me.changeService = CType(Me.GetService(GetType(IComponentChangeService)), IComponentChangeService)
            AddHandler changeService.ComponentRemoving, AddressOf OnRemoving
            Me.Verbs.Add(New DesignerVerb("Add TabStrip", New EventHandler(AddressOf Me.OnAddTabStrip)))
            Me.Verbs.Add(New DesignerVerb("Remove TabStrip", New EventHandler(AddressOf Me.OnRemoveTabStrip)))
        End Sub

        Private Sub OnAddTabStrip(ByVal sender As Object, ByVal e As System.EventArgs)
            Dim service As IDesignerHost = CType(Me.GetService(GetType(IDesignerHost)), IDesignerHost)
            Dim transaction As DesignerTransaction = service.CreateTransaction("Add TabStrip")
            Dim tabItem As TabStripItem = CType(service.CreateComponent(GetType(TabStripItem)), TabStripItem)
            Me.changeService.OnComponentChanging(Me.Control, Nothing)
            Me.Control.AddTab(tabItem)
            tabItem.Title = "TabStrip Page " & ((Me.Control.Items.IndexOf(tabItem) + 1)).ToString()
            Me.Control.SelectItem(tabItem)
            Me.changeService.OnComponentChanged(Me.Control, Nothing, Nothing, Nothing)
            transaction.Commit()
        End Sub

        Private Sub OnRemoveTabStrip(ByVal sender As Object, ByVal e As System.EventArgs)
            Dim transaction As DesignerTransaction = (CType(Me.GetService(GetType(IDesignerHost)), IDesignerHost)).CreateTransaction("Remove Button")
            Me.changeService.OnComponentChanging(Me.Control, Nothing)
            Dim tabItem As TabStripItem = Me.Control.Items(Me.Control.Items.Count - 1)
            Me.Control.UnSelectItem(tabItem)
            Me.Control.Items.Remove(tabItem)
            Me.changeService.OnComponentChanged(Me.Control, Nothing, Nothing, Nothing)
            transaction.Commit()
        End Sub

        Private Sub OnRemoving(ByVal sender As Object, ByVal e As ComponentEventArgs)
            Dim service As IDesignerHost = CType(Me.GetService(GetType(IDesignerHost)), IDesignerHost)
            If TypeOf e.Component Is TabStripItem Then
                Dim component As TabStripItem = TryCast(e.Component, TabStripItem)
                If Me.Control.Items.Contains(component) Then
                    Me.changeService.OnComponentChanging(Me.Control, Nothing)
                    Me.Control.RemoveTab(component)
                    Me.changeService.OnComponentChanged(Me.Control, Nothing, Nothing, Nothing)
                    Return
                End If
            End If
            If TypeOf e.Component Is TabStrip Then
                For i As Integer = Me.Control.Items.Count - 1 To 0 Step -1
                    Dim tabItem As TabStripItem = Me.Control.Items(i)
                    Me.changeService.OnComponentChanging(Me.Control, Nothing)
                    Me.Control.RemoveTab(tabItem)
                    service.DestroyComponent(tabItem)
                    Me.changeService.OnComponentChanged(Me.Control, Nothing, Nothing, Nothing)
                Next i
            End If
        End Sub

        Protected Overrides Sub PreFilterProperties(ByVal properties As IDictionary)
            MyBase.PreFilterProperties(properties)
            properties.Remove("DockPadding")
            properties.Remove("DrawGrid")
            properties.Remove("Margin")
            properties.Remove("Padding")
            properties.Remove("BorderStyle")
            properties.Remove("ForeColor")
            properties.Remove("BackColor")
            properties.Remove("BackgroundImage")
            properties.Remove("BackgroundImageLayout")
            properties.Remove("GridSize")
            properties.Remove("ImeMode")
            properties.Remove("Font")
        End Sub

        Protected Overrides Sub WndProc(ByRef msg As Message)
            If msg.Msg = &H201 Then
                Dim pt As Point = Me.Control.PointToClient(Cursor.Position)
                Dim tabItemByPoint As TabStripItem = Me.Control.GetTabItemByPoint(pt)
                If tabItemByPoint IsNot Nothing Then
                    Me.Control.SelectedItem = tabItemByPoint
                    Dim components As New ArrayList()
                    components.Add(tabItemByPoint)
                    CType(Me.GetService(GetType(ISelectionService)), ISelectionService).SetSelectedComponents(components)
                End If
            End If
            MyBase.WndProc(msg)
        End Sub

        Public Overrides ReadOnly Property AssociatedComponents() As ICollection
            Get
                Return Me.Control.Items
            End Get
        End Property

        Public Overridable Overloads ReadOnly Property Control() As TabStrip
            Get
                Return (TryCast(MyBase.Control, TabStrip))
            End Get
        End Property
    End Class
End Namespace

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder Arkitech EBC Corporation
United States United States
MS, BBA, software developer, consultant, and trainer. Specializing in building data-centric applications designed for business, university, community & faith based organizations. Started developing Excel VBA macros and never looked back. Freelance developer utilizing VB.Net, SQL Server, Microsoft Access, and ASP.Net.

Comments and Discussions