Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic
Expertise: Intermediate
Oct 9, 1998



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Progress Bars With A Gradient

The tip "Color Status Indicator" ["101 Tech Tips for VB Developers," Supplement to the February 1997 issue of VBPJ, page 26] got me thinking that it would look even more attractive to have the status change color in gradient steps. Instead of changing the color at particular points, start at red, blend to orange, then yellow, and finally green.

To try this, place a Sheridan 3-D panel on your form and set the FloodType property to 1 (Left to Right) and ShowFloodPct to False. Place a command button and a text box (set the Caption to blank) on the form to do your testing. Place this code in the form:

 Private Sub Command1_Click()
	Dim i As Integer, iIncrement As Integer
	Dim iMidPoint As Integer, iCheckpoint As Integer
	Dim j As Integer, sAddIncrement As String
	Dim sSubIncrement As String
	iIterations = Val(Text1)
	iMidPoint = iIterations * 0.5 - 1
	' find the midpoint
	If iIterations / 510 = iIterations \ 510 Then
		'perfect algorithm
		iIncrement = 1
		iCheckpoint = 1
	ElseIf iIterations < 510 Then
		'increment the color faster
		iIncrement = 510 \ iIterations
		iCheckpoint = 1
		'change the color less frequently
		iIncrement = 1
		iCheckpoint = iIterations \ 510 + 1
	End If
	sAddIncrement = "&H0000" & _
		Format$(iIncrement, "00") & "00"
	sSubIncrement = "&H0000" & _
		Format$(iIncrement, "0000")
	SSPanel1.FloodColor = &HFF
	'start the panel at Red
	'At your preference show the 
	'percentage. However, the tighter 
	'the loop, the less valuable this is.
	SSPanel1.FloodShowPct = True
	For i = 0 To iIterations - 1
		'do your real processing here
		If i / iCheckpoint = i \ iCheckpoint Then
			With SSPanel1
				If i <= iMidPoint Then
					.FloodColor = _
						.FloodColor + sAddIncrement
					.FloodColor = _
						.FloodColor - sSubIncrement
				End If
				.FloodPercent = (i + 1) / iIterations * 100
			End With
		End If
		'if the loop is tight, be sure to 
		'release the processor 
		'occasionally - also, some flood
		'controls (other than SSPanel) 
		'may require this to get the 
		'color updated in the control
	MsgBox "All done!", vbOKOnly + vbInformation
	SSPanel1.FloodPercent = 0
	SSPanel1.FloodShowPct = False
End Sub

The effect works best when your video is set to at least 16-bit (High Color). Because there are exactly 510 progressive gradients between red and green, the number of iterations you enter will influence what shade of green you end up with. As the iterations increase (>900), you'll end up with bright green.

Jim Poland
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date