Click here to Skip to main content
14,300,872 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have three combo boxes such that when I click on one, it will give a drop-down of years till 2099, and the other, months and the other, weeks that corresponds to the month selected for that year.

What I have tried:

This is what I tried...
for year as integer = DateTime. Now. Year To 2099 Step 1
cbxYear. Items. Add(year)

Dim i As Integer
Dim months As String
Do While i<=12
months =MonthName(i, True)
cbxMonth. Items. Add(months)

Please help!
Updated 11-Sep-19 9:32am
Richard MacCutchan 11-Sep-19 9:36am
"Please help!"
With what? You have not explained what the problem is.
Member 14587396 11-Sep-19 15:01pm
I explained it already but can still repeat it.
I have three combo boxes, I want it such that when I click on the one for year, it will give a drop-down of years till 2099 for selecting, and when I click the other for months, it will also give the drop-down of months in that year for selecting and finally when I click on the one for weeks, the corresponding number of weeks to the month selected for that year will come in a drop-down for selecting.
I seriously don't know how to do that.
What I tried doesn't do what I want it to.
Rate this:
Please Sign up or sign in to vote.

Solution 1

You can use the SelectedIndexChanged Event, see example here: ComboBox.SelectedIndexChanged Event (System.Windows.Forms) | Microsoft Docs[^]
Maciej Los 11-Sep-19 15:44pm
Rate this:
Please Sign up or sign in to vote.

Solution 3

You may want to utilize the Calenda class which "Represents time in divisions, such as weeks, months, and years.", and the GetWeekOfYear method to help you out as well- just give it the date in question and wallah you have the week number

Calendar Class (System.Globalization) | Microsoft Docs[^]
Member 14587396 11-Sep-19 11:34am
please, can you make an illustration for me in code.
I've checked the reference but can't really figure out what to do
Maciej Los 11-Sep-19 15:19pm
MadMyche 11-Sep-19 15:34pm
The logic you use is going to be up to you, and it is not up to me to right it; and I really do not know what the big picture is of your application to determine if this course of action is the correct one.

That said; if this was mine....
1. Years are going to be a static list, as well as months
2. Once the above are selected, I would get the week number based off of the 1st day of the selected month
MadMyche 11-Sep-19 15:34pm
Thank you
Rate this:
Please Sign up or sign in to vote.

Solution 4

Based on @RickZeeland and @MadMyche suggestion, i've created a function which returns an array of integers corresponding to the weeks for specific year and month.

Public Function GetWeeksNumbers(yr As Integer, mth As Integer) As Integer()
	Dim weeks As Dictionary(Of Integer, DateTime) = New Dictionary(Of Integer, DateTime)
	Dim curr As System.Globalization.CultureInfo = System.Globalization.CultureInfo.CurrentCulture
	Dim dtStart As DateTime = New DateTime(yr, mth, 1)
	Dim dtEnd As DateTime = dtStart.AddMonths(1).AddDays(-1)
	Dim dt As DateTime = dtStart
	Do While dt <= dtEnd
		Dim i As Integer  = curr.Calendar.GetWeekOfYear(dt, System.Globalization.CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday)
		If Not weeks.ContainsKey(i) Then
			weeks.Add(i, dt)
		End If
		dt = dt.AddDays(1)		
	Return weeks.Select(Function(kvp) kvp.Key).ToArray()
End Function

Dim weeks As Integer() = GetWeeksNumbers(2019, 9) 'replace constant values with values comes from corresponding comboboxes
Dim i As Integer = 0
With Me.ComboBoxWeeks.Items
    For i = LBound(weeks) To UBound(weeks)
End With
Member 14587396 12-Sep-19 4:19am
Maciej Los, thanks for your help so far but I get an error at
"Dim weeks As Integer () = GetWeeksNumbers( 2019 , 9 )"
The error is," Value of type 'Integer' cannot be converted to '1-dimensional array of Integer'
I replaced it with values from my combobox but still had that error.
Thanks for helping further!
Maciej Los 12-Sep-19 4:29am
My best guess is: a GetWeeksNumbers() method declaration is wrong. You probably define it this way:
Public Function GetWeeksNumbers(yr As Integer, mth As Integer) As Integer

instead of
Public Function GetWeeksNumbers(yr As Integer, mth As Integer) As Integer()

Do you see the difference?
Member 14587396 12-Sep-19 4:35am
Yeah, that is now fixed but the value to return in the function is now giving same error.
I returned 'weeks' in the function.
Am I wrong with that?
Maciej Los 12-Sep-19 5:49am
No. It returns an array of integers. 'weeks' is the name of variable which is used to store that array.
Member 14587396 12-Sep-19 5:02am
I tried to run the program without returning a value and it shows a warning but then another error at this line,
Dim dtStart As DateTime = New DateTime(yr, mth, 1 ).
The error is "Year, Month, and Day parameters describe an un-representable DateTime".
I do not understand that too.
Maciej Los 12-Sep-19 5:50am
Where do you use that line od code? Inside a function or outside of it?
Member 14587396 12-Sep-19 6:02am
inside the function GetWeeksNumber
Maciej Los 12-Sep-19 6:19am
It should works as well, because yr and mth are passed as a parameter to the function, unless you pass null (nothing). Use debugger to find out.
Member 14587396 12-Sep-19 6:33am
Please, sorry that I'm asking too much questions.
What value should I return in the GetWeeksNumber Function?
Maciej Los 12-Sep-19 6:41am
An array of integers.
Member 14587396 12-Sep-19 7:53am
Please, can you show how that is done with regards to the GetWeeksNumber Function.
I know you're completely bored by now but it's what a complete novice like me can cause.
Thank you for the help so far.
I'm getting to understand some things better.
Maciej Los 12-Sep-19 8:05am
Please, read my answer carefully. I already shown you how to use that. All you need to do is to move [Usage] part to the cbxMonth_SelectedIndexChange() procedure. Then you have to pass vaules of bomboboxes: cbxYear and cbxMonth to this line:
Dim weeks As Integer() = GetWeeksNumbers(cbxYear.SelectedValue, cbxMonth.SelectedValue)

But i need to warn you that SelectedValue may return null (nothing). So you have to check if SelectedValue of combobox is not null! For further details, please see: ComboBox SelectedItem, SelectedValue, SelectedWhat??? – jaredpar's WebLog[^]
Member 14587396 12-Sep-19 12:37pm
Thank you so much Sir.
I have it resolved now.
Maciej Los 12-Sep-19 14:04pm
You're very welcome.
Rate this:
Please Sign up or sign in to vote.

Solution 2

Start by looking at the TimeSpan struct: TimeSpan Struct (System) | Microsoft Docs[^] - it is what you get when you subtract two DateTime values.
So if you have the end of the period (2099/12/31 or 2099/01/01) and subtract the current date (DateTime.Now) you will get a TimeSpan value which will represent the number of days between them. That's not a lot of help to you directly, because it can't give you Weeks or Months, because they are "relative values" - they aren't an absolute number, because they can change quite markedly between today and tomorrow.

But ... this may help: Working with Age: it's not the same as a TimeSpan![^] the code is in C#, but it's pretty obvious and it does give you the numbers you need.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100