[英]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
如果日期范围是2018年3月30日至2017年6月1日,则组合框应显示
2017-2018
If the date range is June 1, 2018 between March 30, 2019 then the combo box should display 2018-2019
如果日期范围是2019年3月30日至2018年6月1日,则组合框应显示
2018-2019
How do I do this approach? 我该怎么做? Is it better to use a database for this or can it be hard-coded?
为此使用数据库更好还是可以对其进行硬编码?
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.