繁体   English   中英

VB.NET如何在组合框中显示正确的数据

[英]VB.NET how to display proper data in combo box

我有一个包含以下内容的组合框

2017-2018
2018-2019
2019-2020

我要发生的是,它必须根据特定的日期范围在组合框上显示正确的数据。 例如:

如果日期范围是2018年3月30日至2017年6月1日,则组合框应显示2017-2018

如果日期范围是2019年3月30日至2018年6月1日,则组合框应显示2018-2019

我该怎么做? 为此使用数据库更好还是可以对其进行硬编码?

尝试这个:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM