我可以使用 "dials" 到 select 日期吗?
Can I use "dials" to select date?
我正在创建一个包含两个日期选择器控件的可视化基本表单。我不喜欢用户必须滚动日历才能找到正确的日期。在我必须输入日期(通常是出生日期)的各种网站上,会有 3 个“表盘”,每个代表月、日和年,你可以滚动每个,然后 select你想要的日期。有谁知道这是否可以在 vb 中设置?我已经在互联网上搜索过,也通过日期选择器的文档进行了搜索,但我找不到任何东西。我很想知道是否:a) 这可以在 visual basic 中完成,b) 如果可以,我可以在哪里找到如何做的说明。
不是一个精确的解决方案,但它工作得非常紧密。我没有使用 DatePicker 控件,而是使用了三个组合框 - 每个月、日和年。这些组合框在表单加载时加载。一旦用户为每个选择了合适的值(必须进行一些检查以消除诸如 2 月 30 日之类的东西),我将三个框中的值连接起来并转换为日期格式。这似乎工作得很好。
按照您自己的答案的实际解决方案:
设计器中的控件:
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
与每个控件的交互将相应地更新其他控件,同时考虑每月的天数和闰年。
我正在创建一个包含两个日期选择器控件的可视化基本表单。我不喜欢用户必须滚动日历才能找到正确的日期。在我必须输入日期(通常是出生日期)的各种网站上,会有 3 个“表盘”,每个代表月、日和年,你可以滚动每个,然后 select你想要的日期。有谁知道这是否可以在 vb 中设置?我已经在互联网上搜索过,也通过日期选择器的文档进行了搜索,但我找不到任何东西。我很想知道是否:a) 这可以在 visual basic 中完成,b) 如果可以,我可以在哪里找到如何做的说明。
不是一个精确的解决方案,但它工作得非常紧密。我没有使用 DatePicker 控件,而是使用了三个组合框 - 每个月、日和年。这些组合框在表单加载时加载。一旦用户为每个选择了合适的值(必须进行一些检查以消除诸如 2 月 30 日之类的东西),我将三个框中的值连接起来并转换为日期格式。这似乎工作得很好。
按照您自己的答案的实际解决方案:
设计器中的控件:
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
与每个控件的交互将相应地更新其他控件,同时考虑每月的天数和闰年。