ListBox, how to get the item pick and do something with it

How can i show in a message box the item selected on a listBox?

here is the code i'm using:

 Private Sub frmTableMaintenance_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown

        Dim dt As DataTable =
        Dim dtrw As DataRow
        dtrw = dt.Rows(0)

        For Each dtrw In dt.Rows

        MsgBox("You have Selected: " & strFormName.SelectedItem)

    End Sub
Who is Participating?
You've just added items to the listbox, so there shouldn't be any selecteditem property at this point.  The thing to do would be trap the SelectedIndexChangedevent:

Private Sub strFormName_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strFormName.SelectedIndexChanged
    If strFormName.SelectedIndex > -1 Then
        MsgBox("You've selected " & strFormName.SelectedItem.ToString)
    End If
End Sub
jsctechyAuthor Commented:
if i need to open a form with the name that gets passed on the msgBox how can i do this?

for nstance lets said my first value is frmTax then once i double click on it the message box will display but instead of doing this i want to display the form that has the same name?
If you really need to do that you can do it this way:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim frm As Form = CType(System.Reflection.Assembly.GetExecutingAssembly.CreateInstance("WindowsApplication1.Form2"), Form)
End Sub

Because it is declared as Form and not as Form2, you will not be able to access any custom properties or methods of Form2.

With a little more code, you can have strongly typed forms:
Select Case FormName
    Case "Form2"
        Dim frm As Form2
        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
            'use custom methods of Form2...
        End If
    Case "Form3"
End Select
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.