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

use try catch to check a text box

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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.