VB.Net/GUI/Progress Bar

Материал из VB Эксперт
Перейти к: навигация, поиск

Prograss Bar Demo

<source lang="vbnet"> Imports System.Windows.Forms Imports System.IO

Module Module1

   Sub Main()
       Application.Run(New Form1)
   End Sub

End Module

Public Class Form1

   Inherits System.Windows.Forms.Form
  1. Region " Windows Form Designer generated code "
   Public Sub New()
       MyBase.New()
       "This call is required by the Windows Form Designer.
       InitializeComponent()
       "Add any initialization after the InitializeComponent() call
   End Sub
   "Form overrides dispose to clean up the component list.
   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
   "Required by the Windows Form Designer
   Private components As System.ruponentModel.IContainer
   "NOTE: The following procedure is required by the Windows Form Designer
   "It can be modified using the Windows Form Designer.  
   "Do not modify it using the code editor.
   Friend WithEvents ProgressBar1 As System.Windows.Forms.ProgressBar
   Friend WithEvents Button1 As System.Windows.Forms.Button
   Friend WithEvents StatusBar1 As System.Windows.Forms.StatusBar
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       Me.ProgressBar1 = New System.Windows.Forms.ProgressBar()
       Me.Button1 = New System.Windows.Forms.Button()
       Me.StatusBar1 = New System.Windows.Forms.StatusBar()
       Me.SuspendLayout()
       "
       "ProgressBar1
       "
       Me.ProgressBar1.Location = New System.Drawing.Point(24, 160)
       Me.ProgressBar1.Name = "ProgressBar1"
       Me.ProgressBar1.Size = New System.Drawing.Size(408, 23)
       Me.ProgressBar1.TabIndex = 3
       "
       "Button1
       "
       Me.Button1.Location = New System.Drawing.Point(328, 32)
       Me.Button1.Name = "Button1"
       Me.Button1.Size = New System.Drawing.Size(112, 23)
       Me.Button1.TabIndex = 4
       Me.Button1.Text = "Calculate"
       "
       "StatusBar1
       "
       Me.StatusBar1.Location = New System.Drawing.Point(0, 207)
       Me.StatusBar1.Name = "StatusBar1"
       Me.StatusBar1.Size = New System.Drawing.Size(456, 22)
       Me.StatusBar1.TabIndex = 5
       "
       "Form1
       "
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
       Me.ClientSize = New System.Drawing.Size(456, 229)
       Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.StatusBar1, Me.Button1, Me.ProgressBar1})
       Me.Name = "Form1"
       Me.Text = "UseProgressBar"
       Me.ResumeLayout(False)
   End Sub
  1. End Region
   Function Factorial(ByVal Value As Double) As Double
       If (Value = 0) Then
           Factorial = 1.0
           System.Threading.Thread.Sleep(3000)
       Else
           Factorial = Value * Factorial(Math.Ceiling(Value - 1))
       End If
   End Function
   Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
       StatusBar1.Text = "Calculating Factorial(10)"
       Factorial(10)
       ProgressBar1.Value = 33
       StatusBar1.Text = "Calculating Factorial(20)"
       Factorial(20)
       ProgressBar1.Value = 66
       StatusBar1.Text = "Calculating Factorial(30)"
       Factorial(30)
       ProgressBar1.Value = 100
       StatusBar1.Text = "Done"
   End Sub

End Class

      </source>


ProgressBar control

<source lang="vbnet"> Imports System Imports System.Collections Imports System.Data Imports System.Drawing Imports System.Windows.Forms Imports System.ruponentModel Imports System.Drawing.Drawing2D Imports System.IO Public Class MainClass

   Shared Sub Main()
       Dim form1 As Form = New Form1
       Application.Run(form1)
   End Sub

End Class

Public Class Progress

   Inherits System.Windows.Forms.UserControl
  1. Region " Windows Form Designer generated code "
   Public Sub New()
       MyBase.New()
       "This call is required by the Windows Form Designer.
       InitializeComponent()
       "Add any initialization after the InitializeComponent() call
   End Sub
   "UserControl overrides dispose to clean up the component list.
   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
   "Required by the Windows Form Designer
   Private components As System.ruponentModel.IContainer
   "NOTE: The following procedure is required by the Windows Form Designer
   "It can be modified using the Windows Form Designer.  
   "Do not modify it using the code editor.
   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()
       "
       "Bar
       "
       Me.Bar.Anchor = ((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                   Or System.Windows.Forms.AnchorStyles.Right)
       Me.Bar.Location = New System.Drawing.Point(4, 8)
       Me.Bar.Name = "Bar"
       Me.Bar.Size = New System.Drawing.Size(154, 32)
       Me.Bar.TabIndex = 0
       "
       "lblProgress
       "
       Me.lblProgress.Anchor = ((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                   Or System.Windows.Forms.AnchorStyles.Right)
       Me.lblProgress.Location = New System.Drawing.Point(4, 48)
       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
       "
       "Progress
       "
       Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblProgress, 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, 68)
       Me.ResumeLayout(False)
   End Sub
  1. 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

End Class

Public Class Form1

   Inherits System.Windows.Forms.Form
  1. Region " Windows Form Designer generated code "
   Public Sub New()
       MyBase.New()
       "This call is required by the Windows Form Designer.
       InitializeComponent()
       "Add any initialization after the InitializeComponent() call
   End Sub
   "Form overrides dispose to clean up the component list.
   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
   "Required by the Windows Form Designer
   Private components As System.ruponentModel.IContainer
   "NOTE: The following procedure is required by the Windows Form Designer
   "It can be modified using the Windows Form Designer.  
   "Do not modify it using the code editor.
   Friend WithEvents Status As Progress
   Friend WithEvents cmdStart As System.Windows.Forms.Button
   Friend WithEvents tmrIncrementBar As System.Windows.Forms.Timer
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       Me.ruponents = New System.ruponentModel.Container()
       Me.Status = New Progress()
       Me.cmdStart = New System.Windows.Forms.Button()
       Me.tmrIncrementBar = New System.Windows.Forms.Timer(Me.ruponents)
       Me.SuspendLayout()
       "
       "Status
       "
       Me.Status.Anchor = ((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                   Or System.Windows.Forms.AnchorStyles.Right)
       Me.Status.BackColor = System.Drawing.SystemColors.Control
       Me.Status.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
       Me.Status.Location = New System.Drawing.Point(4, 16)
       Me.Status.Maximum = 100
       Me.Status.Name = "Status"
       Me.Status.Size = New System.Drawing.Size(284, 88)
       Me.Status.Step = 10
       Me.Status.TabIndex = 0
       Me.Status.Value = 0
       "
       "cmdStart
       "
       Me.cmdStart.FlatStyle = System.Windows.Forms.FlatStyle.System
       Me.cmdStart.Location = New System.Drawing.Point(92, 140)
       Me.cmdStart.Name = "cmdStart"
       Me.cmdStart.Size = New System.Drawing.Size(108, 24)
       Me.cmdStart.TabIndex = 1
       Me.cmdStart.Text = "Start"
       "
       "tmrIncrementBar
       "
       Me.tmrIncrementBar.Interval = 1000
       "
       "Form1
       "
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 14)
       Me.ClientSize = New System.Drawing.Size(292, 174)
       Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.cmdStart, Me.Status})
       Me.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
       Me.Name = "Form1"
       Me.Text = "Progress Host"
       Me.ResumeLayout(False)
   End Sub
  1. End Region
   Private Sub tmrIncrementBar_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrIncrementBar.Tick
       Status.PerformStep()
       If Status.Maximum = Status.Value Then tmrIncrementBar.Enabled = False
   End Sub
   Private Sub cmdStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStart.Click
       tmrIncrementBar.Enabled = False
       Status.Value = 0
       Status.Maximum = 20
       Status.Step = 1
       tmrIncrementBar.Enabled = True
   End Sub

End Class

      </source>


Progress Bar Demo: set Value and Step

<source lang="vbnet"> Imports System Imports System.Data Imports System.Windows.Forms Imports System.Drawing Imports System.Diagnostics Imports System.Drawing.Printing Public Class MainClass

   Shared Sub Main()
       
      Dim form1 As Form = New Form1
      Application.Run(form1) 
   End Sub

End Class

Public Class Form1

   Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
       ProgressBar1.Step = 21
       ProgressBar1.Value = 0
       btnGo.Enabled = False
       Timer1.Enabled = True
   End Sub
   Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
       ProgressBar1.PerformStep()
       If ProgressBar1.Value >= ProgressBar1.Maximum Then
           Timer1.Enabled = False
           btnGo.Enabled = True
       End If
   End Sub

End Class

<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _ Partial Public Class Form1

   Inherits System.Windows.Forms.Form
   "Form overrides dispose to clean up the component list.
   <System.Diagnostics.DebuggerNonUserCode()> _
   Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
       If disposing AndAlso components IsNot Nothing Then
           components.Dispose()
       End If
       MyBase.Dispose(disposing)
   End Sub
   "Required by the Windows Form Designer
   Private components As System.ruponentModel.IContainer
   "NOTE: The following procedure is required by the Windows Form Designer
   "It can be modified using the Windows Form Designer.  
   "Do not modify it using the code editor.
   <System.Diagnostics.DebuggerStepThrough()> _
   Private Sub InitializeComponent()
       Me.ruponents = New System.ruponentModel.Container
       Me.ProgressBar1 = New System.Windows.Forms.ProgressBar
       Me.btnGo = New System.Windows.Forms.Button
       Me.Timer1 = New System.Windows.Forms.Timer(Me.ruponents)
       Me.SuspendLayout()
       "
       "ProgressBar1
       "
       Me.ProgressBar1.Location = New System.Drawing.Point(16, 48)
       Me.ProgressBar1.Name = "ProgressBar1"
       Me.ProgressBar1.Size = New System.Drawing.Size(248, 16)
       Me.ProgressBar1.TabIndex = 3
       "
       "btnGo
       "
       Me.btnGo.Location = New System.Drawing.Point(16, 16)
       Me.btnGo.Name = "btnGo"
       Me.btnGo.Size = New System.Drawing.Size(48, 24)
       Me.btnGo.TabIndex = 2
       Me.btnGo.Text = "Go"
       "
       "Timer1
       "
       Me.Timer1.Interval = 250
       "
       "Form1
       "
       Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
       Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
       Me.ClientSize = New System.Drawing.Size(282, 86)
       Me.Controls.Add(Me.ProgressBar1)
       Me.Controls.Add(Me.btnGo)
       Me.Name = "Form1"
       Me.Text = "UseProgressBar"
       Me.ResumeLayout(False)
   End Sub
   Friend WithEvents ProgressBar1 As System.Windows.Forms.ProgressBar
   Friend WithEvents btnGo As System.Windows.Forms.Button
   Friend WithEvents Timer1 As System.Windows.Forms.Timer

End Class

      </source>