Practical
Learning: Using Texture Brushes
|
|
- Start a new Windows Forms Application named SchoolEnrolment2
- Design the form as follows:
|
Control |
Name |
Text |
Label |
|
|
Enrolment / Program
|
Label |
|
|
Graduates |
Label |
|
|
Undergraduates |
Label |
|
|
Certificates |
TextBox |
|
txtGraduates |
0 |
TextBox |
|
txtUndergraduates |
0 |
TextBox |
|
txtCertificates |
0 |
Button |
|
btnCreateChart |
Create Chart |
PictureBox |
|
pbxChart |
|
Label |
|
|
Legend |
Label |
|
lblGraduates |
Graduates |
Label |
|
lblUndergraduates |
Undergraduates |
Label |
|
lblCertificates |
Certificates |
Button |
|
btnClose |
Close |
|
- To design a bitmap, on the main menu, click Project -> Add New Item...
- In the Templates list, click Bitmap File
- Change the Name to graduates and click Add
- To change the file location of the bitmap, on the main menu, click File
-> Save graduates.bmp As...
- Locate and display the SchoolEnrolment2\SchoolEnrolment2\bin\debug
folder
- Click Save
- In the Solution Explorer, double-click graduates.bmp to make sure it is
displayed
- In the Properties Window, click Width and type 16
- Click Height and type 16
- Design the bitmap as follows:
- In the Solution Explorer, right-click the Debug folder -> Add -> New
Item...
- In the Templates list, make sure Bitmap File is selected.
Set the Name to undergraduates and click Add
- In the Properties window, click Width and type 16
- Click Height and type 16
- Design the bitmap as follows:
- In the Solution Explorer, right-click the Debug folder -> Add -> New
Item...
- In the Templates list, make sure Bitmap File is selected.
Set the Name to certificates and click Add
- In the Properties window, click Width and type 16
- Click Height and type 16
- Design the bitmap as follows:
- Display the form
- Right-click the form and click View Code
- Declare three variables as follows:
Public Class Form1
Dim Graduates As Single
Dim Undergraduates As Single
Dim Certificates As Single
End Class
- In the Class Name combo box, select (Form1 Events)
- 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
- In the Class Name combo box, select pbxChart
- 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
- In the Class Name combo box, select btnCreateChart
- 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
- Execute the application and test the form
- After using it, close the form
Download
|
|