Home

GDI+ Application: Weekly Sales

 

Weekly Sales

Description

This is an example of a vertical rectangular chart. To draw the rectangles, we use simple solid brushes.

Windows Controls:

     

 

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:
     
    Public Class Form1
    
        Private GraphDrawingArea As Graphics
        Private BmpDrawingArea As Bitmap
    
        Private Sub Form1Load(ByVal sender As System.Object, 
                               ByVal e As System.EventArgs) 
                               Handles MyBase.Load
            BmpDrawingArea = New Bitmap(Width, Height)
            GraphDrawingArea = Graphics.FromImage(bmpDrawingArea)
        End Sub
    End Class
  4. In the Method Name combo box, select Paint and implement its event as follows:
     
    Private Sub Form1Paint(ByVal sender As Object, 
                                ByVal e As System.Windows.Forms.PaintEventArgs) 
                                Handles Me.Paint
            e.Graphics.DrawImage(BmpDrawingArea, 0, 0)
    End Sub
  5. In the Class Name combo box, select btnGenerate
  6. In the Method Name combo box, select Click and implement its Click event as follows:
     
    Private Sub btnGenerateClick(ByVal sender As Object, 
                                      ByVal e As System.EventArgs) 
                                      Handles btnGenerate.Click
            Dim Monday As Integer
            Dim Tuesday As Integer
            Dim Wednesday As Integer
            Dim Thursday As Integer
            Dim Friday As Integer
    
            Try
                Monday = CInt(txtMonday.Text) / 100
            Catch
                MsgBox("Invalid value")
            End Try
    
            Try
                Tuesday = CInt(txtTuesday.Text) / 100
            Catch
                MsgBox("Invalid value")
            End Try
    
            Try
                Wednesday = CInt(txtWednesday.Text) / 100
            Catch
                MsgBox("Invalid value")
            End Try
    
            Try
                Thursday = CInt(txtThursday.Text) / 100
            Catch
                MsgBox("Invalid value")
            End Try
    
            Try
                Friday = CInt(txtFriday.Text) / 100
            Catch
                MsgBox("Invalid value")
            End Try
    
            GraphDrawingArea.Clear(BackColor)
    
            GraphDrawingArea.FillRectangle(New SolidBrush(Color.Red), 
                    txtMonday.Left + 5, 
                    280 - monday, 40, monday)
            GraphDrawingArea.DrawRectangle(New Pen(Color.Black), 
                    txtMonday.Left + 5, 
                        280 - monday, 40, monday)
            GraphDrawingArea.FillRectangle(New SolidBrush(Color.Blue), 
                    txtTuesday.Left + 5, 
                    280 - tuesday, 40, tuesday)
            GraphDrawingArea.DrawRectangle(New Pen(Color.Black), 
                    txtTuesday.Left + 5, 
                        280 - tuesday, 40, tuesday)
            GraphDrawingArea.FillRectangle(New SolidBrush(Color.Fuchsia), 
                    txtWednesday.Left + 5, 
                    280 - wednesday, 40, wednesday)
            GraphDrawingArea.DrawRectangle(New Pen(Color.Black), 
                    txtWednesday.Left + 5, 
                    280 - wednesday, 40, wednesday)
            GraphDrawingArea.FillRectangle(New SolidBrush(Color.Brown), 
                    txtThursday.Left + 5, 
                    280 - thursday, 40, thursday)
            GraphDrawingArea.DrawRectangle(New Pen(Color.Black), 
                    txtThursday.Left + 5, 
                    280 - thursday, 40, thursday)
            GraphDrawingArea.FillRectangle(New SolidBrush(Color.Turquoise), 
                    txtFriday.Left + 5, 
                    280 - friday, 40, friday)
            GraphDrawingArea.DrawRectangle(New Pen(Color.Black), 
                    txtFriday.Left + 5, 
                        280 - friday, 40, friday)
    
            GraphDrawingArea.DrawRectangle(New Pen(Color.Black), 
                        10, 280, Width - 30, 1)
            Invalidate()
    End Sub
  7. Execute the application and test the form
     
     Weekly Sales
  8. After using it, close the form

Download

 

Home Copyright © 2008-2012 FunctionX Home