469,647 Members | 1,730 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,647 developers. It's quick & easy.

use try catch to check a text box

Hello,

I am using VB2005. I would like to check a textbox to see if it is blank
using a Try Catch block. I assumed an easy way to do that would be to
perform some action on the string that can't be done if the text box is
blank, then catch it. But I can't find an action to perform that errors if
the text box is blank. I don't want to use If Then Else. Can anyone help?

Rip
Oct 2 '06 #1
3 4571
That sounds like overkill for just checking if the textbox is blank.
Throwing and catching exceptions is somewhat expensive, and not something
that should be done unless there is an actual error occurring. A textbox
being empty is not an unexpected error - it is something you can easily
catch.

Something like this should work:

If String.IsNullOrEmpty(TextBox1.Text) Then
' textbox empty
Else
' textbox not empty
End If.

"RipperT @comcast.net>" <rippert<nospamwrote in message
news:u7**************@TK2MSFTNGP04.phx.gbl...
Hello,

I am using VB2005. I would like to check a textbox to see if it is blank
using a Try Catch block. I assumed an easy way to do that would be to
perform some action on the string that can't be done if the text box is
blank, then catch it. But I can't find an action to perform that errors if
the text box is blank. I don't want to use If Then Else. Can anyone help?

Rip

Oct 2 '06 #2
If textbox.Text.Trim.Length = 0 then Throw New Exception("foo")

Izzy

RipperT wrote:
Hello,

I am using VB2005. I would like to check a textbox to see if it is blank
using a Try Catch block. I assumed an easy way to do that would be to
perform some action on the string that can't be done if the text box is
blank, then catch it. But I can't find an action to perform that errors if
the text box is blank. I don't want to use If Then Else. Can anyone help?

Rip
Oct 2 '06 #3
Something like :

chk.Checked=(txt.Text="") or similar depending on what you mean by "blank".

Else elaborate a bit on the reason why you absolutely need a try/catch block
(which sounds for now like a cumbersome way to do something simple).

--
Patrice

"RipperT @comcast.net>" <rippert<nospama écrit dans le message de news:
u7**************@TK2MSFTNGP04.phx.gbl...
Hello,

I am using VB2005. I would like to check a textbox to see if it is blank
using a Try Catch block. I assumed an easy way to do that would be to
perform some action on the string that can't be done if the text box is
blank, then catch it. But I can't find an action to perform that errors if
the text box is blank. I don't want to use If Then Else. Can anyone help?

Rip

Oct 2 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by kaeli | last post: by
23 posts views Thread by VB Programmer | last post: by
4 posts views Thread by ILCSP | last post: by
9 posts views Thread by John Devlon | last post: by
5 posts views Thread by vovan | last post: by
7 posts views Thread by Lloyd Sheen | last post: by
9 posts views Thread by SMH | last post: by
3 posts views Thread by =?Utf-8?B?SmVycnk=?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.