VB.Net Progressbarklasse

Allgemeine Diskussionen über die .NET-Bibliothek

VB.Net Progressbarklasse

Beitragvon Andreas Vogt » Mi 19. Aug 2015, 12:24

Hallo,
habe eine Klasse in VB.Net erstellt zur Anzeige einer Progressbar im Stile der Visual Studio Setup/Update Screens:

Code: Alles auswählen
Public Class clsProgressbar

    Private m_size As Long = 500
    Private m_step As Double = 1
    Private m_max As Long = 100
    Private m_min As Long = 0
    Private m_factor As Double = 1
    Private m_left As Long = 0
    Private m_top As Long = 0
    Private m_location As Object = Nothing

    Private stepcount As Long = 0
    Private rec1 As New Label
    Private pbar As New Label

    Public Property Max() As Long
        Get
            Max = m_max
        End Get
        Set(value As Long)
            m_max = value
            factor = Size / (m_max - m_min)
        End Set
    End Property

    Public Property Min() As Long
        Get
            Min = m_min
        End Get
        Set(value As Long)
            m_min = value
            factor = Size / (m_max - m_min)
        End Set
    End Property

    Public Property Size As Long
        Get
            Size = m_size
        End Get
        Set(value As Long)
            m_size = value
            factor = Size / (m_max - m_min)
        End Set
    End Property

    Public Property StepWidth() As Double
        Get
            StepWidth = m_step
        End Get
        Set(value As Double)
            m_step = value
        End Set
    End Property

    Public Property Left As Long
        Get
            Left = m_left
        End Get
        Set(value As Long)
            m_left = value
        End Set
    End Property

    Public Property Top As Long
        Get
            Top = m_top
        End Get
        Set(value As Long)
            m_top = value
        End Set
    End Property

    Public Property Location As Object
        Get
            Location = m_location
        End Get
        Set(value As Object)
            m_location = value
        End Set
    End Property

    Private Property factor() As Double
        Get
            factor = m_factor
        End Get
        Set(value As Double)
            m_factor = value
        End Set
    End Property

    Public Sub performStep()
        stepcount = stepcount + 1
        pbar.Width = Int(stepcount * StepWidth * factor)
        Application.DoEvents()

        If pbar.Width = Size Then
            StopProgress
        End If
    End Sub

    Public Sub maximize()
        pbar.Width = Size
    End Sub

    Public Sub StopProgress()
        stepcount = 0
    End Sub

    Public Sub Reset()
        stepcount = 0
        pbar.Width = 0
    End Sub

    Public Sub Load()
        With rec1
            .Left = Left
            .Top = Top
            .Width = Size
            .Height = 9
            .BackColor = System.Drawing.Color.FromArgb(32, 77, 110)
        End With

        With pbar
            .AutoSize = False
            .Left = Left
            .Top = Top
            .Width = 0
            .Height = 9
            .BackColor = System.Drawing.Color.FromArgb(0, 152, 248)
        End With

        Location.Controls.Add(pbar)
        Location.Controls.Add(rec1)
        Application.DoEvents()
    End Sub
End Class


Beispiel-Verwendung in Formular/Panel:
Code: Alles auswählen
Public Class Form1
    Dim probar As clsProgressbar

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        probar = New clsProgressbar
        With probar
            .Location = Me.Panel1
            .Left = 20          'Location X
           .Top = 80           'Location Y
           .Min = 20           'default = 0
           .Max = 80           'default = 100
           .Size = 350         'default = 500; Width of Progressbar
           .StepWidth = 3   'default = 1
           .Load()              'draw the empty Progressbar
       End With
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim i As Long = 0
        For i = 1 To 60
            If i Mod 3 = 0 Then
                probar.performStep()
                Sleep(50)
            End If
        Next
    End Sub

    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        probar = Nothing
    End Sub
End Class


Vieleicht kann man den Code gebrauchen. Kommentare wie immer erwünscht.

Gruß Andreas
Andreas Vogt
Entwickler
 
Beiträge: 151
Registriert: Do 18. Mär 2010, 18:00
Wohnort: Offenburg
Accessversion: 2.0, 97, 2002, 2003, 2007, 2010
Access-Erfahrung: Fortgeschritten

Zurück zu Allgemeines

cron