i have 2 combobox, one is for the start time and the other is for the end time. my problem is on how to set the time interval.. for example if user select 6:00am as start time and 6:00 am as end time it must prompt that is in invalid.. i would like to allow my user to select only between time intervals with 30 mins, 45mins, 50 mins and 1 hour..how do i do that? please help me

I had a code for this...but I was having a text box will tells the interval...

Suppose the doctor starts his day at 9 am morning and ends at 6pm evening and the appointment interval is 30 mins then the result will be displayed in one combo box with the interval differnce....but the doctor timings should be stored in the database...

check for the below code....

it shows the data in a 24 hr format

      cboAppTime.Items.Clear()

            Dim starttime As DateTime = FormatDateTime("00:00")
            Dim endtime As DateTime = FormatDateTime("00:00")
             Dim duration As New TimeSpan

                starttime = frmApptSchedule.txtSunST.Text 'contains the complete start time of the doctor i.e. 09:00
                endtime = frmApptSchedule.txtSunET.Text 'contains the complete start time of the doctor i.e. 20:00

               duration = TimeValue(endtime.ToString) - TimeValue(starttime.ToString)
            '   Debug.Print("endtime: " + endtime)
            '     Debug.Print("starttime: " + starttime)
            '     Debug.Print("duration: " + duration.ToString)
            Dim Dur As Integer = duration.Hours
            Dim Dura As Integer = Integer.Parse(txtDuration.Text)
            Do Until starttime >= endtime
                cboAppTime.Items.Add(FormatDateTime(starttime.TimeOfDay.ToString, DateFormat.ShortTime))
                starttime = starttime.AddMinutes(Dura)
            Loop

Hope it helps u

Edited 4 Years Ago by Dani: use ~~~ vb code tags for vb

I would use a function to return a boolean stating if the two times are at the good intervals.
If you can send your selections as DateTime objects to the function, it will tell you if they are the correct intervals.
Here is an example of the function being called in a Console App, but it will work in any type of app:

Imports System
Module Module1
   Function IsGoodInterval(ByVal dtStart As DateTime, ByVal dtEnd As DateTime) As Boolean

      Dim lstGoodTimeSpans As New List(Of TimeSpan) From
       {New TimeSpan(1, 0, 0), New TimeSpan(0, 45, 0),
        New TimeSpan(0, 30, 0), New TimeSpan(0, 15, 0)}

      Return lstGoodTimeSpans.Contains(dtEnd - dtStart)
   End Function

   Sub Main()
      Dim dtStart As New DateTime(2012, 1, 1, 6, 1, 0)
      Dim dtEnd As New DateTime(2012, 1, 1, 6, 16, 0) '15 minutes from start'
      Dim dtEnd2 As New DateTime(2012, 1, 1, 6, 17, 0) '16 minutes from start'

      Console.WriteLine(IsGoodInterval(dtStart, dtEnd)) 'prints True (15-minute interval)'
      Console.WriteLine(IsGoodInterval(dtStart, dtEnd2)) 'Prints False (16-minute interval)'
   End Sub

End Module

Edited 4 Years Ago by thines01: clarity

Try :

    Dim diffrence As TimeSpan
    diffrence = DateTime.Parse(ComboStartTime.SelectedItem.ToString) - DateTime.Parse(ComboEndTime.SelectedItem.ToString)
    Select Case diffrence.TotalMinutes.ToString
        Case 30, 45, 50, 60
            MsgBox("true")
        Case Else
            MsgBox("false")
    End Select

i have 2 combobox, one is for the start time and the other is for the end time. my problem is on how to set the time interval.. for example if user select 6:00am as start time and 6:00 am as end time it must prompt that is in invalid..

Why you dont populate 2nd comboBox (for endtime) after 1st one is selected, I mean if user chooses 6am in 1st comboBox, populate 2nd comboBox with time after 6am (start with 6.30am). So there will no missunderstandings of any kind.

Edited 4 Years Ago by Mitja Bonca: gg

After re-reading the first post, I realized I was on the wrong track.

Here is a different function that when given a time (as a string) will return a List(Of String) containing the intervals of 30,45,50 and 60 from the given time:

Function GetGoodIntervals(ByVal strStart As String) As List(Of String)
   Dim lstRetVal As New List(Of String)

   Dim lstGoodTimeSpans As New List(Of TimeSpan) From
    {New TimeSpan(0, 30, 0), New TimeSpan(0, 45, 0),
     New TimeSpan(0, 50, 0), New TimeSpan(1, 0, 0)}

   Dim arr_strHrMin = strStart.Split(":")
   Dim tsNow = New TimeSpan(0, Integer.Parse(arr_strHrMin(0)), Integer.Parse(arr_strHrMin(1)), 0)

   For i As Integer = 0 To (lstGoodTimeSpans.Count - 1)
      Dim tsNext = tsNow + lstGoodTimeSpans(i)
      lstRetVal.Add(tsNext.Hours & ":" & tsNext.Minutes.ToString().PadLeft(2, "0"))
   Next

   Return lstRetVal
End Function

So, if called like this:

Dim strTimeChosen As String = "6:00"
Dim lstValidIntervals = GetGoodIntervals(strTimeChosen)
' Add that list to your combo-box of intervals'

Will produce a list of: 6:30, 6:45, 6:50, 7:00

Remember to add to the top of the module:

Imports System.Collections.Generic

Edited 4 Years Ago by thines01: clarity

this is my new code and this works ...when i select 6:00 am in start time the combobox for endtime will be populated with items 6:45, 6:50 and 7:00 am this is the function of this code

Private Sub ComboBox4_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox4.SelectedIndexChanged
    ComboBox5.Items.Clear()
    ComboBox5.Items.Add(Date.Parse(ComboBox4.Text).AddMinutes(45).ToString("hh:mm tt"))
    ComboBox5.Items.Add(Date.Parse(ComboBox4.Text).AddMinutes(50).ToString("hh:mm tt"))
    ComboBox5.Items.Add(Date.Parse(ComboBox4.Text).AddMinutes(60).ToString("hh:mm tt"))
End Sub

my problem now is the start time...combobox4 is the start time and combobox5 is the end time...

i'll say an example so that it will be easy for you to understand
what i wanted now is when the user selects in the end time 7:00 am he must no be able to select time that is lesser than 7:00 for example 6:50
hope you guys understand and can help me solved this...

I assume you mean the user can no longer select lesser values in ComboBox4.
Can you just remove all of the values lesser than previously chosen?

I can do a code for you, but 1st explain what means this:

i would like to allow my user to select only between time intervals with 30 mins, 45mins, 50 mins and 1 hour..how do i do that? please help me

Does it means for example if we take 6am hour, that it has to display:
6.30am
6.45am
6.50am

But this doesnt make any sence, does it? What else?

@Mitja that problem has already been solved by this code
Private Sub ComboBox4_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox4.SelectedIndexChanged
ComboBox5.Items.Clear()
ComboBox5.Items.Add(Date.Parse(ComboBox4.Text).AddMinutes(45).ToString("hh:mm tt"))
ComboBox5.Items.Add(Date.Parse(ComboBox4.Text).AddMinutes(50).ToString("hh:mm tt"))
ComboBox5.Items.Add(Date.Parse(ComboBox4.Text).AddMinutes(60).ToString("hh:mm tt"))
End Sub
my problem now is the start time...combobox4 is the start time and combobox5 is the end time...

i'll say an example so that it will be easy for you to understand
what i wanted now is when the user selects in the end time 7:00 am he must no be able to select time that is lesser than 7:00 for example 6:50
hope you guys understand and can help me solved this...

it is meaning less qutn dear !!if u choice start time first, the end time can be choosen after as your time interval may me it 6.20 to + 30,45 or 60.

hmm, so how to define which to select 1st? If you select starttime, then endtime has to adapt to starttime.
If you select 1st the end time, the starttime has to adapt to endtime.
And you have to know something more, the comboBox that will be selected 2nd will always have to adapt.
This is not a good idea, at least i am thinking this way.

then what should i do? should i disregard this code? how can i make a good time interval? help me

this is my code for the start time:

CbEnd.Items.Clear()
    CbEnd.Items.Add(Date.Parse(CbStart.Text).AddMinutes(45).ToString("hh:mm tt"))
    CbEnd.Items.Add(Date.Parse(CbStart.Text).AddMinutes(50).ToString("hh:mm tt"))
    CbEnd.Items.Add(Date.Parse(CbStart.Text).AddMinutes(60).ToString("hh:mm tt"))

this is my code for the end time:

CbStart.Items.Add(ComboBox5.SelectedItem.ToString)
    CbStart.Items.Add(Date.Parse(CbEnd.Text).AddMinutes(45).ToString("hh:mm tt"))
    CbStart.Items.Add(Date.Parse(CbEnd.Text).AddMinutes(50).ToString("hh:mm tt"))
    CbStart.Items.Add(Date.Parse(CbEnd.Text).AddMinutes(60).ToString("hh:mm tt"))

when i add
CbStart.Items.Clear()
in end time it clears the combobox and i can't save anything in my database coz it keeps clearing but when i don't add
CbStart.Items.Clear()
the items that had already been selected is still there which makes the user to select a wrong time...hope its clear and hope anyone will help me with this...sorry again for the trouble

Curious: Why would you add time intervals to the start-time based on the end-time?

Are you trying to just narrow-down the options the user can make?
Are multiple steps necessary?

Edited 4 Years Ago by thines01: clarity

I understand.
Think about what you really want the user to do.
What is the most important thing they need to do and what is the shortest way you can get them there.
Sometimes cool effects get in the way of the ACTUAL function.

The way I understand it: You want the user to pick a start-time then have the option of certain end-times. Is that correct?
Maybe it's as simple as disabling the start-time combo box once they have selected an end-time.

Edited 4 Years Ago by thines01: typo

i would like to allow my user to select only between time intervals with 30 mins, 45mins, 50 mins and 1 hour..

You've done that already, right?
They pick a start time, the you FILL the end time combo box with their options.

yes but the next problem is when i select the second start time...for example when they select in the end time 7:00 am they must not be able to select lesser than 7:00 in the second start time

Then you're going to need to just re-populate the subsequent start boxes when a previous one is chosen.

i did it and it works but i still have other problem...
this is my code for the start time:

    CbEnd.Items.Clear()

    CbEnd.Items.Add(Date.Parse(CbStart.Text).AddMinutes(45).ToString("hh:mm tt"))

    CbEnd.Items.Add(Date.Parse(CbStart.Text).AddMinutes(50).ToString("hh:mm tt"))

    CbEnd.Items.Add(Date.Parse(CbStart.Text).AddMinutes(60).ToString("hh:mm tt"))

this is my code for the end time:

    CbStart.Items.Add(ComboBox5.SelectedItem.ToString)

    CbStart.Items.Add(Date.Parse(CbEnd.Text).AddMinutes(45).ToString("hh:mm tt"))

    CbStart.Items.Add(Date.Parse(CbEnd.Text).AddMinutes(50).ToString("hh:mm tt"))

    CbStart.Items.Add(Date.Parse(CbEnd.Text).AddMinutes(60).ToString("hh:mm tt"))

when i add

CbStart.Items.Clear()

in end time it clears the combobox and i can't save anything in my database coz it keeps clearing but when i don't add

CbStart.Items.Clear()

the items that had already been selected is still there which makes the user to select a wrong time...hope its clear and hope anyone will help me with this...sorry again for the trouble

Sorry, but I still dont get one thing:

i would like to allow my user to select only between time intervals with 30 mins, 45mins, 50 mins and 1 hour..

What is that suppose to mean, 30 min, 45 min, 50 min and 1 hour?

Can you do some print screen so I can see what exactly do you have in mind?

yes it means 30 min, 45 min, 50 min and 1 hour...i'm doing a scheduling module for an enrollment system..

This article has been dead for over six months. Start a new discussion instead.