What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleList the HatchStyle values defined by VB .NET
DescriptionThis example shows how to list the HatchStyles defined by VB .NET. It uses reflection to get a list of the values at run time rather than requiring you to hard code the values at design time.
KeywordsEnum, reflection, HatchStyle
CategoriesVB.NET, Graphics
When the program loads, it uses GetType to get type information about the HatchStyle Enum. It calls the GetFields method to get an array of FieldInfo objects describing the Enum's fields (public variables).

The program checks each FieldInfo object's IsLiteral property to see if it is a literal design time defined value. For each literal value, it uses the FieldInfo object's GetValue method to get the literal's value and passes the result to the DrawHatchStyleSample subroutine, converting the value into its true HatchStyle data type.

Imports System.Reflection
Imports System.Drawing.Drawing2D
Private Const WID As Integer = 150
Private Const HGT As Integer = 20
Private Const DX As Integer = 5
Private Const DY As Integer = 20

Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
    ' Make a Bitmap and associated Graphics object.
    Dim samples_size As New Size( _
        5 * (WID + DX), 12 * (HGT + DY))
    Dim bm As New Bitmap(samples_size.Width, _
    Dim gr As Graphics = Graphics.FromImage(bm)

    ' Enumerate the HatchStyle fields.
    Dim field_infos() As FieldInfo = _

    ' Loop over the HatchStyle fields.
    For Each field_info As FieldInfo In field_infos
        ' See if this is a literal value set at compile
        ' time.
        If field_info.IsLiteral Then
            ' Draw a sample.
            Dim hatch_style As HatchStyle = _
                DirectCast(field_info.GetValue(Me), _
            DrawHatchStyleSample(gr, hatch_style)
        End If
    Next field_info

    ' Display the result.
    picSamples.Image = bm
    picSamples.ClientSize = samples_size
    Me.ClientSize = picSamples.Size

    ' Clean up.
End Sub
Subroutine DrawHatchStyleSample makes a Brush using the indicated HatchStyle and fills a rectangle with it. It then draws the HatchStyle's name below the rectangle and updates variables x and y to give the position for the next sample rectangle.
' Draw a sample of this hatch style.
Private Sub DrawHatchStyleSample(ByVal gr As Graphics, _
    ByVal hatch_style As HatchStyle)
    Static x As Integer = 0
    Static y As Integer = 0

    ' Draw the sample.
    Dim hatch_brush As New HatchBrush( _
        hatch_style, Color.Black, Color.White)
    gr.FillRectangle(hatch_brush, x, y, WID, HGT)
    gr.DrawRectangle(Pens.Black, x, y, WID, HGT)

    ' Draw the sample's name.
    Dim string_format As New StringFormat
    string_format.Alignment = StringAlignment.Center
    string_format.LineAlignment = StringAlignment.Near
    gr.DrawString(hatch_style.ToString, Me.Font, _
        Brushes.Black, x + WID \ 2, y + HGT, string_format)

    ' Increment the position for the next sample.
    y += HGT + DY
    If y + HGT + DY > gr.VisibleClipBounds.Bottom Then
        y = 0
        x += WID + DX
    End If
End Sub
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.