Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Studio
  4. If condition

If condition

Scheduled Pinned Locked Moved Visual Studio
question
4 Posts 3 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    VSLearner2013
    wrote on last edited by
    #1

    How can I shorten this lengthy code, which is not right as well? Though I want to write it using if condition / select case instead of using DateTimePicker. It starts from 7:00 AM, 7:15 AM, 7:30 AM, 7:45 AM, 8:00 AM ................So on until 7:00 PM.

    Private Sub txtStartTime_Leave(sender As Object, e As EventArgs) Handles txtStartTime.Leave
    If txtStartTime.Text = 7 Then
    txtStartTime.Text = "7:00 AM"
    ElseIf txtStartTime.Text = 715 Then
    txtStartTime.Text = "7:15 AM"
    ElseIf txtStartTime.Text = 730 Then
    txtStartTime.Text = "7:30 AM"
    ElseIf txtStartTime.Text = 745 Then
    txtStartTime.Text = "7:45 AM"
    ElseIf txtStartTime.Text = 8 Then
    txtStartTime.Text = "8:00 AM"
    ElseIf txtStartTime.Text = 815 Then
    txtStartTime.Text = "8:15 AM"
    ElseIf txtStartTime.Text = 830 Then
    txtStartTime.Text = "8:30 AM"
    ElseIf txtStartTime.Text = 845 Then
    txtStartTime.Text = "8:45 AM"
    End If
    End Sub

    Richard DeemingR L 2 Replies Last reply
    0
    • V VSLearner2013

      How can I shorten this lengthy code, which is not right as well? Though I want to write it using if condition / select case instead of using DateTimePicker. It starts from 7:00 AM, 7:15 AM, 7:30 AM, 7:45 AM, 8:00 AM ................So on until 7:00 PM.

      Private Sub txtStartTime_Leave(sender As Object, e As EventArgs) Handles txtStartTime.Leave
      If txtStartTime.Text = 7 Then
      txtStartTime.Text = "7:00 AM"
      ElseIf txtStartTime.Text = 715 Then
      txtStartTime.Text = "7:15 AM"
      ElseIf txtStartTime.Text = 730 Then
      txtStartTime.Text = "7:30 AM"
      ElseIf txtStartTime.Text = 745 Then
      txtStartTime.Text = "7:45 AM"
      ElseIf txtStartTime.Text = 8 Then
      txtStartTime.Text = "8:00 AM"
      ElseIf txtStartTime.Text = 815 Then
      txtStartTime.Text = "8:15 AM"
      ElseIf txtStartTime.Text = 830 Then
      txtStartTime.Text = "8:30 AM"
      ElseIf txtStartTime.Text = 845 Then
      txtStartTime.Text = "8:45 AM"
      End If
      End Sub

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      Try something like this:

      Dim time As Integer
      If Integer.TryParse(txtStartTime.Text, time) Then

      Dim hour As Integer = 0, minutes As Integer = 0
      
      If 7 <= time AndAlso time <= 19 Then
          hour = time
          minutes = 0
      Else If 700 <= time AndAlso time <= 1900 Then
          hour = time \\ 100
          minutes = time Mod 100
      End If
      
      If hour <> 0 Then
          ' Round to the nearest 15 minutes:
          minutes = 15 \* Math.Round(minutes / 15)
          If minutes = 60 Then
              hour += 1
              minutes = 0
          End If
      
          Dim d As New Date(1, 1, 1, hour, minutes, 0)
          txtStartTime.Text = d.ToString("hh:mm tt")
      End If
      

      End If


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      V 1 Reply Last reply
      0
      • V VSLearner2013

        How can I shorten this lengthy code, which is not right as well? Though I want to write it using if condition / select case instead of using DateTimePicker. It starts from 7:00 AM, 7:15 AM, 7:30 AM, 7:45 AM, 8:00 AM ................So on until 7:00 PM.

        Private Sub txtStartTime_Leave(sender As Object, e As EventArgs) Handles txtStartTime.Leave
        If txtStartTime.Text = 7 Then
        txtStartTime.Text = "7:00 AM"
        ElseIf txtStartTime.Text = 715 Then
        txtStartTime.Text = "7:15 AM"
        ElseIf txtStartTime.Text = 730 Then
        txtStartTime.Text = "7:30 AM"
        ElseIf txtStartTime.Text = 745 Then
        txtStartTime.Text = "7:45 AM"
        ElseIf txtStartTime.Text = 8 Then
        txtStartTime.Text = "8:00 AM"
        ElseIf txtStartTime.Text = 815 Then
        txtStartTime.Text = "8:15 AM"
        ElseIf txtStartTime.Text = 830 Then
        txtStartTime.Text = "8:30 AM"
        ElseIf txtStartTime.Text = 845 Then
        txtStartTime.Text = "8:45 AM"
        End If
        End Sub

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        This is a Visual Basic issue, please use the correct forum for your questions: http://www.codeproject.com/Forums/1646/Visual-Basic.aspx[^].

        1 Reply Last reply
        0
        • Richard DeemingR Richard Deeming

          Try something like this:

          Dim time As Integer
          If Integer.TryParse(txtStartTime.Text, time) Then

          Dim hour As Integer = 0, minutes As Integer = 0
          
          If 7 <= time AndAlso time <= 19 Then
              hour = time
              minutes = 0
          Else If 700 <= time AndAlso time <= 1900 Then
              hour = time \\ 100
              minutes = time Mod 100
          End If
          
          If hour <> 0 Then
              ' Round to the nearest 15 minutes:
              minutes = 15 \* Math.Round(minutes / 15)
              If minutes = 60 Then
                  hour += 1
                  minutes = 0
              End If
          
              Dim d As New Date(1, 1, 1, hour, minutes, 0)
              txtStartTime.Text = d.ToString("hh:mm tt")
          End If
          

          End If


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          V Offline
          V Offline
          VSLearner2013
          wrote on last edited by
          #4

          Thank you so much for your kind help.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups