Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Getting a value from a listbox

Posted on 2009-12-16
6
Medium Priority
?
161 Views
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
        
        'SMonth
        'This is where I need to prompt the user to select the month
 
        Try
            Process.Start("Y:\Documents\Statements\" & BankName & "\" & SMonth & "\" & AcctNum & ".pdf")
        Catch
            ErrMessage = MsgBox("Document not found!", MsgBoxStyle.OkOnly, "Error")
        End Try
    End Sub

Open in new window

0
Comment
Question by:bhlabelle
  • 3
  • 2
6 Comments
 
LVL 6

Accepted Solution

by:
iandian earned 1200 total points
ID: 26062771
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
	else
		msgbox("Incorrect month")
	end if
else
	'no input, cancel sub?
	exit sub
end if

Open in new window

0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 800 total points
ID: 26063053
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
            MessageBox.Show(month)
        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
            lb.Items.Add("January")
            lb.Items.Add("February")
            lb.Items.Add("March")
            lb.Items.Add("April")
            lb.Items.Add("May")
            lb.Items.Add("June")
            lb.Items.Add("July")
            lb.Items.Add("August")
            lb.Items.Add("September")
            lb.Items.Add("October")
            lb.Items.Add("November")
            lb.Items.Add("December")
            AddHandler lb.DoubleClick, AddressOf Me.lb_DoubleClick
            frm.Controls.Add(lb)
            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
        Next
    End Sub
 
End Class

Open in new window

0
 

Author Comment

by:bhlabelle
ID: 26063165
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.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 26063202
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...
0
 

Author Comment

by:bhlabelle
ID: 26066128
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!
0
 

Author Closing Comment

by:bhlabelle
ID: 31666723
Great site...best $12 a month I spend.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
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…
Suggested Courses

810 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