Hello,
I am using basic graphics to produce the "vanilla" picture box on the left. I was wondering what I can do to get something nicer like the image on the right.
The idea is to fill the "bucket" with a certain color based on a given percentage (in this case 20%).
My code for the vanilla is as follows:
(Note: the code is using an array for the percentages and an array for the colors, but for this application, there will only ever be one of each - i.e. "fill the bucket with 20% of blue".)
Code:Public Sub FillRiskBucket(pobjPic As PictureBox, percentages As Integer(), colorsToUse As Color(), pstrText As String, Optional pblnHideIfZero As Boolean = True, Optional pintFontSize As Integer = 10) pobjPic.CreateGraphics.Clear(Color.Azure) ' Create a Graphics object to draw on the picturebox Dim G As Graphics = pobjPic.CreateGraphics() ' Calculate the number of pixels per 1 percent Dim pixelsPerPercent As Single = pobjPic.Height / 100.0F ' Keep track of the height at which to start drawing (starting from the bottom going up) Dim drawHeight As Integer = pobjPic.Height ' Loop through all percentages and draw a rectangle for each For i As Integer = 0 To percentages.Length - 1 ' Create a brush with the current color Dim brush As New SolidBrush(colorsToUse(i)) ' Update the height at which the next rectangle is drawn. drawHeight -= CInt(pixelsPerPercent * percentages(i)) ' Draw a filled rectangle G.FillRectangle(brush, 0, drawHeight, pobjPic.Width, pixelsPerPercent * percentages(i)) Next Dim objMyFont As Font = New Font("Tahoma", pintFontSize, FontStyle.Bold) G.DrawString(pstrText, objMyFont, Brushes.Black, New PointF(pobjPic.Width / 2 - (G.MeasureString(pstrText, objMyFont).Width / 2.0F), pobjPic.Height / 2 - (G.MeasureString(pstrText, objMyFont).Height / 2.0F))) If pblnHideIfZero Then pobjPic.Visible = (percentages(0) > 0) Else pobjPic.Visible = True End If End Sub




Reply With Quote