Zebra0.com

csharp drawing

Draw Pie for 2 Scroll Bars

Start a new project. Add scroll bars hsbStart and hsbDegrees.
Set the properties so that you can select any integer value from 0 to 360.
Add labels lblInfo1, lblInfo2 and lblStart and lblDegrees as shown.

Add this code:

private void DrawGraph()
{
   // Get the graphics of the form.
   Graphics g = this.CreateGraphics();
   // Create a Black Brush for background.
   SolidBrush myBrushBackground = new SolidBrush(Color.Black);
   // Create a Red Brush for foreground.
   SolidBrush myBrushForeground = new SolidBrush(Color.Red);
   // We want to draw the pie under the 2nd scroll bar.
   int top = hsbDegrees.Location.Y + hsbDegrees.Height + 10;
   // Create a rectangle 200x200 (square) for the circle.
   Rectangle myRect = new Rectangle(10, top, 200, 200);
   g.FillPie(myBrushBackground, myRect, 0, 360);
   g.FillPie(myBrushForeground, myRect, hsbStart.Value, hsbDegrees.Value);
}

private void hsbStart_Scroll(object sender, ScrollEventArgs e)
{
   // Display start value and draw pie
   lblStart.Text = hsbStart.Value.ToString();
   DrawGraph();
}

private void hsbDegrees_Scroll(object sender, ScrollEventArgs e)
{
   // Display degrees value and draw pie
   lblDegrees.Text = hsbDegrees.Value.ToString();
   DrawGraph();
}
To Do: Experiment! Try using 2 scroll bars to select rent and food. Use the pie to show which percentage each item is,

End of lesson, Next lesson: ListBox Manager in C#