Home

GDI+ Application: School Enrolment

 

School Enrolment

Introduction

This is an example of drawing a pie chart in GDI+.

Windows Controls:

  • Label
  • Button
  • Text Box
  • Picture Box

Practical LearningPractical Learning: Using Texture Brushes

  1. Start a new Windows Forms Application named SchoolEnrolment2
  2. Design the form as follows:
     
    School Enrolment
    Control Name Text
    Label Label   Enrolment / Program
    Label Label   Graduates
    Label Label   Undergraduates
    Label Label   Certificates
    TextBox TextBox txtGraduates 0
    TextBox TextBox txtUndergraduates 0
    TextBox TextBox txtCertificates 0
    Button Button btnCreateChart Create Chart
    PictureBox PictureBox pbxChart  
    Label Label   Legend
    Label Label lblGraduates Graduates
    Label Label lblUndergraduates Undergraduates
    Label Label lblCertificates Certificates
    Button Button btnClose Close
  3. To design a bitmap, on the main menu, click Project -> Add New Item...
  4. In the Templates list, click Bitmap File
  5. Change the Name to graduates and click Add
  6. To change the file location of the bitmap, on the main menu, click File -> Save graduates.bmp As...
  7. Locate and display the SchoolEnrolment2\SchoolEnrolment2\bin\debug folder
  8. Click Save
  9. In the Solution Explorer, double-click graduates.bmp to make sure it is displayed
  10. In the Properties Window, click Width and type 16
  11. Click Height and type 16
  12. Design the bitmap as follows:
     
    Graduates
  13. In the Solution Explorer, right-click the Debug folder -> Add -> New Item...
  14. In the Templates list, make sure Bitmap File is selected.
    Set the Name to undergraduates and click Add
  15. In the Properties window, click Width and type 16
  16. Click Height and type 16
  17. Design the bitmap as follows:
     
    Undergraduates
  18. In the Solution Explorer, right-click the Debug folder -> Add -> New Item...
  19. In the Templates list, make sure Bitmap File is selected.
    Set the Name to certificates and click Add
  20. In the Properties window, click Width and type 16
  21. Click Height and type 16
  22. Design the bitmap as follows:
     
    Certificates
  23. Display the form
  24. Right-click the form and click View Code
  25. Declare three variables as follows:
    Public Class Form1
        Dim Graduates As Single
        Dim Undergraduates As Single
        Dim Certificates As Single
    End Class
  26. In the Class Name combo box, select (Form1 Events)
  27. In the Method Name combo box, select Paint and implement the event as follows:
    Private Sub Form1Paint(ByVal sender As Object, 
                            ByVal e As System.Windows.Forms.PaintEventArgs) 
                            Handles Me.Paint
    
            Dim bmpGraduates As Bitmap = New Bitmap("graduates.bmp")
            Dim BrushGraduates As TextureBrush = 
    		New TextureBrush(bmpGraduates)
            Dim bmpUndergraduates As Bitmap = New Bitmap("undergraduates.bmp")
            Dim brushUndergraduates As TextureBrush = 
    		New TextureBrush(bmpUndergraduates)
            Dim bmpCertificates As Bitmap = New Bitmap("certificates.bmp")
            Dim brushCertificates As TextureBrush = 
    		New TextureBrush(bmpCertificates)
    
            pbxChart.CreateGraphics().FillPie(BrushGraduates, 
                                      0.0F, 0.0F, 
                              260.0F, 200.0F, 0.0F, Graduates)
    
            pbxChart.CreateGraphics().FillPie(brushUndergraduates, 
                                      0.0F, 0.0F, 260.0F, 
                        200.0F, Graduates, Undergraduates)
    
            pbxChart.CreateGraphics().FillPie(brushCertificates, 
                                      0.0F, 0.0F, 260.0F, 
                          200.0F, Graduates + Undergraduates, 
                              Certificates)
    
            e.Graphics.FillRectangle(BrushGraduates, 
                                  New Rectangle(lblGraduates.Left, 
                                lblGraduates.Top + 18, 
                        btnClose.Width, 20))
    
            e.Graphics.DrawRectangle(New Pen(Color.Black), 
                                  New Rectangle(lblGraduates.Left - 1, 
                                lblGraduates.Top + 18, 
                        btnClose.Width, 20))
    
            e.Graphics.FillRectangle(brushUndergraduates, 
                                  New Rectangle(btnClose.Left, 
                                lblUndergraduates.Top + 18, 
                        btnClose.Width, 20))
    
            e.Graphics.DrawRectangle(New Pen(Color.Black), 
                                  New Rectangle(btnClose.Left - 1, 
                                lblUndergraduates.Top + 18, 
                        btnClose.Width + 1, 20))
    
            e.Graphics.FillRectangle(brushCertificates, 
                                  New Rectangle(btnClose.Left, 
                                lblCertificates.Top + 18, 
                        btnClose.Width, 20))
    
            e.Graphics.DrawRectangle(New Pen(Color.Black), 
                                  New Rectangle(btnClose.Left - 1, 
                                lblCertificates.Top + 18, 
                        btnClose.Width + 1, 20))
    
    
            pbxChart.CreateGraphics().DrawPie(New Pen(Color.Blue), 
                                      0.0F, 0.0F, 260.0F, 
                              200.0F, 0.0F, Graduates)
            pbxChart.CreateGraphics().DrawPie(New Pen(Color.Red), 
                                      0.0F, 0.0F, 260.0F, 
                        200.0F, Graduates, Undergraduates)
            pbxChart.CreateGraphics().DrawPie(New Pen(Color.Green), 
                                      0.0F, 0.0F, 260.0F, 
                          200.0F, Graduates + Undergraduates, 
                              Certificates)
    
            pbxChart.CreateGraphics().DrawEllipse(New Pen(Color.Red, 2), 
                                      New Rectangle(0.0F, 0.0F, 260.0F, 200))
    End Sub
  28. In the Class Name combo box, select pbxChart
  29. In the Method Name combo box, select Paint and implement the event as follows:
    Private Sub pbxChartPaint(ByVal sender As Object, 
                             ByVal e As System.Windows.Forms.PaintEventArgs) 
                               Handles pbxChart.Paint
            Invalidate()
        End Sub
  30. In the Class Name combo box, select btnCreateChart
  31. In the Method Name combo box, select Click and implement the event as follows:
    Private Sub btnCreateChartClick(ByVal sender As Object, 
                                         ByVal e As System.EventArgs) 
                                         Handles btnCreateChart.Click
            Dim grad As Single
            Dim under As Single
            Dim cert As Single
            Dim total As Single
            Dim percentGraduates As Single
            Dim percentUndergraduates As Single
            Dim percentCertificates As Single
    
            Try
                grad = CSng(txtGraduates.Text)
            Catch
                MsgBox("Invalid Value")
            End Try
    
            Try
                under = CSng(txtUndergraduates.Text)
            Catch
                MsgBox("Invalid Value")
            End Try
    
            Try
                cert = CSng(txtCertificates.Text)
            Catch
                MsgBox("Invalid Value")
            End Try
    
            total = grad + under + cert
            percentGraduates = (grad / total) * 100
            percentUndergraduates = (under / total) * 100
            percentCertificates = (cert / total) * 100
    
            Graduates = (360 * percentGraduates) / 100
            Undergraduates = (360 * percentUndergraduates) / 100
            Certificates = (360 * percentCertificates) / 100
    
            pbxChart.Invalidate()
    End Sub
  32. Execute the application and test the form
     
     School Enrolment
  33. After using it, close the form

Download

 

Home Copyright © 2008-2012 FunctionX Home