美好的一天。任何人都可以帮我解决这个问题。我有两个组合框1。)一年2.)。这几个月我有一个代码。而且我希望它像循环一样简单,就像在2000年到2015年的组合框中获得一年一样。任何解决方案,建议......?
' I've tried this. but it shows 01-12 numbers in a combobox not January to December
For i As Integer = 1 To Date.Now.Month
ComboBox1.Items.Add(i)
Next
其余的代码......
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
ValueComboxformonth()
For i As Integer = 2000 To Date.Now.Year
ComboBox1.Items.Add(i)
Next
' I want my month combo box have a solution like the above for loop
' I've tried this. but it shows 01-12 numbers in a combobox not January to December
'For i As Integer = 1 To Date.Now.Month
' ComboBox1.Items.Add(i)
'Next
End Sub
Private Sub ValueComboxformonth()
ComboBox1.Items.Add(New forMonthlist("January", "01"))
ComboBox1.Items.Add(New forMonthlist("February", "02"))
ComboBox1.Items.Add(New forMonthlist("March", "03"))
ComboBox1.Items.Add(New forMonthlist("April", "04"))
ComboBox1.Items.Add(New forMonthlist("May", "05"))
ComboBox1.Items.Add(New forMonthlist("June", "06"))
ComboBox1.Items.Add(New forMonthlist("July", "07"))
ComboBox1.Items.Add(New forMonthlist("August", "08"))
ComboBox1.Items.Add(New forMonthlist("September", "09"))
ComboBox1.Items.Add(New forMonthlist("October", "10"))
ComboBox1.Items.Add(New forMonthlist("November", "11"))
ComboBox1.Items.Add(New forMonthlist("December", "12"))
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim nMonthStart As forMonthlist = CType(ComboBox1.SelectedItem, forMonthlist)
End Sub
End Class
for forMonthlist ....
Public Class forMonthlist
Private mText As String
Private mValue As String
Public Sub New(ByVal pText As String, ByVal pValue As String)
mText = pText
mValue = pValue
End Sub
Public ReadOnly Property Text() As String
Get
Return mText
End Get
End Property
Public ReadOnly Property Value() As String
Get
Return mValue
End Get
End Property
Public Overrides Function ToString() As String
Return mText
End Function
End Class
由于
答案 0 :(得分:2)
您可以使用DateTimeFomatInfo.GetMonthName
:
Dim formatInfo = System.Globalization.DateTimeFormatInfo.CurrentInfo
For i As Integer = 1 To Date.Today.Month
Dim monthName = formatInfo.GetMonthName(i)
ComboBox1.Items.Add(monthName)
Next
如果您有月份名称,并且想要它的整数值,则可以将其解析为DateTime
:
Dim month As Int32 = Date.ParseExact("July", "MMMM", CultureInfo.CurrentCulture).Month
如果您不想要本地化名称,可以使用CultureInfo.InvariantCulture
。
答案 1 :(得分:1)
作为一种选择,您可以使用DateTimeFormatInfo.MonthNames
查找文化的月份名称:
ComboBox1.DataSource = System.Globalization.CultureInfo.CurrentCulture _
.DateTimeFormat.MonthNames() _
.Where(Function(x) Not String.IsNullOrEmpty(x)) _
.Select(Function(x, i) _
New With {.Name = x, .Value = (i + 1).ToString("D2")}) _
.ToList()
Me.ComboBox1.DisplayMember = "Name"
Me.ComboBox1.ValueMember = "Value"
要查找所选月份的值,您只需使用SelectedValue
:
MessageBox.Show(ComboBox1.SelectedValue.ToString())
'It shows 07 for example for July
注意强>
DateTimeFormat.MonthNames
会返回13个月的数组。对于12个月的日历,数组的第13个元素是空字符串,因此我使用了Not String.IsNullOrEmpty(x)
。这样我们显示12个月,如果日历有12个月,我们显示13个月,如果日历有13个月。