Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleUse reflection to list an object's events and their descriptions in VB .NET
DescriptionThis example shows how to use reflection to list an object's events and their descriptions in Visual Basic .NET. It uses a type's GetEvents method to get event information. It then iterates through the event information displaying each event's name and description.
Keywordsreflection, event, VB.NET
CategoriesVB.NET
 
The program gives its ListView control two column headers: Event and Description. It gets the Form1 type and uses GetEvents to get an array of EventInfo objects describing the events.

For each of those objects, the code adds the event's name and description to the ListView.

After it has created a ListView row for each event, the code sizes the ListView's columns to fit their data and then sizes the form to fit the ListView control.

 
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
    ' Make column headers.
    lvwEvents.Columns.Clear()
    lvwEvents.Columns.Add("Event", 10, _
        HorizontalAlignment.Left)
    lvwEvents.Columns.Add("Description", 10, _
        HorizontalAlignment.Left)

    ' List the events.
    ' Use the class you want to study instead of Form1.
    Dim events_info As EventInfo() = _
        GetType(Form1).GetEvents()
    lvwEvents.Items.Clear()
    For i As Integer = 0 To events_info.Length - 1
        With events_info(i)
            ListViewMakeRow(lvwEvents, _
                .Name, .ToString)
        End With
    Next i

    ' Size the columns to fit the data.
    lvwEvents.Columns(0).Width = -2
    lvwEvents.Columns(1).Width = -2

    ' Size the form.
    Dim new_wid As Integer = _
        lvwEvents.Columns(0).Width + _
        lvwEvents.Columns(1).Width + _
        30
    Me.Size = New Size(new_wid, Me.Size.Height)
End Sub
 
Helper subroutine ListViewMakeRow makes an item and sub-items in a ListView control.
 
' Make a ListView row.
Private Sub ListViewMakeRow(ByVal lvw As ListView, ByVal _
    item_title As String, ByVal ParamArray subitem_titles() _
    As String)
    ' Make the item.
    Dim new_item As ListViewItem = lvw.Items.Add(item_title)

    ' Make the sub-items.
    For i As Integer = subitem_titles.GetLowerBound(0) To _
        subitem_titles.GetUpperBound(0)
        new_item.SubItems.Add(subitem_titles(i))
    Next i
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated