Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

Page Edit 2.0 Alpha 2 OUT NOW!

- Download Now -

Posted

The easiest approach is probably just to catch the rror if it doesn't exist:

 

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

 

:)

Please check the Knowledge Base before you post.

"Computers are useless. They can only give you answers." - Pablo Picasso

The Code Net

Posted
Hmm' date=' it froze the app for 5 seconds...[/quote']

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,

 

 

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

 

maybe this helps

 

Greetz

  • Leaders
Posted (edited)

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.

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]

Edited by snarfblam
[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...