none
VB Date application RRS feed

  • Question

  • Hey all, can anyone please help me with the code for this application *below* I've created programs where, if you type in the date in a label box. It outputs the same text.. Not something that this is asking for. I just don't know how to start it off. Any explanation would be highly appreciated.

    Write a program that accepts two inputs: Date and month (both in numeric format).

    Validate the input to ensure days are within the range 1 - 31, and months are within the range 1 - 12. Send the user a message if the input values are invalid.

    1. Use an input box to accept the year.

      Output the format on two lines to say

      The U.S. date is

      mm/dd/yyyy

      Include a second button to switch the date format to one recognized elsewhere in the world. The new message should say

      The date is

      dd/mm/yyyy


    Saturday, November 4, 2017 5:52 PM

All replies

  • Hi

    Is this a school assignment?

    You say "I've created programs" - show the code you have so far.


    Regards Les, Livingston, Scotland

    Saturday, November 4, 2017 6:05 PM
  • We try to avoid to help students with code. 

    However, on this page is probably everything you need to know

    https://msdn.microsoft.com/en-us/library/zdtaw1bw(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1


    Success
    Cor


    Saturday, November 4, 2017 6:07 PM
  • Sounds like a class assignment. The best way to help you is to try to help you figure it out yourself. You need to make an effort and tell us what you are having difficulty with. Your question is actually multiple questions since you are asking how to do multiple things. People that attempt to help others usually don't like writing complete programs. Please be specific about what problem you need help with.


    Sam Hobbs
    SimpleSamples.Info

    Saturday, November 4, 2017 6:08 PM
  • Sorry Guys,

    Correct, this is an assignment. I'm not looking for a complete answer, just advice. I got my code, but the issue I'm having is that when I click a button.. the result just goes over the text, and does not pop up in another window.

    Public Class DateFormat
    
        Dim intDay As Integer = 4
        Dim intMonth As Integer = 11
    
        Private Sub DateFormat_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles BtnUSDate.Click
    
    
            If intDay = 4 AndAlso GetIntMonth() Then
                BtnUSDate.Text = "U.S. date is: 11/04/2017"
            End If
        End Sub
    
        Private Function GetIntMonth() As Integer
            Return intMonth
        End Function
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles BtnNonUS.Click
    
            If intDay = 4 AndAlso GetIntMonth() Then
                BtnNonUS.Text = "Non U.S. date is: 04/11/2017"
            End If
    
        End Sub
    
        Private Sub btnexit_Click(sender As Object, e As EventArgs) Handles BtnExit.Click
            Me.Close()
        End Sub
    End Class

    EDIT** I got it working how I wanted it too.. 

    Public Class DateFormat
    
        Dim intDay As Integer = 4
        Dim intMonth As Integer = 11
    
        Private Sub DateFormat_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles BtnUSDate.Click
    
    
            If intDay = 4 AndAlso GetIntMonth() Then
                LblMsg.Text = "U.S. date is: 11/04/2017"
            End If
        End Sub
    
        Private Function GetIntMonth() As Integer
            Return intMonth
        End Function
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles BtnNonUS.Click
    
            If intDay = 4 AndAlso GetIntMonth() Then
                LblMsg.Text = "The date is: 04/11/2017"
            End If
    
        End Sub
    
        Private Sub btnexit_Click(sender As Object, e As EventArgs) Handles BtnExit.Click
            Me.Close()
        End Sub
    End Class


    • Edited by teck_58 Saturday, November 4, 2017 8:41 PM
    Saturday, November 4, 2017 8:31 PM
  • Good. Yes, I see that you were putting the result into BtnUSDate and BtnNonUS but you fixed that by putting the result into LblMsg. So you have made progress understanding how things work.

    In Button1_Click you are checking intDay for 4. I am not sure but probably you don't want to do that. I assume that intDay is coming from the form and if it is coming from the same field then you probably need to modify something. I assume you are not asking about that and we don't see enough of the code to be sure what would be correct.



    Sam Hobbs
    SimpleSamples.Info

    Saturday, November 4, 2017 8:55 PM
  • Hi teck-58,

    According to your description, I am not sure what you want to do, you said that you want to write a program that accepts two inputs: Date and month (both in numeric format), validate the input to ensure days are within the range 1 - 31, and months are within the range 1 - 12.

    I suggest you to get the number of days in this month according to the year and month, you can use system.DateTime.DaysInMonth(Int32, Int32) method, and I so one simple that you can refer to

    Dim intDay As Integer
        Dim intMonth As Integer
        Dim intyear As Integer = 2017
        Dim days As Integer
    
        Private Sub Form12_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            intDay = TextBox2.Text
            LblMsg.Text = "The U.S Date is " & intMonth & "/" & intDay & "/" & intyear
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            intDay = TextBox2.Text
            LblMsg.Text = "The Date is " & intDay & "/" & intMonth & "/" & intyear
        End Sub
    
        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
            Me.Close()
        End Sub
    
        Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
            If Asc(e.KeyChar) <> 13 AndAlso Asc(e.KeyChar) <> 8 AndAlso Not IsNumeric(e.KeyChar) Then
                MessageBox.Show("Please enter numbers only")
                e.Handled = True
            End If
        End Sub
    
        Private Sub TextBox2_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox2.KeyPress
            If Asc(e.KeyChar) <> 13 AndAlso Asc(e.KeyChar) <> 8 AndAlso Not IsNumeric(e.KeyChar) Then
                MessageBox.Show("Please enter numbers only")
                e.Handled = True
            End If
        End Sub
    
        Private Sub TextBox1_Validated(sender As Object, e As EventArgs) Handles TextBox1.Validated
            If Convert.ToInt32(TextBox1.Text) >= 1 AndAlso Convert.ToInt32(TextBox1.Text) <= 12 Then
            Else
                MessageBox.Show("Please enter the correct Months!")
                Return
            End If
            intMonth = TextBox1.Text
            days = System.DateTime.DaysInMonth(intyear, intMonth)
        End Sub
    
        Private Sub TextBox2_Validated(sender As Object, e As EventArgs) Handles TextBox2.Validated
            If TextBox2.Text >= 1 AndAlso TextBox2.Text <= days Then
            Else
                MessageBox.Show("Please enter the correct days")
                Return
            End If
        End Sub

    Best Regards,

    Cherry

     


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, November 7, 2017 5:37 AM
    Moderator
  • Hi teck-58,

    According to your description, I am not sure what you want to do, you said that you want to write a program that accepts two inputs: Date and month (both in numeric format), validate the input to ensure days are within the range 1 - 31, and months are within the range 1 - 12.

    I suggest you to get the number of days in this month according to the year and month, you can use system.DateTime.DaysInMonth(Int32, Int32) method, and I so one simple that you can refer to

    Dim intDay As Integer
        Dim intMonth As Integer
        Dim intyear As Integer = 2017
        Dim days As Integer
    
        Private Sub Form12_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            intDay = TextBox2.Text
            LblMsg.Text = "The U.S Date is " & intMonth & "/" & intDay & "/" & intyear
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            intDay = TextBox2.Text
            LblMsg.Text = "The Date is " & intDay & "/" & intMonth & "/" & intyear
        End Sub
    
        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
            Me.Close()
        End Sub
    
        Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
            If Asc(e.KeyChar) <> 13 AndAlso Asc(e.KeyChar) <> 8 AndAlso Not IsNumeric(e.KeyChar) Then
                MessageBox.Show("Please enter numbers only")
                e.Handled = True
            End If
        End Sub
    
        Private Sub TextBox2_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox2.KeyPress
            If Asc(e.KeyChar) <> 13 AndAlso Asc(e.KeyChar) <> 8 AndAlso Not IsNumeric(e.KeyChar) Then
                MessageBox.Show("Please enter numbers only")
                e.Handled = True
            End If
        End Sub
    
        Private Sub TextBox1_Validated(sender As Object, e As EventArgs) Handles TextBox1.Validated
            If Convert.ToInt32(TextBox1.Text) >= 1 AndAlso Convert.ToInt32(TextBox1.Text) <= 12 Then
            Else
                MessageBox.Show("Please enter the correct Months!")
                Return
            End If
            intMonth = TextBox1.Text
            days = System.DateTime.DaysInMonth(intyear, intMonth)
        End Sub
    
        Private Sub TextBox2_Validated(sender As Object, e As EventArgs) Handles TextBox2.Validated
            If TextBox2.Text >= 1 AndAlso TextBox2.Text <= days Then
            Else
                MessageBox.Show("Please enter the correct days")
                Return
            End If
        End Sub

    Best Regards,

    Cherry

     


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Yea it is a pity that Microsoft MSFT's currently fail for beginners school assignments. 

    Success
    Cor


    Tuesday, November 7, 2017 7:53 AM