Jump to content
xisto Community
Sign in to follow this  
kvarnerexpress

Combobox

Recommended Posts

Hi allI m having two combobox on my page cmbyear(years) and cmbmon(months). i want to make a check that if year selected is current year then selected month should not be greater than current month.how we can achieve this.one way out is that we can store values of selected options in two variables in javascript function. In the function we can make the check that if selected value for year is current year and selected value for month is greater than current month then it ll show some error message and reload the page i m trying this but do not get results. May be i m having some mistake in syntax. can anyone tell me how to get selected value in a variable in javascript or some other way out to achieve this.I have applied same technique on text box value. its working fine. but with combobox.......... i dont know whats the prob.

Share this post


Link to post
Share on other sites

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:

Dim Count As IntegerFor 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.

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:

Private Sub cboMonth_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboMonth.SelectedIndexChangedEnd 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:

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 IfEnd 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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.