use try catch to check a text box

  • Thread starter Thread starter RipperT
  • Start date Start date
R

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
 
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.
 
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).
 
Back
Top