我正在創建一個包含兩個日期選擇器控制元件的可視化基本表單。我不喜歡用戶必須滾動日歷才能找到正確的日期。在我必須輸入日期(通常是出生日期)的各種網站上,將有 3 個“撥號”,每個月、日和年各一個,您可以滾動每一個,然后選擇您想要的日期。有誰知道這是否可以在vb中設定?我已經在互聯網上搜索過日期選擇器的檔案,但我找不到任何東西。我很高興知道:a)這可以在visual basic中完成,b)如果是這樣,我可以在哪里找到如何做的說明。
uj5u.com熱心網友回復:
與您自己的答案一致的實際解決方案:
設計器中的控制元件:
DateTimePicker1 As DateTimePicker
YearComboBox As ComboBox
MonthComboBox As ComboBox
DayComboBox As ComboBox
Imports System.Globalization
Private earliestYear As Integer = 1900
Private latestYear As Integer = DateTime.Now.Year ' x ' if going x years into the future
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
setDataSources(DateTime.Today)
End Sub
Private Sub setDataSources(d As DateTime)
RemoveHandler YearComboBox.SelectedValueChanged, AddressOf YearMonthComboBox_SelectedValueChanged
RemoveHandler MonthComboBox.SelectedValueChanged, AddressOf YearMonthComboBox_SelectedValueChanged
RemoveHandler DayComboBox.SelectedValueChanged, AddressOf DayComboBox_SelectedValueChanged
Dim yearDataSource = Enumerable.
Range(earliestYear, DateTime.Now.Year - earliestYear 1).
OrderByDescending(Function(y) y).
ToList()
YearComboBox.DataSource = yearDataSource
YearComboBox.SelectedItem = d.Year
Dim monthDataSource = DateTimeFormatInfo.CurrentInfo.MonthNames.
Where(Function(month) Not String.IsNullOrEmpty(month)).
Select(Function(month, i) New KeyValuePair(Of Integer, String)(i 1, month)).
ToList()
MonthComboBox.DisplayMember = "Value"
MonthComboBox.ValueMember = "Key"
MonthComboBox.DataSource = monthDataSource
MonthComboBox.SelectedValue = d.Month
Dim dayDataSource = Enumerable.
Range(1, DateTime.DaysInMonth(d.Year, d.Month)).
ToList()
DayComboBox.DataSource = dayDataSource
DayComboBox.SelectedItem = d.Day
AddHandler YearComboBox.SelectedValueChanged, AddressOf YearMonthComboBox_SelectedValueChanged
AddHandler MonthComboBox.SelectedValueChanged, AddressOf YearMonthComboBox_SelectedValueChanged
AddHandler DayComboBox.SelectedValueChanged, AddressOf DayComboBox_SelectedValueChanged
End Sub
Private Sub YearMonthComboBox_SelectedValueChanged(sender As Object, e As EventArgs)
Dim daysInMonth = DateTime.DaysInMonth(CInt(YearComboBox.SelectedItem), CInt(MonthComboBox.SelectedValue))
Dim selectedDay = CInt(DayComboBox.SelectedItem)
Dim dayDataSource = Enumerable.Range(1, daysInMonth).ToList()
DayComboBox.DataSource = dayDataSource
DayComboBox.SelectedItem = Math.Min(daysInMonth, selectedDay)
DateTimePicker1.Value = New DateTime(CInt(YearComboBox.SelectedItem), CInt(MonthComboBox.SelectedValue), CInt(DayComboBox.SelectedItem))
End Sub
Private Sub DayComboBox_SelectedValueChanged(sender As Object, e As EventArgs)
DateTimePicker1.Value = New DateTime(CInt(YearComboBox.SelectedItem), CInt(MonthComboBox.SelectedValue), CInt(DayComboBox.SelectedItem))
End Sub
Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged
setDataSources(DateTimePicker1.Value)
End Sub
考慮到每月的天數和閏年,與每個控制元件的互動將相應地更新其他控制元件。
uj5u.com熱心網友回復:
不是一個確切的解決方案,但它的作業原理非常緊密。我沒有使用 DatePicker 控制元件,而是使用了三個組合框 - 每個月、日和年各一個。這些組合框在表單加載時加載。一旦用戶為每個值選擇了合適的值(必須進行一些檢查以消除諸如 2 月 30 日之類的事情),我將三個框中的值連接起來并轉換為日期格式。這似乎作業得很好。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/388604.html
標籤:网络
