Getting a value from a listbox

Posted on 2009-12-16
Last Modified: 2012-05-08
I have a form with a datagrid that has a list of bank accounts.  I want to be able to open monthly statements when I double-click the grid.  The only thing I need is for the user to pick the month they want to open.  I was hoping to bring up a listbox with the 12 months, and when the user selects the month, I want to be able to identify what month they selected.
Private Sub TblTBDataGridView_CellContentDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles TblTBDataGridView.CellContentDoubleClick

        Dim AcctNum As String = TblTBDataGridView.Rows(e.RowIndex).Cells(2).Value

        Dim BankName As String = TblTBDataGridView.Rows(e.RowIndex).Cells(1).Value

        Dim SMonth 'this is what I need to get from user

        Dim ErrMessage

        Dim MResponse



        'This is where I need to prompt the user to select the month


            Process.Start("Y:\Documents\Statements\" & BankName & "\" & SMonth & "\" & AcctNum & ".pdf")


            ErrMessage = MsgBox("Document not found!", MsgBoxStyle.OkOnly, "Error")

        End Try

    End Sub

Open in new window

Question by:bhlabelle
    LVL 6

    Accepted Solution

    I don't think this is possible with existing functions. You can use an inputbox ( ) to ask the user what month they want to open with a text field (so e.g. 1-12). (see code below).

    Otherwise you would have to create a form with a listbox youself.

    SMonth = inputbox("What month do you want to open?")
    If SMonth <> "" Then
    	If IsNumeric(SMonth) Then
    		if SMonth < 1 or SMonth > 12 then
    			msgbox("Incorrect month")
    		end if
    		msgbox("Incorrect month")
    	end if
    	'no input, cancel sub?
    	exit sub
    end if

    Open in new window

    LVL 85

    Assisted Solution

    by:Mike Tomlinson
    You could just make a simple form with a ListBox and the Months in it then call it using ShowDialog()...

    Here is an example with a dynamic form:
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim month As String = GetMonthFromUser()
            If Not IsNothing(month) Then
            End If
        End Sub
        Private Function GetMonthFromUser() As String
            Using frm As New Form
                frm.Text = "Double Click Month"
                frm.Size = New Size(200, 200)
                frm.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedDialog
                frm.MinimizeBox = False
                frm.MaximizeBox = False
                Dim lb As New ListBox
                lb.Dock = DockStyle.Fill
                AddHandler lb.DoubleClick, AddressOf Me.lb_DoubleClick
                If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    Return frm.Tag
                End If
            End Using
            Return Nothing
        End Function
        Private Sub lb_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs)
            Dim lb As ListBox = CType(sender, ListBox)
            Dim pt As Point = lb.PointToClient(Cursor.Position)
            For i As Integer = 0 To lb.Items.Count - 1
                If lb.GetItemRectangle(i).Contains(pt) Then
                    lb.Parent.Tag = lb.Items(i)
                    CType(lb.Parent, Form).DialogResult = Windows.Forms.DialogResult.OK
                End If
        End Sub
    End Class

    Open in new window


    Author Comment

    The input box idea worked very nicely,  I will also try the post by idle mind and whichever one works the best I will accept as the solution (although I bet they both will work fine).  Might take until tonight as I am pretty swamped today.
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    I think the best solution would be an actual Form that you design beforehand and display with ShowDialog() (instead of a dynamic one like in my example).  This way you can control the size/font/etc...

    Author Comment

    Well the application is only for a small office, where people are afraid to open Access databases, and can't remember 10 minutes after saving a pdf where it went.  I like the solution posted by Idle Mind a little better because people here will only have to double click the's best to give them fewer things to do.  

    This hack programmer really appreciates all of the anwers I get from this site.  It makes me look a lot smarter than I am!

    Author Closing Comment

    Great $12 a month I spend.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Suggested Solutions

    Introduction In a recent article ( for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
    Introduction While answering a recent question ( in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
    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…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    755 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

    20 Experts available now in Live!

    Get 1:1 Help Now