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

Posted on 2007-08-07
Last Modified: 2013-11-05
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
Question by:jsctechy
    LVL 38

    Accepted Solution

    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
    LVL 1

    Author Comment

    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?
    LVL 38

    Expert Comment

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
    Article by: Martin
    Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now