First of all - how do u intend to implement JavaScript in VB.NET code ?
Anyways, what I'm providing below is pure VB.NET code - but can be used for ASP.NET as well - without any modifications.
Let us assume that your first combobox containing the year, is named
cboYear and the one containing month is named
cboMonth.
Now our tasks are:
1. To fill up the year combobox with a certain number of years, say starting from 1900 till the present year. This can be achieved using the following code:
CODE
Dim Count As Integer
For Count = 1900 To Val (Format(Now(), "yyyy"))
cboYear.Items.Add(Count)
Next
2. Next task is to fill up the combo for month with the Month names. Best way to do it is have an array containing the month names and then use a For..Next loop to fill up the combo.
CODE
Dim Months () As String = { "January", "February", ............... , "December"}
For Count = LBound(Months) To UBound(Months)
cboMonth.Items.Add (Months(Count))
Next
Now that our boxes are filled up, we need to implement a check for whenever the month combobox item is chosen. When a combo item is selected, one of the events thrown is
SelectedIndexChanged.
In the code editor of VS.NET, you have to select the cboMonth and then in the events dropdown list on the right pick this event. This will create a skeleton event handler procedure for you, which looks like:
CODE
Private Sub cboMonth_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboMonth.SelectedIndexChanged
End Sub
Inside this procedure you have to implement the check - the logic would be:
As soon as a month is selected, check if it is current month --> Next, check the current selected year in the cboYear --> If that matches the current year --> print error message or simply force the combo month to jump to the current month.
Here's the code:
CODE
Private Sub cboMonth_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboMonth.SelectedIndexChanged
' Check whether selected month > present month
If cboMonth.SelectedIndex > Val (Format(Now(), "M") - 1 Then
' Check whether selected year = current year
If cboYear.SelectedItem.Equals ( Format (Now(), "yyyy" ) Then
' Print error message
MsgBox ( "Error. Cannot select above current month of this year")
' Make month combo jump to current month
cboMonth.SelectedIndex = Val (Format(Now(), "M") - 1
End If
End If
End Sub
That code should solve your problem - if any month greater than that of current year is chosen, that message box will pop up and as a precautionary step the combobox will be forced to assume the current month. No amount of trying will help you choose a month above that.
Hope this helps. Let me know if you need any further clarifications on this.
Regards,
m^e
Reply