little help(not sure what this is)

Getox

Centurion
Joined
Jul 8, 2004
Messages
122
Ok well i didnt know what to call this, Sorry.

How would i check if a textbox exists and if it does then instert text into it, instead of having it try to instert text and crash?
 
The easiest approach is probably just to catch the rror if it doesn't exist:

Visual Basic:
try
    TextBox1.Text="Some text"
Catch e as Exception
     'textbox doesn't exist
End try

:)
 
Getox said:
Hmm, it froze the app for 5 seconds...
You can test if the textbox itself is != null that is if you know the variable exists but you don't knof if it has been initialised, something like this,


Code:
     Textbox mytextbox;
     if(mytextbox != null)
      {
            mytextbox.Text = "some text";
      }
      else
           do something else

maybe this helps

Greetz
 
I have found most Try-Catch's will freeze the app while in debug build. Once you build it in release, its fine.
 
The first exception thrown will always freeze the framework for a few seconds. A whole bunch of things are jitted and set up for exception handling at that point. This generally happens regardless of the build. The behavior you observe, sjn78, strikes me as unusual.

After the first exception all other exceptions generally execute very fast. They are, though, as the name indicates, for exceptional circumstances only, and NOT for program flow. Microsoft has little concern for the pause caused by the first exception because they should be few and far between. TripleB gave a good sample of the "proper way" to do it.

This is basically the same thing as TripleB's code, just in VB.
Visual Basic:
Sub SetText(Text As String)
    If Not (MyTextBox Is Nothing) Then 'If the textbox exists
        MyTextBox.Text = Text
    Else
        'You might want to respond to the fact that part of the
        'program is trying to assign text to a non-existant textbox.
    End If
End Sub
[Edit]Just a note: Using exceptions for program flow also becomes very annoying if you are debugging and the debugger is set to break on all exceptions. Using excpetions only as they are meant to be used makes debugging very easy.[/Edit]
 
Last edited:
Back
Top