NoobCoder
NoobCoder

Reputation: 73

VB.NET how to display proper data in combo box

I have a combo box that contains the following

2017-2018
2018-2019
2019-2020

What I want to happen is that it must display the proper data on the combo box based on a specific date range. For example:

If the date range is June 1, 2017 between March 30, 2018 then the combo box should display 2017-2018

If the date range is June 1, 2018 between March 30, 2019 then the combo box should display 2018-2019

How do I do this approach? Is it better to use a database for this or can it be hard-coded?

Upvotes: 0

Views: 73

Answers (1)

Major Fifth
Major Fifth

Reputation: 90

Try this:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim dtDate As Date
    Date.TryParse(TextBox1.Text, dtDate)
    ComboBox1.SelectedItem = DoDate(dtDate)
End Sub

Private Function DoDate(ByVal dtInputDate As Date) As String
    Dim intYear As Integer = dtInputDate.Year
    If dtInputDate >= New Date(intYear, 6, 1) And dtInputDate <= New Date(intYear, 12, 31) Then
        Return CStr(intYear) & "-" & CStr(intYear + 1)
    ElseIf dtInputDate >= New Date(intYear, 1, 1) And dtInputDate <= New Date(intYear, 3, 30) Then
        Return CStr(intYear - 1) & "-" & CStr(intYear)
    Else
        MsgBox("ERROR: Date must be between 1 Jan and 30 Mar, or 1 June to 31 Dec.")
        Return ""
    End If
End Function

Upvotes: 1

Related Questions