VB write a custom progress bar control that can display percentages

Source: Internet
Author: User
Percent | control | show Run Effect:



Design method:

1. Add a Label control Label1 to the UserControl, set it to a plane, and use it to make the outer frame. Add two PictureBox controls PictureBox1 as a progress indicator, PICTUREBOX2 control as the background of the control.



2. Add the following code

Option Explicit

' Define private variables to store property values
Private Mvarmax as Long
Private Mvarmin as Long
Private Mvarvalue as Long

Private Rate as String

Private Sub usercontrol_initialize ()
' Initialization
Picture2.backcolor = Vbblue
End Sub

Public Property Get BackColor () as Ole_color
' Read BackColor properties
BackColor = Picture1.backcolor
End Property

Public Property Let BackColor (ByVal Vnewvalue as OLE_COLOR)
' Set the BackColor property
Picture1.backcolor = Vnewvalue
End Property

Private Sub usercontrol_initproperties ()
' Initialize properties
Max = 100
Min = 0
Value = 0
End Sub

Private Sub usercontrol_readproperties (propbag as PropertyBag)
' Read the property values set from the property form
Mvarmax = Propbag.readproperty ("Max", 100)
Mvarmin = Propbag.readproperty ("Min", 0)
' Value property values are not provided here, mainly imitate VB from the progress bar control
' Mvarvalue = Propbag.readproperty ("Value", 0)
End Sub

Private Sub usercontrol_writeproperties (propbag as PropertyBag)
' Save the property values set from the property form
Propbag.writeproperty "Max", Mvarmax, 100
Propbag.writeproperty "Min", Mvarmin, 0
' Propbag.writeproperty ' Value ', Mvarvalue, 0
End Sub

Private Sub usercontrol_resize ()
' Resize Event
Label1.move 0, 0, Usercontrol.width/screen.twipsperpixelx, usercontrol.height/screen.twipsperpixely
Picture1.move 1, 1, usercontrol.width/screen.twipsperpixelx-2, usercontrol.height/screen.twipsperpixely-2
Picture2.move 1, 1, 1, usercontrol.height/screen.twipsperpixely-2
End Sub

Public Property Get Max () as Long
' Read Max properties
Max = Mvarmax
End Property

Public Property Let Max (ByVal Vnewvalue as Long)
' Set Max Property
Mvarmax = Vnewvalue
If vnewvalue < min Then err.raise "1001", "Max must be greater than min"
End Property

Public Property Get Min () as Long
' Read min attribute
Min = Mvarmin
End Property

Public Property Let Min (ByVal Vnewvalue as Long)
' Set min property
If vnewvalue > Max Then err.raise "1000", "min must be less than Max"
Mvarmin = Vnewvalue
End Property

Public Property Get Value () as Long
' Read the Value property
Value = Mvarvalue
End Property

Public Property Let Value (ByVal Vnewvalue as Long)
' Set the Value property
' principle is to print percent progress in different colors in two PictureBox
Dim DX as Long, DY as Long

If vnewvalue > Max Then err.raise "1002", "value cannot be greater than Max"
Mvarvalue = Vnewvalue

Picture2.width = Value/(max-min) * (usercontrol.width/screen.twipsperpixelx-2)
Rate = Int (Value/max-min) & "%"
DX = (Picture1.width-picture1.textwidth (Rate))/2
DY = (Picture1.height-picture1.textheight (Rate))/2
Picture1.forecolor = Vbblack
Picture2.forecolor = Vbwhite
If DX < Picture2.width Then
Picture2.cls
Picture2.currentx = DX
Picture2.currenty = DY
Picture2.print Rate
Else
Picture1.cls
Picture1.currentx = DX
Picture1.currenty = DY
Picture1.Print Rate
End If
End Property

3. Create another test project, add your own progress bar control and a system progress bar control, add the following code:

Option Explicit

Private Sub Command1_Click ()
Unload Me
End Sub

Private Sub Timer1_timer ()
Myprogressbar1.value = Myprogressbar1.value + 2
progressBar1.Value = progressBar1.Value + 2
If myprogressbar1.value = Myprogressbar1.max Then timer1.enabled = False
End Sub


OK. Run and see how it works.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.