Jul 27, 2008

Combobox

Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > .NET ( ASP.net VB.net )

free web hosting

Combobox

kvarnerexpress
Hi all

I 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.

Reply

miCRoSCoPiC^eaRthLinG
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



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Recent Queries:-
  1. vb.net add "month names" to a combobox - 113.72 hr back. (1)
Similar Topics

Keywords : combobox


    Looking for combobox

Searching Video's for combobox
advertisement



Combobox



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE