Home All Groups Group Topic Archive Search About

use try catch to check a text box

Author
2 Oct 2006 3:45 PM
RipperT
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

Author
2 Oct 2006 3:50 PM
Marina Levit [MVP]
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.

Show quoteHide quote
"RipperT @comcast.net>" <rippert<nospam> wrote in message
news:u7$kHnj5GHA.1256@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
>
Author
2 Oct 2006 3:53 PM
Izzy
If textbox.Text.Trim.Length = 0 then Throw New Exception("foo")

Izzy

RipperT wrote:
Show quoteHide quote
> 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
Author
2 Oct 2006 4:04 PM
Patrice
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<nospam> a écrit dans le message de news:
u7$kHnj5GHA.1***@TK2MSFTNGP04.phx.gbl...
Show quoteHide quote
> 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
>