Link to home
Start Free TrialLog in
Avatar of jsctechy
jsctechyFlag for United States of America

asked on

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

hi,
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 = com.library.data.Tools.GetTableAccess()
        Dim dtrw As DataRow
        dtrw = dt.Rows(0)

        For Each dtrw In dt.Rows
            strFormName.Items.Add(dtrw("strTableName"))
        Next

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

    End Sub
ASKER CERTIFIED SOLUTION
Avatar of PaulHews
PaulHews
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jsctechy

ASKER

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)
    frm.ShowDialog()
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