4 Replies - 563 Views - Last Post: 25 January 2013 - 10:31 AM Rate Topic: -----

#1 Un_dead  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-October 12

Putting individual sections of a date (yyyy/mm/dd) from Combo Boxes

Posted 25 January 2013 - 01:46 AM

Hey guys,
I've hit a hitch in my computing project. I want to actually create a date out of values held in combo boxes.

Posted Image

Now, the "Year" combo box is filled at the frmAddCustomer_Load event using this code:
Dim DateYearIndex As Integer
        For DateYearIndex = 1900 To Today.Year
            cmbYear.Items.Add(DateYearIndex)
        Next



At the cmbYear_SelectedIndexChanged event, the cmbMonth control is filled using:

Dim DateMonthIndex as Integer

For DateMonthIndex = 1 To 12
     cmbMonth.Items.Add(DateMonthIndex)
Next       



And the cmbDay control is filled in the same way.

Now, my question is: how would one take all the individual integers (DateYearIndex, DateMonthIndex, DateDayIndex) and combine them to make a single date (dd/mm/yyyy)?

I've tried this code:
  
Dim Year, Month, Day As Integer
Dim DateOfBirth As New Date(Day, Month, Year)

        Year = cmbYear.Text
        Month = cmbMonth.Text
        Day = cmbDay.Text

        lblShowDate.Text = DateOfBirth



but it gives me this error:

Posted Image

Could anyone shed some light on what might be happening, and how I could solve this? I'm pretty confident with VB.NET, but I've not been programming very long. Do I need to make a custom format, or is there a special process for converting integers into a date?


Thanks people! :)/>/>

Is This A Good Question/Topic? 0
  • +

Replies To: Putting individual sections of a date (yyyy/mm/dd) from Combo Boxes

#2 maxrock  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 8
  • View blog
  • Posts: 70
  • Joined: 13-September 09

Re: Putting individual sections of a date (yyyy/mm/dd) from Combo Boxes

Posted 25 January 2013 - 02:56 AM

Easy!!

Assign values first and use the method properly!!

Its
 Dim DateOfBirth As New Date(Year, Month, Day)


Regards,
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3480
  • View blog
  • Posts: 11,843
  • Joined: 12-December 12

Re: Putting individual sections of a date (yyyy/mm/dd) from Combo Boxes

Posted 25 January 2013 - 04:31 AM

        Dim dateOfBirth As DateTime = New Date(CInt(cmbYear.Text), CInt(cmbMonth.Text), CInt(cmbDay.Text))

        lblShowDate.Text = String.Format("{0:dd/MM/yyyy}", dateOfBirth)


I would use camelCase for local variables, particular to help distinguish my variable year (even dtmYear) from the property .Year.

Capitalization Conventions

Of course, you'll need to check for invalid dates, 31st February?! You can either use IsDate() or try to convert the combination of values to a DateTime instance and use Try..Catch if the date is not valid.

This post has been edited by andrewsw: 25 January 2013 - 04:34 AM

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3480
  • View blog
  • Posts: 11,843
  • Joined: 12-December 12

Re: Putting individual sections of a date (yyyy/mm/dd) from Combo Boxes

Posted 25 January 2013 - 04:38 AM

  
Dim Year, Month, Day As Integer
Dim DateOfBirth As New Date(Day, Month, Year)

        Year = cmbYear.Text
        Month = cmbMonth.Text
        Day = cmbDay.Text

        lblShowDate.Text = DateOfBirth




This is incorrect because Year, Month Day will have default values of 0, and you should explicitly convert cmbYear.Text, etc., to integers before storing them in the integer variables. Similarly, you should convert DateOfBirth to a string before assigning it to a label's Text.
Was This Post Helpful? 0
  • +
  • -

#5 Un_dead  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-October 12

Re: Putting individual sections of a date (yyyy/mm/dd) from Combo Boxes

Posted 25 January 2013 - 10:31 AM

View Postandrewsw, on 25 January 2013 - 11:31 AM, said:

Of course, you'll need to check for invalid dates, 31st February?! You can either use IsDate() or try to convert the combination of values to a DateTime instance and use Try..Catch if the date is not valid.



This is already achieved through these procedures :P/>

The Year integer is passed through this procedure:
 
Public Sub LeapYearCheck(ByVal SelectedYear As Integer, ByRef ConfirmedLeapYear As Boolean)
        ConfirmedLeapYear = False
        If SelectedYear Mod 4 = 0 Then
            If SelectedYear Mod 100 <> 0 Then
                ConfirmedLeapYear = True
            Else
                If SelectedYear Mod 400 = 0 Then
                    ConfirmedLeapYear = True
                Else
                    ConfirmedLeapYear = False
                End If
            End If
        Else
            ConfirmedLeapYear = False
        End If
    End Sub



And then the month is passed through this procedure:

Public Sub MonthCheck(ByVal SelectedMonthNumber As Short, ByVal ConfirmedLeapYear As Boolean, ByRef NumberOfDays As Short)
        Select Case SelectedMonthNumber
            Case 1, 3, 5, 7, 8, 10, 12
                NumberOfDays = 31
            Case 2
                If ConfirmedLeapYear = True Then
                    NumberOfDays = 29
                Else
                    NumberOfDays = 28
                End If
            Case 4, 6, 9, 11
                NumberOfDays = 30
        End Select
    End Sub



Quote

This is incorrect because Year, Month Day will have default values of 0, and you should explicitly convert cmbYear.Text, etc., to integers before storing them in the integer variables. Similarly, you should convert DateOfBirth to a string before assigning it to a label's Text.


Do you mean I should define the values for the year, month, day, and then use declare the new date?

Un_dead
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1