Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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

Posted on 2007-08-07
Medium Priority
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 = com.library.data.Tools.GetTableAccess()
        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
  • 2
LVL 38

Accepted Solution

PaulHews earned 1000 total points
ID: 19649799
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

Author Comment

ID: 19649859
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

ID: 19653065
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

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses
Course of the Month11 days, 10 hours left to enroll

564 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