Home

GDI+ Example Application: Weekly Sales

 

Introduction

This application is an example of drawing rectangular shapes and painting them. The idea is to simulate column chart. Each rectangle represents a value of a day (Monday, Tuesday, Wednesday, Thursday, and Friday). Their widths are the same. To evaluate the height, we use the value of the day entered in the corresponds text box. To make it a little entertaining, each rectangle is painted with a different color.

 

Practical LearningPractical Learning: Using a Solid Brush

  1. Start a new Windows Forms Application named WeeklySales2
  2. Design the form as follows:
     
    Company Weekly Sales
    Control Name Text Other Properties
    Label Label   Monday  
    Label Label   Tuesday  
    Label Label   Wednesday  
    Label Label   Thursday  
    Label Label   Friday  
    TextBox TextBox txtMonday 12000 TextAlign: Right
    TextBox TextBox txtTuesday 11000 TextAlign: Right
    TextBox TextBox txtWednesday 8500 TextAlign: Right
    TextBox TextBox txtThursday 16800 TextAlign: Right
    TextBox TextBox txtFriday 17500 TextAlign: Right
    Button Button Generate btnGenerate  
  3. Double-click an unoccupied area of the form and change the file as follows:
     
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WeeklySales2
    {
        public partial class Form1 : Form
        {
            Graphics graphDrawingArea;
            Bitmap bmpDrawingArea;
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                bmpDrawingArea = new Bitmap(Width, Height);
                graphDrawingArea = Graphics.FromImage(bmpDrawingArea);
            }
        }
    }
  4. Return to the form and click an empty area on it. In the Properties window, click the Events button Events
  5. Double-click the Paint field and implement its event as follows:
     
    private void Form1_Paint(object sender, PaintEventArgs e)
    {
                e.Graphics.DrawImage(bmpDrawingArea, 0, 0);
    }
  6. Return to the form and double-click the Generate button
  7. Implement its Click event as follows:
     
    private void btnGenerate_Click(object sender, EventArgs e)
    {
                int monday = 0;
                int tuesday = 0;
                int wednesday = 0;
                int thursday = 0;
                int friday = 0;
    
                try
                {
                    monday = int.Parse(txtMonday.Text) / 100;
                }
                catch (FormatException)
                {
                    MessageBox.Show("Invalid value");
                }
    
                try
                {
                    tuesday = int.Parse(txtTuesday.Text) / 100;
                }
                catch (FormatException)
                {
                    MessageBox.Show("Invalid value");
                }
    
                try
                {
                    wednesday = int.Parse(txtWednesday.Text) / 100;
                }
                catch (FormatException)
                {
                    MessageBox.Show("Invalid value");
                }
    
                try
                {
                    thursday = int.Parse(txtThursday.Text) / 100;
                }
                catch (FormatException)
                {
                    MessageBox.Show("Invalid value");
                }
    
                try
                {
                    friday = int.Parse(txtFriday.Text) / 100;
                }
                catch (FormatException)
                {
                    MessageBox.Show("Invalid value");
                }
    
                graphDrawingArea.Clear(this.BackColor);
    
                graphDrawingArea.FillRectangle(new SolidBrush(Color.Red),
                        this.txtMonday.Left + 5,
                        280 - monday, 40, monday);
                graphDrawingArea.DrawRectangle(new Pen(Color.Black),
                        this.txtMonday.Left + 5,
                            280 - monday, 40, monday);
                graphDrawingArea.FillRectangle(new SolidBrush(Color.Blue),
                        this.txtTuesday.Left + 5,
                        280 - tuesday, 40, tuesday);
                graphDrawingArea.DrawRectangle(new Pen(Color.Black),
                        this.txtTuesday.Left + 5,
                            280 - tuesday, 40, tuesday);
                graphDrawingArea.FillRectangle(new SolidBrush(Color.Fuchsia),
                        this.txtWednesday.Left + 5,
                        280 - wednesday, 40, wednesday);
                graphDrawingArea.DrawRectangle(new Pen(Color.Black),
                        this.txtWednesday.Left + 5,
                        280 - wednesday, 40, wednesday);
                graphDrawingArea.FillRectangle(new SolidBrush(Color.Brown),
                        this.txtThursday.Left + 5,
                        280 - thursday, 40, thursday);
                graphDrawingArea.DrawRectangle(new Pen(Color.Black),
                        this.txtThursday.Left + 5,
                        280 - thursday, 40, thursday);
                graphDrawingArea.FillRectangle(new SolidBrush(Color.Turquoise),
                        this.txtFriday.Left + 5,
                        280 - friday, 40, friday);
                graphDrawingArea.DrawRectangle(new Pen(Color.Black),
                        this.txtFriday.Left + 5,
                            280 - friday, 40, friday);
    
                graphDrawingArea.DrawRectangle(new Pen(Color.Black),
                        10, 280, Width - 30, 1);
                Invalidate();
    }
  8. Execute the application and test the form
     
     Weekly Sales
  9. After using it, close the form
 
 

Home Copyright 2008-2009, yevol.com