Reputation: 302
Good day. Anyone can help me with this problem. I have two combo box 1.) for month 2.) for year. I have a code here for months. And I want it to make simple like a loop, Just like getting a year from 2000 to 2015 in the year combo box. Any solutions, suggestions...?
' 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
For the rest of the code......
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 the 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
Thanks
Upvotes: 1
Views: 2849
Reputation: 125342
As an option you can use DateTimeFormatInfo.MonthNames
to find month names for a culture:
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"
To find the value of selected month you can simply use SelectedValue
:
MessageBox.Show(ComboBox1.SelectedValue.ToString())
'It shows 07 for example for July
Note
DateTimeFormat.MonthNames
returns an array with 13 month. For a 12 month calendar, the 13th element of array is empty string, so I used Not String.IsNullOrEmpty(x)
. This way we show 12 month if the calendar have 12 months and we show 13 month if the calendar have 13 month.Upvotes: 1
Reputation: 460380
You can use 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
If you have a month-name and you want it's integer-value you can parse it to a DateTime
:
Dim month As Int32 = Date.ParseExact("July", "MMMM", CultureInfo.CurrentCulture).Month
If you don't want localized names you could use CultureInfo.InvariantCulture
.
Upvotes: 2