Selasa, 20 November 2012

Membuat User Control Progress Bar

Progress Bar sering kali digunakan dalam berbagai aplikasi terutama dalam proses yang memerlukan waktu sedikit lama.Sehingga user dapat membedakan bahwa proses sedang berjalan. Pembahasan kali ini saya akan membuat User Control Progress Bar. Pertama kali kita buat User Control dan beri nama ProgreswsBar, kemudian masukkan Progress Bar dan Label seperti gambar dibawah ini :


progress Bar diberi nama : bar
label diberi nama : lblProgress


kemudian ketikan list program dibawah ini :


    Inherits System.Windows.Forms.UserControl

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()
        InitializeComponent()
    End Sub

    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    Private components As System.ComponentModel.IContainer

    Friend WithEvents Bar As System.Windows.Forms.ProgressBar
    Friend WithEvents lblProgress As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Bar = New System.Windows.Forms.ProgressBar
        Me.lblProgress = New System.Windows.Forms.Label
        Me.SuspendLayout()
        Me.Bar.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.Bar.Location = New System.Drawing.Point(4, 8)
        Me.Bar.Name = "Bar"
        Me.Bar.Size = New System.Drawing.Size(154, 23)
        Me.Bar.TabIndex = 0       
        Me.lblProgress.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.lblProgress.Location = New System.Drawing.Point(5, 31)
        Me.lblProgress.Name = "lblProgress"
        Me.lblProgress.Size = New System.Drawing.Size(152, 16)
        Me.lblProgress.TabIndex = 1
        Me.lblProgress.Text = "0% Done"
        Me.lblProgress.TextAlign = System.Drawing.ContentAlignment.TopCenter
        Me.Controls.Add(Me.lblProgress)
        Me.Controls.Add(Me.Bar)
        Me.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Name = "Progress"
        Me.Size = New System.Drawing.Size(164, 49)
        Me.ResumeLayout(False)

    End Sub

#End Region

    Property Value() As Integer
        Get
            Return Bar.Value
        End Get
        Set(ByVal Value As Integer)
            Bar.Value = Value
            UpdateLabel()
        End Set
    End Property

    Property Maximum() As Integer
        Get
            Return Bar.Maximum
        End Get
        Set(ByVal Value As Integer)
            Bar.Maximum = Value
        End Set
    End Property

    Property [Step]() As Integer
        Get
            Return Bar.Step
        End Get
        Set(ByVal Value As Integer)
            Bar.Step = Value
        End Set
    End Property

    Public Sub PerformStep()
        Bar.PerformStep()
        UpdateLabel()
    End Sub

    Private Sub UpdateLabel()
        lblProgress.Text = ((Bar.Value * 100) \ Bar.Maximum).ToString() & "% Done"
    End Sub

setelah selesai kita akan panggil dalam WinForm. Buat sebuah Form dengan nama FrmBar. Masukkan Timer beri nama tmrIncrementBar. masukkan juga User Control yang sudah dibuat ProgressBar kedalam Form. Dalam Propertis Form ubah Control Box menjadi False, Form Border Style -->FixedToolWindow. Skema UI nya akan seperti gambar dibawah ini :








ketik list prog sbb :

Private Sub LoadProgressBar()
        status.Visible = True

        tmrIncrementBar.Enabled = False

        status.Value = 0
        status.Maximum = 100
        status.Step = 1

        tmrIncrementBar.Enabled = True
End Sub

Private Sub tmrIncrementBar_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrIncrementBar.Tick
        Dim sJum As Double

        sJum = status.Value

        status.PerformStep()
        If status.Value = 10 Then
            Call ---Prosedur 1
        ElseIf status.Value = 30 Then
            Call ---Prosedur 2
        ElseIf status.Value = 50 Then
            Call ---Prosedur 3
        ElseIf status.Value = 70 Then
            Call ---Prosedur 4
        ElseIf status.Value = 90 Then
            Call ---Prosedur 5
        ElseIf status.Value = status.Maximum Then
            tmrIncrementBar.Enabled = False
            status.Visible = False
            Close()
        End If
End Sub

dalam load form panggil prosedur LoadProgressBar
     call LoadProgressBar()


demikian pembuatan User Control Progress Bar. Silahkan untuk mencobanya...