By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,246 Members | 1,480 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,246 IT Pros & Developers. It's quick & easy.

Time Check Function

geo039
P: 47
I tried to write a simple application that takes user input by text and time selected by date time picker. It displays the appt description in one
list box and the time in another list box. I wrote a simple function
that checks the times for duplicates. I wanted to check for a duplicate
before it writes to the listbox by returning a boolean (true/false)
value. I only need to check hours and minutes not date or seconds. The following is what I have, it's not recognizing the duplicates, am I confused in how the function reads? Any suggestions?

Expand|Select|Wrap|Line Numbers
  1. If (txtAppointment.Text = "") Then
  2.             'invalid input, alert user
  3.             MessageBox.Show(Text:="You Must Enter an Appointment Description", _
  4.                 caption:="Input error - Add Appointment")
  5.             'select the invalid input
  6.             txtAppointment.SelectAll()
  7.             txtAppointment.Focus()
  8.  
  9.         Else
  10.  
  11.             Dim Time As Boolean
  12.  
  13.             Time = TimeTaken(Me.dtmTime.Value.ToShortDateString)
  14.  
  15.             If Time Then
  16.                 MessageBox.Show("You already have an appointment at this time")
  17.             Else
  18.  
  19.             'display appointment in Listbox
  20.                 lstApptResults.Items.Add(txtAppointment.Text)
  21.                 txtAppointment.Clear() 'clear appointment from TextBox
  22.                 txtAppointment.Focus() 'transfer focus to TextBox
  23.  
  24.             'display appointment time in Listbox
  25.                 lstTimeResults.Items.Add(Me.dtmTime.Value.ToShortTimeString)
  26.  
  27.             End If
  28.         End If
  29.     End Sub 'btnAddAppt_Click
  30.     'function to check if an appointment time already exists in listbox
  31.     Public Function TimeTaken(ByVal ApptTime As DateTime) As Boolean
  32.  
  33.         Dim DuplicateTime As Boolean = False
  34.  
  35.         'loop that checks listed times for duplicates
  36.         For Each strItem As String In lstTimeResults.Items
  37.             If strItem = ApptTime Then
  38.                 DuplicateTime = True
  39.  
  40.                 Exit For
  41.             End If
  42.         Next
  43.  
  44.         Return DuplicateTime
  45.     End Function 'TimeTaken
Nov 14 '06 #1
Share this Question
Share on Google+
4 Replies


Lok
P: 11
Lok
Expand|Select|Wrap|Line Numbers
  1. If (txtAppointment.Text = "") Then
  2.             'invalid input, alert user
  3.             MessageBox.Show(Text:="You Must Enter an Appointment Description", _
  4.                 caption:="Input error - Add Appointment")
  5.             'select the invalid input
  6.             txtAppointment.SelectAll()
  7.             txtAppointment.Focus()
  8.  
  9.         Else
  10.  
  11.             Dim Time As Boolean
  12.  
  13.             Time = TimeTaken(Me.dtmTime.Value.ToShortDateString)
  14.  
  15.             If Time Then
  16.                 MessageBox.Show("You already have an appointment at this time")
  17.             Else
  18.  
  19.             'display appointment in Listbox
  20.                 lstApptResults.Items.Add(txtAppointment.Text)
  21.                 txtAppointment.Clear() 'clear appointment from TextBox
  22.                 txtAppointment.Focus() 'transfer focus to TextBox
  23.  
  24.             'display appointment time in Listbox
  25.                 lstTimeResults.Items.Add(Me.dtmTime.Value.ToShortTimeString)
  26.  
  27.             End If
  28.         End If
  29.     End Sub 'btnAddAppt_Click
  30.     'function to check if an appointment time already exists in listbox
  31.     Public Function TimeTaken(ByVal ApptTime As DateTime) As Boolean
  32.  
  33.         Dim DuplicateTime As Boolean = False
  34.  
  35.         'loop that checks listed times for duplicates
  36.         For Each strItem As String In lstTimeResults.Items
  37.             If strItem = ApptTime Then
  38.                 DuplicateTime = True
  39.  
  40.                 Exit For
  41.             End If
  42.         Next
  43.  
  44.         Return DuplicateTime
  45.     End Function 'TimeTaken

Try to investigate whether the parameter value that you sent to the TimeTaken function is what you expected (eg. put a MsgBox or something to inspect the value).

I don't have chance to try it, but so far from what I'm read your code, that your listbox items are string. Your time-input is also a string (gained from .ToShortTimeString). But why your TimeTaken parameter type is a DateTime ?

When the program run, the original time entered by the user will be convert to a string using .ToShortTimeString, the when passed to the TimeTaken function, it will converted back to DateTime. Then when compared to the listbox items, it will converted again to a string (using DateTime default format).

Don't know, maybe it's the root of the problem. Please check.

regards,
Lok
Nov 15 '06 #2

geo039
P: 47
I think the conversion is where I am confused. The user writes their appt in a text box and selects a time from a datetimepicker. The input from text goes to one list box and the time to another. I want to check against the times only but somewhere along the lines it's not checking the time as a time. The functionality works the function just isn't picking up the time as a format needing to check for duplicates for some reason.
Nov 15 '06 #3

Lok
P: 11
Lok
Ok, then make it in the same format then, avoid any conversion. So you must be firm, decide one, keep it as a string, or as a DateTime.

As a string, after user pick the DateTime using DatePicker, convert it to string. Put inside listbox (also as a string), then when you want to compare, compare it also string to string.

As a DateTime, after user pick the DateTime using DatePicker, convert it to string (why? it's the easiest way to populate listbox), then when you want to compare, get the item from the listbox, convert it back to DateTime, then compare it DateTime to DateTime.

If you want to put more effort, create a custom ListBoxItem class, overriding its ToString() method (a string value with specific date/time format that you required). So you can still keep the original DateTime value to be use for comparison.

Regards,

Lok
Nov 16 '06 #4

geo039
P: 47
Got the simple problem to work. Sometimes after all is said and done the solution ends up being something so simple. After staring at something for hours it all just starts to go to mush.

Thanks Again
Nov 16 '06 #5

Post your reply

Sign in to post your reply or Sign up for a free account.