Imran,
You can actually reduce that to
arr.AddRange([Enum].GetValues(GetType(ReportTypes)))
Although I think I would skip the ArrayList and simply add the values to the
Items collection of the ComboBox. Something like:
ComboBox1.Items.AddRange([Enum].GetValues(GetType(ReportTypes)))
Then in my Button_Click I would use the SelectedValue:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Select Case ComboBox1.SelectedItem
Case ReportTypes.Report1
MessageBox.Show("call Report1 procedure...")
Case ReportTypes.Report2
MessageBox.Show("call Report2 procedure...")
Case ReportTypes.Report3
MessageBox.Show("call Report3 procedure...")
Case Else
Throw New ArgumentOutOfRangeException("ReportType",
ComboBox1.SelectedItem, "Invalid Report Type in ComboBox1.")
End Select
End Sub
Private Sub MainForm_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
For Each item As ReportTypes In
[Enum].GetValues(GetType(ReportTypes))
ComboBox1.Items.Add(item)
Next
ComboBox1.SelectedItem = ReportTypes.Report1
End Sub
Note due to GetValues returns an array of Enum, and not Object, I had to use
a For Each.
Note: I am putting actual Enum values into the ComboBox, not the name of the
values, because the combo box will call Enum.ToString, the string name of
the enum will be displayed...
Hope this helps
Jay
Imran Koradia said:
Well - you can avoid (3) by doing something like:
For Each sReportType As String In [Enum].GetNames(ReportTypes)
arr.Add(sReportType)
Next sReportType
Also, IMHO, that's a bit more clean and also avoids the overhead that
reflection incurs. I wouldn't want to use reflection if I know the type
and
method I need to execute. I would usually resort to Reflection only when I
really need to (late binding because of unknown types, etc). Just a
thought..
Imran.
Rick Palmer said:
Yes, but the way you've show below, if I added a new report, I would
have to add it:
1) to the Enum
2) to the Case Select Statement
3) to the With... statement that fills the arraylist
in addition to writing the new code....That leaves two chances for me
to forget to add it somewhere and make myself look like like a dumbass
when it goes into production. The way I'm trying, you would only have
to add an entry to the combobox and add the code.
I actually figured out how to do this without using AddHandler.
Instead of changing handler for the button.click event, I just use one
handler, ion which I use System.Type.InvokeMember to call the
subroutine named in the combobox.
On Wed, 6 Oct 2004 15:00:17 -0400, "Imran Koradia"
Instead of doing that, you could do something like this:
Public Class Form1
Inherits System.Windows.Forms.Form
' omiting designer code..
' one button - Button1
' one combobox - Combobox1
Enum ReportTypes
Report1
Report2
Report3
End Enum
Private arr As New ArrayList
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
With arr
.Add(ReportTypes.Report1)
.Add(ReportTypes.Report2)
.Add(ReportTypes.Report3)
End With
ComboBox1.DataSource = arr
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Select Case ComboBox1.SelectedIndex
Case ReportTypes.Report1
' call Report1 procedure..
Case ReportTypes.Report2
' call Report2 procedure..
Case ReportTypes.Report3
' call Report3 procedure..
End Select
End Sub
End Class
hope that helps..
Imran.
I have an app I'm working on that will allow a user to run one of 5
reports. The report names are in a combobox on my form. I have a sub
defined for each report that has the exact same name as is displayed
in the combobox. I have one button on the form to start processing.
What I want to do is this: When the user selects the report they want
to run from the combobox, I want to dynamically bind the appropriate
sub to the button's click event. Since the sub names are the same as
the display names in the box, I'm wanting to do it like this:
Addhandler Button1.Click, combobox1.text
Unfortunately, that makes VB look for a sub called "combobox1.text",
instead of looking for the function named in combobox1.text. Anyone
know how to do what I'm trying to do????
-rp