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

Flow control

P: n/a
Hello:

There are a series of textboxes (x.text, y.text, z.text etc.) in which user input is expected.
As:
dtmX=CDate(x.text)
dtmY=CDate(y.text)
dtmZ=CDate(z.text)

where x, y, and z.text must be in hh:mm format.

1. What is the best method for capturing hh:mm from a user? Ideally, I would like to use a control similar to the time input box in clock.exe, with the two up/down arrows that also uses the up/down keyboard keys, but without the seconds. What I don't want is the drop down calendar of the date timepicker with the single drop down arrow. How might that be accomplished?

2. I want the program to check for input errors (InvalidCastException ex: input of 24 hours or more in the hours part) and return to the textbox for user correction if an exception is caught, but not to keep executing the program until that correction has been accomplished, and then to test the box again, and then continue execution if the input is acceptable.

Try
dtmX = CDate(x.Text)
Catch ic As InvalidCastException
InputError()
Finally
txtX.Focus()
End Try

3. Using a Try/Catch/Finally the program will catch an input error in a textbox, but the program continues to execute beyond the subroutine, rather than returning to the textbox at position 1 and waiting for corrected input.

Should I use a do while or if loop or something similar to maintain flow control if I want the user to return to the box with the error for a user correction. Do I need a loop for each text box?

4. The Focus(), places the carat after the error instead at the beginning of the box (position 1). How do I get it to return to position 1, and return the box to the default condition prior to input?

5. Do I need to do a Try/Catch/Finally for each (of many) textboxes, or is there some simple way to iterate through them and stop only for an exception?
Thank You,
Dennis D.
--
http://www.dennisys.com/
Nov 21 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Dennis,
1. What is the best method for capturing hh:mm from a user?
You can use the DateTimePicker to enter the time values. To use a DateTime
picker to enter your times set the following properties:

Format = Custom
CustomFormat = "HH:mm" (for 24 hour:min)
ShowUpDown = True

2. I want the program to check for input errors
3...
4... I normally use the Validating event to check for valid input. The Validating
event is able to keep the cursor on the control in error. For a start on
using the Validating event see:

http://msdn.microsoft.com/library/de...ndowsForms.asp

5. Do I need to do a Try/Catch/Finally for each (of many) textboxes,
or is there some simple way to iterate through them and stop
only for an exception?


You can have a single Validating event handler handle many TextBoxes...

Hope this helps
Jay

"Dennis D." <te**@dennisys.com> wrote in message
news:OS**************@tk2msftngp13.phx.gbl...
Hello:

There are a series of textboxes (x.text, y.text, z.text etc.) in which user
input is expected.
As:
dtmX=CDate(x.text)
dtmY=CDate(y.text)
dtmZ=CDate(z.text)

where x, y, and z.text must be in hh:mm format.

1. What is the best method for capturing hh:mm from a user? Ideally, I would
like to use a control similar to the time input box in clock.exe, with the
two up/down arrows that also uses the up/down keyboard keys, but without the
seconds. What I don't want is the drop down calendar of the date timepicker
with the single drop down arrow. How might that be accomplished?

2. I want the program to check for input errors (InvalidCastException ex:
input of 24 hours or more in the hours part) and return to the textbox for
user correction if an exception is caught, but not to keep executing the
program until that correction has been accomplished, and then to test the
box again, and then continue execution if the input is acceptable.

Try
dtmX = CDate(x.Text)
Catch ic As InvalidCastException
InputError()
Finally
txtX.Focus()
End Try

3. Using a Try/Catch/Finally the program will catch an input error in a
textbox, but the program continues to execute beyond the subroutine, rather
than returning to the textbox at position 1 and waiting for corrected input.

Should I use a do while or if loop or something similar to maintain flow
control if I want the user to return to the box with the error for a user
correction. Do I need a loop for each text box?

4. The Focus(), places the carat after the error instead at the beginning of
the box (position 1). How do I get it to return to position 1, and return
the box to the default condition prior to input?

5. Do I need to do a Try/Catch/Finally for each (of many) textboxes, or is
there some simple way to iterate through them and stop only for an
exception?
Thank You,
Dennis D.
--
http://www.dennisys.com/
Nov 21 '05 #2

P: n/a
Dennis,

All your answers on your questions are in it, although I did not take the solution you was proposing

I made it for your solution so you should try it. The sequence from the "if" is special done in that way. The Isdate acts as a mini trycatch block so the first time it is slow.

\\\
If x.Text.Length <> 5 _
OrElse x.Text.Substring(2, 1) <> ":" _
OrElse Not IsDate(x.Text) Then
MessageBox.Show("the date is wrong")
x.Focus()
x.SelectionLength = 0
x.SelectionStart = 0
Else
Dim dtmX As DateTime = CDate(x.Text)
End If
///

I hope this helps?

Cor
Nov 21 '05 #3

P: n/a

"Dennis D." <te**@dennisys.com> wrote

There are a series of textboxes (x.text, y.text, z.text etc.) in which user input is expected.
As:
dtmX=CDate(x.text)
dtmY=CDate(y.text)
dtmZ=CDate(z.text)

where x, y, and z.text must be in hh:mm format.
1. What is the best method for capturing hh:mm from a user?
Create a class that inherits from Textbox, and give it the functionality you need,
then use those in place of the original textboxes.
2. I want the program to check for input errors
I'd look into the validating event to handle validation....

3. Using a Try/Catch/Finally the program will catch an input error in a textbox,
Don't use a Try/Catch block for something you can easily test for. Try/Catch
is expensive, If/Then is not.
4. The Focus(), places the carat after the error instead at the beginning of the box (position 1). How do I get it to return to position 1, and return the box to the default condition prior to input?

Maybe you could just hide the caret and use judical use of highlighting
to indicate which part will be effected.
5. Do I need to do a Try/Catch/Finally for each (of many) textboxes,


Once you have that inherited control working, you can use it in
multiple places.

LFS

Nov 21 '05 #4

P: n/a
How "expensive is Try/Catch". I don't use it when I can use the If/then but
there are a lot of cases where I want to catch various types of errors.

"Larry Serflaten" wrote:

"Dennis D." <te**@dennisys.com> wrote

There are a series of textboxes (x.text, y.text, z.text etc.) in which user input is expected.
As:
dtmX=CDate(x.text)
dtmY=CDate(y.text)
dtmZ=CDate(z.text)

where x, y, and z.text must be in hh:mm format.
1. What is the best method for capturing hh:mm from a user?


Create a class that inherits from Textbox, and give it the functionality you need,
then use those in place of the original textboxes.
2. I want the program to check for input errors


I'd look into the validating event to handle validation....

3. Using a Try/Catch/Finally the program will catch an input error in a textbox,


Don't use a Try/Catch block for something you can easily test for. Try/Catch
is expensive, If/Then is not.
4. The Focus(), places the carat after the error instead at the beginning of the box (position 1). How do I get it to return

to position 1, and return the box to the default condition prior to input?

Maybe you could just hide the caret and use judical use of highlighting
to indicate which part will be effected.
5. Do I need to do a Try/Catch/Finally for each (of many) textboxes,


Once you have that inherited control working, you can use it in
multiple places.

LFS

Nov 21 '05 #5

P: n/a


"Larry Serflaten" wrote:

"Dennis D." <te**@dennisys.com> wrote

There are a series of textboxes (x.text, y.text, z.text etc.) in which user input is expected.
As:
dtmX=CDate(x.text)
dtmY=CDate(y.text)
dtmZ=CDate(z.text)

where x, y, and z.text must be in hh:mm format.
1. What is the best method for capturing hh:mm from a user?


Create a class that inherits from Textbox, and give it the functionality you need,
then use those in place of the original textboxes.
2. I want the program to check for input errors


I'd look into the validating event to handle validation....

3. Using a Try/Catch/Finally the program will catch an input error in a textbox,


Don't use a Try/Catch block for something you can easily test for. Try/Catch
is expensive, If/Then is not.
4. The Focus(), places the carat after the error instead at the beginning of the box (position 1). How do I get it to return

to position 1, and return the box to the default condition prior to input?

Maybe you could just hide the caret and use judical use of highlighting
to indicate which part will be effected.
5. Do I need to do a Try/Catch/Finally for each (of many) textboxes,


Once you have that inherited control working, you can use it in
multiple places.

LFS

Nov 21 '05 #6

P: n/a

"Dennis" <De****@discussions.microsoft.com> wrote
How "expensive is Try/Catch". I don't use it when I can use the If/then but
there are a lot of cases where I want to catch various types of errors.


That sounds OK, but you indicated that you would be using it to respond to
bad user input. I'd consider that routine type use that should avoid throwing and
catching exceptions. YMMV. Perhaps this will shed some light on the subject:

http://msdn.microsoft.com/library/de...Exceptions.asp

HTH
LFS
Nov 21 '05 #7

P: n/a
Larry,

Although I as well try to avoid the try and catch is there AFAIK an effect.
Only the first exception is slow.

By the way, that page you show is rare. The text is almost right however the
showed samples are wrong they don't show the finally.

And as I wrote in my sample answer, probably is the "IsDate" a mini Try
Catch block so sometimes there is no advantage at all avoiding that Try
Catch.

However I keep it there where it can by avoiding the Try and Catch and am
using the If because that gives direct control over the conditions I want to
test.

Just a little perception of mine in addition to your message.

Cor

"Larry Serflaten" <se*******@usinternet.com>


"Dennis" <De****@discussions.microsoft.com> wrote
How "expensive is Try/Catch". I don't use it when I can use the If/then
but
there are a lot of cases where I want to catch various types of errors.


That sounds OK, but you indicated that you would be using it to respond to
bad user input. I'd consider that routine type use that should avoid
throwing and
catching exceptions. YMMV. Perhaps this will shed some light on the
subject:

http://msdn.microsoft.com/library/de...Exceptions.asp

HTH
LFS

Nov 21 '05 #8

P: n/a
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message news:OV**************@TK2MSFTNGP11.phx.gbl...
Dennis,
1. What is the best method for capturing hh:mm from a user?


You can use the DateTimePicker to enter the time values. To use a DateTime
picker to enter your times set the following properties:

Format = Custom
CustomFormat = "HH:mm" (for 24 hour:min)
ShowUpDown = True


I am trying to capture a timespan, such as 00:15 for a 15 minute timespan. The Date TimePicker unfortunately does not allow 00 in the hours position, or it would be perfect.

In many applications, you can set an input filter: Phone: (nnn) nnn - nnnn where the ( ) and the - are not writable.
The input to the user looks something like (xxx) xxx - xxxx

In this case I want to use a textbox to get a string nn : nn where n is a numeric characters and the : colon is not writable.

To supply an hours format, I would like the first character to increment from 0 to 2, and the second character to increment from 0 to 9, but for the combination not to exceed 24, using the arrow keys on the keyboard, with the option to simply type in the numbers. To me this implies character level manipulation, and some keyboard translations, all of which are fairly advanced for a vb novice. I'm still learning about character and string manipulation in Visual Basic.

Is it possible to set an input filter in a textbox in visual basic, or do I need to use two textboxes to capture nn : nn?


Thank You,
Dennis D.
--
http://www.dennisys.com/
Nov 21 '05 #9

P: n/a
Dennis,
Please disable HTML formatting in your News Reader!
I am trying to capture a timespan, such as 00:15 for a 15 minute timespan.
The Date TimePicker unfortunately does not allow 00 in the hours position,
When you use CustomFormat = "HH:mm" the DateTimePicker allows 00 in the
hours! At least in VS.NET 2003 (.NET 1.1) it does!

Note the upper case HH & the lower case mm!

To get or set the "Time" value I normally use something like:

Public Property Time() As TimeSpan
Get
Return Me.DateTimePicker1.Value.TimeOfDay
End Get
Set(ByVal value As TimeSpan)
Me.DateTimePicker1.Value = Me.DateTimePicker1.MinDate.Add(value)
End Set
End Property

Optionally you could try:
Return
Me.DateTimePicker1.Value.Subtract(Me.DateTimePicke r1.MinDate)

For Time.Get to allow entry of "Days" also...
Are you using VS.NET 2002 or VS.NET 2005? If you are using VS.NET 2002, then
I strongly recommend you upgrade. If you are using VS.NET 2005, then I would
strongly recommend you report the bug.
In many applications, you can set an input filter:
Phone: (nnn) nnn - nnnn where the ( ) and the - are not writable.
The input to the user looks something like (xxx) xxx - xxxx I understand you will need to wait for VS.NET 2005 for the masked edit
control. VS.NET 2005 (aka Whidbey, due out later in 2005) is currently in
beta, for details see:
http://lab.msdn.microsoft.com/vs2005/

Hope this helps
Jay
"Dennis D." <te**@dennisys.com> wrote in message
news:Ox**************@TK2MSFTNGP09.phx.gbl...
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:OV**************@TK2MSFTNGP11.phx.gbl... Dennis,
1. What is the best method for capturing hh:mm from a user?


You can use the DateTimePicker to enter the time values. To use a DateTime
picker to enter your times set the following properties:

Format = Custom
CustomFormat = "HH:mm" (for 24 hour:min)
ShowUpDown = True


I am trying to capture a timespan, such as 00:15 for a 15 minute timespan.
The Date TimePicker unfortunately does not allow 00 in the hours position,
or it would be perfect.

In many applications, you can set an input filter: Phone: (nnn) nnn - nnnn
where the ( ) and the - are not writable.
The input to the user looks something like (xxx) xxx - xxxx

In this case I want to use a textbox to get a string nn : nn where n is a
numeric characters and the : colon is not writable.

To supply an hours format, I would like the first character to increment
from 0 to 2, and the second character to increment from 0 to 9, but for the
combination not to exceed 24, using the arrow keys on the keyboard, with the
option to simply type in the numbers. To me this implies character level
manipulation, and some keyboard translations, all of which are fairly
advanced for a vb novice. I'm still learning about character and string
manipulation in Visual Basic.

Is it possible to set an input filter in a textbox in visual basic, or do I
need to use two textboxes to capture nn : nn?
Thank You,
Dennis D.
--
http://www.dennisys.com/
Nov 21 '05 #10

P: n/a
Please disable HTML formatting in your News Reader!
- Done
When you use CustomFormat = "HH:mm" the DateTimePicker allows 00 in the
hours! At least in VS.NET 2003 (.NET 1.1) it does! I noticed the difference between HH and hh, forgot the meaning, and ignored
it.
I learned lazy is not good practice.
- Are you using VS.NET 2002 or VS.NET 2005? Using 2002 that came with VB.NET. I'll take a look at the beta.
- the masked edit control With the Maxwell Smart voice, "Ah, yes, the Masked Edit Control!"
Can almost hear him saying it can't you? How does the brain do that?
Now there's an intelligence tool for the CIA to drool over. Real time voice
translation.
-
But it begs the question: Shouldn't VB.Net+VS.Net be able to create the
masked edit control solution?
Same with DateTimePicker. The source for the control is in VS.Net or not?
- A few replies down Larry said: "Create a class that inherits from Textbox,
and give it the functionality you need,

then use those in place of the original textboxes."
-
There's a programming challenge for me. I'm reading this about stuff, but
there is nothing like actually doing the coding to help the learning
process. Hence, the project.

Thanks Jay B. Harlow,
and thanks Larry Serflaten,

I'm on the case!

Dennis D.
--
http://www.dennisys.com/
Nov 21 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.