• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 165
  • Last Modified:

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

  • 3
  • 2
2 Solutions
I don't think this is possible with existing functions. You can use an inputbox (http://msdn.microsoft.com/en-us/library/3yfdhzk5%28VS.85%29.aspx ) 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

Mike TomlinsonMiddle School Assistant TeacherCommented:
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.
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Mike TomlinsonMiddle School Assistant TeacherCommented:
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 month....it'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 site...best $12 a month I spend.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now