?
Solved

Validating Combo Boxes

Posted on 2005-04-17
5
Medium Priority
?
163 Views
Last Modified: 2010-04-23
Hi any help would be much appreciated as this problem has got me stuck, although I am sure the answer may well be very simple.

I am writing an application, that has open and closing times of a retail outlet. As the times may vary widly I have opted for 4 combo boxes

OpenHour 00-24
OpenMinute 00-55 (in five minute intervals)
Close hour 00-24
CloseMinute 00-55 (in five minute intervals)

All I want to do is validate that the closing time (hour and minutes combined) is greater than the opening times (again hour and minutes).  I have set up a handler on each combo box to validate the opening time is still less than the closing time each time a value has been changed.

If the result of the check is "false" i.e. the closing time is less than the opening time, can I reset the last changed value back to it's previous value, after displaying an error message.

Thanks for any help

Steve
0
Comment
Question by:swtiley
  • 3
  • 2
5 Comments
 
LVL 14

Expert Comment

by:amyhxu
ID: 13803426
Call the following sub in each combobox's SelectedIndexChanged event:
suppose you call these combobox cb1, cb2, cb3, cb4 in the order you listed in the question

Dim passValidate As Boolean

Private Sub cbValidate()

    Dim d1 As Date = TimeValue(cb1.Text.Trim & ":" & cb2.Text.Trim)
    Dim d2 As Date = TimeValue(cb3.Text.Trim & ":" & cb4.Text.Trim)
    If d1 >= d2 Then
        MsgBox("The closing time should be later than the opening time!")
        passValidate = False
    Else
        passValidate = True
    End If

End Sub

0
 
LVL 14

Expert Comment

by:amyhxu
ID: 13803471
Here's the code for validating, and if the result is false, reset the last changed value back to its previous value:

Dim passValidate As Boolean

Private Sub cbValidate()

    Dim d1 As Date = TimeValue(cb1.Text.Trim & ":" & cb2.Text.Trim)
    Dim d2 As Date = TimeValue(cb3.Text.Trim & ":" & cb4.Text.Trim)
    If d1 >= d2 Then
        MsgBox("The closing time should be later than the opening time!")
        passValidate = False
    Else
        passValidate = True
    End If

End Sub


    Dim preVal As Integer
    Dim resetAfterValidate As Boolean

    Private Sub cb1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cb1.SelectedIndexChanged
       
        If Not resetAfterValidate Then
            cbValidate()
            If Not passValidate Then
                resetAfterValidate = true
                cb1.Text = preVal
                resetAfterValidate = False
            End If
        End If
   
    End Sub

    'Same code for other comboboxes
0
 
LVL 1

Author Comment

by:swtiley
ID: 13804289
Hi amyhxu

Firstly thanks for your response.

I can follow the code above, and the key part for me is the

cb1.Text = preVal

as that is exactly what I need, however the preVal in the example above does not get set anywhere, and therefore I still can not reset the combobox to its previous value.

Any ideas ?

Thanks
 Steve
0
 
LVL 14

Accepted Solution

by:
amyhxu earned 2000 total points
ID: 13806395
Sorry, my mistake.

    Private Sub cb1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cb1.SelectedIndexChanged
       
        If Not resetAfterValidate Then
            cbValidate()
            If Not passValidate Then
                resetAfterValidate = true
                cb1.Text = preVal
                resetAfterValidate = False
            End If
            preVal = cb1.Text
        End If
     
    End Sub

    'Same code for other comboboxes

0
 
LVL 1

Author Comment

by:swtiley
ID: 13807232
Many thanks amyhxu.....
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Loops Section Overview

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question