Practical
Learning: Using a Hatch Brush
|
|
- Start a new Windows Forms Application named YearlySales2
- Design the form as follows:
 |
Control |
Name |
Text |
GroupBox |
 |
|
Current Year's Sales |
Label |
 |
|
1st Qtr |
Label |
 |
|
2nd Qtr |
Label |
 |
|
3rd Qtr |
Label |
 |
|
4th Qtr |
TextBox |
 |
txtCurrentQtr1 |
0 |
TextBox |
 |
txtCurrentQtr2 |
0 |
TextBox |
 |
txtCurrentQtr3 |
0 |
TextBox |
 |
txtCurrentQtr4 |
0 |
Button |
 |
Close |
btnClose |
GroupBox |
 |
|
Previous Year's Sales |
Label |
 |
|
1st Qtr |
Label |
 |
|
2nd Qtr |
Label |
 |
|
3rd Qtr |
Label |
 |
|
4th Qtr |
TextBox |
 |
txtPreviousQtr1 |
0 |
TextBox |
 |
txtPreviousQtr2 |
0 |
TextBox |
 |
txtPreviousQtr3 |
0 |
TextBox |
 |
txtPreviousQtr4 |
0 |
Button |
 |
Generate |
btnGenerate |
Label |
 |
|
Legend |
Label |
 |
|
This Year's Sales |
Label |
 |
|
Last Year's Sales |
|
- Double-click an unoccupied area of the form and change the file as
follows:
Imports System.Drawing.Drawing2D
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
- In 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
- In the Class Name combo box, select btnGenerate
- In the Method Name combo box, select Click and implement the event as
follows:
Private Sub btnGenerateClick(ByVal sender As Object,
ByVal e As System.EventArgs)
Handles btnGenerate.Click
' Retrieve the values of the current year's sales
Dim CurQtr1 As Integer
Dim CurQtr2 As Integer
Dim CurQtr3 As Integer
Dim CurQtr4 As Integer
Try
CurQtr1 = CInt(txtCurrentQtr1.Text) / 100
Catch
MsgBox("Invalid Value")
End Try
Try
CurQtr2 = CInt(txtCurrentQtr2.Text) / 100
Catch
MsgBox("Invalid Value")
End Try
Try
CurQtr3 = CInt(txtCurrentQtr3.Text) / 100
Catch
MsgBox("Invalid Value")
End Try
Try
CurQtr4 = CInt(txtCurrentQtr4.Text) / 100
Catch
MsgBox("Invalid Value")
End Try
' Create an array of Rectangle objects for the current year
Dim RectCurrentYear As Rectangle() = {
New Rectangle(txtCurrentQtr1.Left + 20,
380 - CurQtr1, 40, CurQtr1),
New Rectangle(txtCurrentQtr2.Left + 20,
380 - CurQtr2, 40, CurQtr2),
New Rectangle(txtCurrentQtr3.Left + 20,
380 - CurQtr3, 40, CurQtr3),
New Rectangle(txtCurrentQtr4.Left + 20,
380 - CurQtr4, 40, CurQtr4)}
' Retrieve the values of last year's sales
Dim PrvQtr1 As Integer
Dim PrvQtr2 As Integer
Dim PrvQtr3 As Integer
Dim PrvQtr4 As Integer
Try
PrvQtr1 = CInt(txtPreviousQtr1.Text) / 100
Catch
MsgBox("Invalid Value")
End Try
Try
PrvQtr2 = CInt(txtPreviousQtr2.Text) / 100
Catch
MsgBox("Invalid Value")
End Try
Try
PrvQtr3 = CInt(txtPreviousQtr3.Text) / 100
Catch
MsgBox("Invalid Value")
End Try
Try
PrvQtr4 = CInt(txtPreviousQtr4.Text) / 100
Catch
MsgBox("Invalid Value")
End Try
' Create an array of Rectangle objects for the previous year
Dim RectPreviousYear As Rectangle() = {
New Rectangle(txtPreviousQtr1.Left + 30,
380 - PrvQtr1, 40, PrvQtr1),
New Rectangle(txtPreviousQtr2.Left + 30,
380 - PrvQtr2, 40, PrvQtr2),
New Rectangle(txtPreviousQtr3.Left + 30,
380 - PrvQtr3, 40, PrvQtr3),
New Rectangle(txtPreviousQtr4.Left + 30,
380 - PrvQtr4, 40, PrvQtr4)}
' In case the user has changed the values, erase the previous chart
GraphDrawingArea.Clear(BackColor)
Dim brushDiagCross As HatchBrush =
New HatchBrush(HatchStyle.DiagonalCross,
Color.White, Color.Blue)
Dim brushDotDiamond As HatchBrush =
New HatchBrush(HatchStyle.DottedDiamond,
Color.Fuchsia, Color.Brown)
' Draw the chart for the previous year first to send it back
GraphDrawingArea.FillRectangles(brushDiagCross,
RectPreviousYear)
GraphDrawingArea.DrawRectangles(New Pen(Color.Blue),
RectPreviousYear)
' Draw the chart for the current year in front
GraphDrawingArea.FillRectangles(brushDotDiamond,
RectCurrentYear)
GraphDrawingArea.DrawRectangles(New Pen(Color.Red),
RectCurrentYear)
' Draw the small rectangles of the legend
graphDrawingArea.FillRectangle(brushDotDiamond,
lblCurYear.Left - 30,
lblCurYear.Top, 20, 14)
graphDrawingArea.DrawRectangle(new Pen(Color.Red),
lblCurYear.Left - 30,
lblCurYear.Top, 20, 14)
graphDrawingArea.FillRectangle(brushDiagCross,
lblLastYear.Left - 30,
lblLastYear.Top, 20, 14)
graphDrawingArea.DrawRectangle(new Pen(Color.Blue),
lblLastYear.Left - 30,
lblLastYear.Top, 20, 14)
graphDrawingArea.DrawRectangle(new Pen(Color.Black),
25, 380, Width - 220, 1)
Invalidate()
End Sub
- Execute the application and test the form

- After using it, close the form
- Change the Paint event of the form as follows:
Private Sub btnGenerateClick(ByVal sender As Object,
ByVal e As System.EventArgs)
Handles btnGenerate.Click
. . . No Change
' In case the user has changed the values, erase the previous chart
GraphDrawingArea.Clear(BackColor)
Dim rect As Rectangle = New Rectangle(10, 190, 300, 210)
Dim linGradBrush As LinearGradientBrush =
New LinearGradientBrush(rect,
Color.FromArgb(204, 102, 0),
Color.AntiqueWhite,
LinearGradientMode.Vertical)
GraphDrawingArea.FillRectangle(linGradBrush, rect)
GraphDrawingArea.DrawRectangle(New Pen(Color.Black), rect)
Dim brushDiagCross As HatchBrush =
New HatchBrush(HatchStyle.DiagonalCross,
Color.White, Color.Blue)
. . . No Change
End Sub
- Execute the application to test it:

- After using the form, close it
Download
|
|