locked
Reset ComboBox value

    Question

  •  

    My question is how to reseting the value in combobox. In this code is to calculate the leapyear, when selecting the year  2004 in (combobox1.text)  and then select februray in (combobox2.text) it suspose to display 29 day in (combobox3.text) which is the day combobox.  But when i select the year 2005 or the other, the day still 29 didn;t change to 28 instead, and the other problem is that when i select the month first it giving me this error: "Input String Was Not in Correct Format" Can anyone help me with it

     

    Here is the Code:

     

    Dim LeapYear As Double = ToDouble(ComboBox1.Text)

    If (LeapYear Mod 4 = 0) And (ComboBox2.Text = "2") Then

     

    Select Case ComboBox2.Text

    Case "2"

    ComboBox3.Items.Clear()

    ComboBox3.Items.Add("1")

    ComboBox3.Items.Add("2")

    ComboBox3.Items.Add("3")

    ComboBox3.Items.Add("4")

    ComboBox3.Items.Add("5")

    ComboBox3.Items.Add("6")

    ComboBox3.Items.Add("7")

    ComboBox3.Items.Add("8")

    ComboBox3.Items.Add("9")

    ComboBox3.Items.Add("10")

    ComboBox3.Items.Add("11")

    ComboBox3.Items.Add("12")

    ComboBox3.Items.Add("13")

    ComboBox3.Items.Add("14")

    ComboBox3.Items.Add("15")

    ComboBox3.Items.Add("16")

    ComboBox3.Items.Add("17")

    ComboBox3.Items.Add("18")

    ComboBox3.Items.Add("19")

    ComboBox3.Items.Add("20")

    ComboBox3.Items.Add("21")

    ComboBox3.Items.Add("22")

    ComboBox3.Items.Add("23")

    ComboBox3.Items.Add("24")

    ComboBox3.Items.Add("25")

    ComboBox3.Items.Add("26")

    ComboBox3.Items.Add("27")

    ComboBox3.Items.Add("28")

    ComboBox3.Items.Add("29")

     

     

    End Select

    Else

    Select Case ComboBox2.Text

    Case "2"

    ComboBox3.Items.Clear()

    ComboBox3.Items.Add("1")

    ComboBox3.Items.Add("2")

    ComboBox3.Items.Add("3")

    ComboBox3.Items.Add("4")

    ComboBox3.Items.Add("5")

    ComboBox3.Items.Add("6")

    ComboBox3.Items.Add("7")

    ComboBox3.Items.Add("8")

    ComboBox3.Items.Add("9")

    ComboBox3.Items.Add("10")

    ComboBox3.Items.Add("11")

    ComboBox3.Items.Add("12")

    ComboBox3.Items.Add("13")

    ComboBox3.Items.Add("14")

    ComboBox3.Items.Add("15")

    ComboBox3.Items.Add("16")

    ComboBox3.Items.Add("17")

    ComboBox3.Items.Add("18")

    ComboBox3.Items.Add("19")

    ComboBox3.Items.Add("20")

    ComboBox3.Items.Add("21")

    ComboBox3.Items.Add("22")

    ComboBox3.Items.Add("23")

    ComboBox3.Items.Add("24")

    ComboBox3.Items.Add("25")

    ComboBox3.Items.Add("26")

    ComboBox3.Items.Add("27")

    ComboBox3.Items.Add("28")

     

    End Select

    End If

    Select Case ComboBox2.Text

    Case "1", "3", "5", "7", "8", "10", "12"

    ComboBox3.Items.Clear()

    ComboBox3.Items.Add("1")

    ComboBox3.Items.Add("2")

    ComboBox3.Items.Add("3")

    ComboBox3.Items.Add("4")

    ComboBox3.Items.Add("5")

    ComboBox3.Items.Add("6")

    ComboBox3.Items.Add("7")

    ComboBox3.Items.Add("8")

    ComboBox3.Items.Add("9")

    ComboBox3.Items.Add("10")

    ComboBox3.Items.Add("11")

    ComboBox3.Items.Add("12")

    ComboBox3.Items.Add("13")

    ComboBox3.Items.Add("14")

    ComboBox3.Items.Add("15")

    ComboBox3.Items.Add("16")

    ComboBox3.Items.Add("17")

    ComboBox3.Items.Add("18")

    ComboBox3.Items.Add("19")

    ComboBox3.Items.Add("20")

    ComboBox3.Items.Add("21")

    ComboBox3.Items.Add("22")

    ComboBox3.Items.Add("23")

    ComboBox3.Items.Add("24")

    ComboBox3.Items.Add("25")

    ComboBox3.Items.Add("26")

    ComboBox3.Items.Add("27")

    ComboBox3.Items.Add("28")

    ComboBox3.Items.Add("29")

    ComboBox3.Items.Add("30")

    ComboBox3.Items.Add("31")

    End Select

    Select Case ComboBox2.Text

    Case "4", "6", "9", "11"

    ComboBox3.Items.Clear()

    ComboBox3.Items.Add("1")

    ComboBox3.Items.Add("2")

    ComboBox3.Items.Add("3")

    ComboBox3.Items.Add("4")

    ComboBox3.Items.Add("5")

    ComboBox3.Items.Add("6")

    ComboBox3.Items.Add("7")

    ComboBox3.Items.Add("8")

    ComboBox3.Items.Add("9")

    ComboBox3.Items.Add("10")

    ComboBox3.Items.Add("11")

    ComboBox3.Items.Add("12")

    ComboBox3.Items.Add("13")

    ComboBox3.Items.Add("14")

    ComboBox3.Items.Add("15")

    ComboBox3.Items.Add("16")

    ComboBox3.Items.Add("17")

    ComboBox3.Items.Add("18")

    ComboBox3.Items.Add("19")

    ComboBox3.Items.Add("20")

    ComboBox3.Items.Add("21")

    ComboBox3.Items.Add("22")

    ComboBox3.Items.Add("23")

    ComboBox3.Items.Add("24")

    ComboBox3.Items.Add("25")

    ComboBox3.Items.Add("26")

    ComboBox3.Items.Add("27")

    ComboBox3.Items.Add("28")

    ComboBox3.Items.Add("29")

    ComboBox3.Items.Add("30")

    End Select

    End Sub

    Monday, October 29, 2007 11:27 PM

Answers

  • Your program has a multitude of redundant code.  You can reduce it to a fraction of the original size if you use a For loop to add the numbers up to a value for each of the months.  A short Select Case block can be used for finding that value.

     

    I redid your program using a textbox for the year input and a NumericUpDown control for the month input.  It seems to me that instead of using a combobox for the days, you should also use a NumericUpDown control for that too, and I included a commented-out statement for that.  The For loop is only needed for the ComboBox control, if you still want to use that.  I left it there to show you how it can work.  If you insist, you can revert my code back to using the rest of your combobox controls. 

     

    You also need to simply use a formula for the year instead of what I suspect is a list of leap years you might have placed in one of the combo boxes.  It's not simply:    LeapYear Mod 4 = 0  

    but a bit more complex than that.

     

    When the button is clicked, the combobox is populated with the correct list of data.

     

    If you want all the controls to work together without using the button click event, then you need to call the code as a Sub whenever a value is changed in any of the controls.

     

    Code Block

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim year, numdays As Integer

    Dim leapyear As Boolean = False

    Integer.TryParse(TextBox1.Text, year)

    If year Mod 4 = 0 And (year Mod 100 <> 0 Or year Mod 400 = 0) Then

       leapyear = True

    End If

    Select Case NumericUpDown1.Value

       Case 2

          If leapyear = True Then

             numdays = 29

          Else

             numdays = 28

          End If

       Case 4, 6, 9, 11

          numdays = 30

       Case Else

          numdays = 31

    End Select

    '  NumericUpDown2.Maximum = numdays

    ComboBox1.Items.Clear()

    For x As Integer = 0 To numdays - 1

       ComboBox1.Items.Add((x + 1).ToString)

    Next

    End Sub

     

     

    Tuesday, October 30, 2007 1:54 AM
  • Hi,

     

    You can use the Date.IsLeapYear function to determine if the year is a leap year or not.

    I think every so often certain years are not a leap year as you might expect.

    I think it is if the year ends with 2 zeros as in 2100 ( but I am not sure on this ).

     

    Anyway you should ideally rename your controls so you can follow the code better.

     

    Have the ComboBox names as something like;

    • cmboYearHired
    • cmboMonthHired
    • cmboDayHired
    • cmboYearReturned
    • cmboMonthReturned
    • cmboDayReturned

    Use the NAME property of each control to change them as you wish.

    It will be easier for you to follow your code then.

     

     

     

     

    Regards,

     

    John

     

    Wednesday, October 31, 2007 3:43 AM

All replies

  • Your program has a multitude of redundant code.  You can reduce it to a fraction of the original size if you use a For loop to add the numbers up to a value for each of the months.  A short Select Case block can be used for finding that value.

     

    I redid your program using a textbox for the year input and a NumericUpDown control for the month input.  It seems to me that instead of using a combobox for the days, you should also use a NumericUpDown control for that too, and I included a commented-out statement for that.  The For loop is only needed for the ComboBox control, if you still want to use that.  I left it there to show you how it can work.  If you insist, you can revert my code back to using the rest of your combobox controls. 

     

    You also need to simply use a formula for the year instead of what I suspect is a list of leap years you might have placed in one of the combo boxes.  It's not simply:    LeapYear Mod 4 = 0  

    but a bit more complex than that.

     

    When the button is clicked, the combobox is populated with the correct list of data.

     

    If you want all the controls to work together without using the button click event, then you need to call the code as a Sub whenever a value is changed in any of the controls.

     

    Code Block

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim year, numdays As Integer

    Dim leapyear As Boolean = False

    Integer.TryParse(TextBox1.Text, year)

    If year Mod 4 = 0 And (year Mod 100 <> 0 Or year Mod 400 = 0) Then

       leapyear = True

    End If

    Select Case NumericUpDown1.Value

       Case 2

          If leapyear = True Then

             numdays = 29

          Else

             numdays = 28

          End If

       Case 4, 6, 9, 11

          numdays = 30

       Case Else

          numdays = 31

    End Select

    '  NumericUpDown2.Maximum = numdays

    ComboBox1.Items.Clear()

    For x As Integer = 0 To numdays - 1

       ComboBox1.Items.Add((x + 1).ToString)

    Next

    End Sub

     

     

    Tuesday, October 30, 2007 1:54 AM
  • thanks you, that was helpful, can you also help me with this too?



    I suspose to use constant to declare the car, insurance and cost price which is 49.99 for car, 14.99 for insurance, and 2.65 for fuel on the Unit Cost columm, and when user select Under 25 on radio button, it suspose to charge extra 10 % than the original price, and for 65 or older give an discount 10%.  Can you help me with it please?
    Tuesday, October 30, 2007 4:22 AM
  • First of all, an unrelated question should be posted in a separate thread.

     

    Secondly, if your question was answered by my post, then you should mark it as answered.

     

    Third, is this a homework assignment?

    We will not do your homework for you.  You need to make an effort to write the code yourself and post it if you are having difficulty with any portion of it.

     

    I probably should not have posted the concise solution to your problem if you are going to take credit for it as a homework assignment.

     

     

     

    Tuesday, October 30, 2007 9:11 PM
  • Hi,

     

    You can use the Date.IsLeapYear function to determine if the year is a leap year or not.

    I think every so often certain years are not a leap year as you might expect.

    I think it is if the year ends with 2 zeros as in 2100 ( but I am not sure on this ).

     

    Anyway you should ideally rename your controls so you can follow the code better.

     

    Have the ComboBox names as something like;

    • cmboYearHired
    • cmboMonthHired
    • cmboDayHired
    • cmboYearReturned
    • cmboMonthReturned
    • cmboDayReturned

    Use the NAME property of each control to change them as you wish.

    It will be easier for you to follow your code then.

     

     

     

     

    Regards,

     

    John

     

    Wednesday, October 31, 2007 3:43 AM