Getting a value from a listbox

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

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
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

bhlabelleAuthor Commented:
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.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
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...
bhlabelleAuthor Commented:
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!
bhlabelleAuthor Commented:
Great $12 a month I spend.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.