Solved

Get months and year from a combobox and put in a list

Posted on 2013-10-23
3
788 Views
Last Modified: 2013-10-28
I have a 2 comboboxes that contain a month, year.  I need to get a range of the months/years between the 2 combobox's.

combobox1=May,2013
combobox2=August,2013

So I would like a set a variable in a list that contains these dates in a date format.
Something like
Dim mydates As ArrayList
                    mydates.Add("May" & "2013")
                    mydates.Add("June" & "2013")
                    mydates.Add("July" & "2013")
                    mydates.Add("August" & "2013")

I just need these in a date format so I can use them late in a date function.  I'm not sure how to calculate the range to put into the list.
0
Comment
Question by:chadmanvb
3 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
Comment Utility
Try something like this:
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        If ComboBox1.SelectedIndex <> -1 AndAlso ComboBox2.SelectedIndex <> -1 Then
            Dim myDates As New List(Of String)

            Dim startDT As DateTime
            Dim stopDT As DateTime
            Dim English As New System.Globalization.CultureInfo("en-US")
            If DateTime.TryParseExact(ComboBox1.SelectedItem, "MMMM,yyyy", English, Globalization.DateTimeStyles.AllowWhiteSpaces, startDT) Then
                If DateTime.TryParseExact(ComboBox2.SelectedItem, "MMMM,yyyy", English, Globalization.DateTimeStyles.AllowWhiteSpaces, stopDT) Then
                    While startDT <= stopDT
                        myDates.Add(startDT.ToString("MMMM,yyyy", English))
                        startDT = startDT.AddMonths(1)
                    End While
                End If
            End If
            For Each dt As String In myDates
                Debug.Print(dt)
            Next
        End If
    End Sub

Open in new window

0
 
LVL 27

Expert Comment

by:Ark
Comment Utility
Just simplified way if
a) both combos have same date set
b) dates are in continuous ascending order (month by month without skips)
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim months = System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat.MonthNames
        Dim years = "2012,2013,2014".Split(","c)
        For Each y In years
            For Each m In months.Where(Function(x) x <> "")
                ComboBox1.Items.Add(m & "," & y)
                ComboBox2.Items.Add(m & "," & y)
            Next
        Next
        ComboBox1.SelectedIndex = 0
        ComboBox2.SelectedIndex = 0
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim dtStart As Date
        Dim lstDates As New List(Of Date)
        If DateTime.TryParseExact(ComboBox1.Items(0), "MMMM,yyyy",
                                  Globalization.CultureInfo.InvariantCulture,
                                  Globalization.DateTimeStyles.None,
                                  dtStart) Then
            For i = ComboBox1.SelectedIndex To ComboBox2.SelectedIndex
                lstDates.Add(dtStart.AddMonths(i))
            Next
        End If
        MsgBox("You select: " & vbCrLf & String.Join(vbCrLf, (From x In lstDates Select x.ToString("MMMM,yyyy"))))
    End Sub

Open in new window

Actually, in this case (when a) and b) are true, or at least b) is true) you can store just combo indexes. When form is loaded, you can get baseDate=DateTime.ParseExact(combo1.items(0))
and then use in a date function like
For i=firstComboIndex to secondComboIndex
     YourDateFunction(baseDate.AddMonth(i))
Next

If combos have different date set, you can use

Dim baseDate = New Date(2010,1,1)
combo1Offset = 24 'months - assuming combo1 starts from Jan,2012
combo2Offset = 30 'months - assuming combo2 starts from Jul,2012

For i=firstComboIndex+combo1Offset to secondComboIndex + combo2Offset
     YourDateFunction(baseDate.AddMonth(i))
Next
0
 

Author Closing Comment

by:chadmanvb
Comment Utility
Thanks!  That worked great.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

772 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

12 Experts available now in Live!

Get 1:1 Help Now