Getox Posted September 17, 2005 Posted September 17, 2005 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? Quote Page Edit 2.0 Alpha 2 OUT NOW! - Download Now -
mark007 Posted September 17, 2005 Posted September 17, 2005 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 :) Quote Please check the Knowledge Base before you post. "Computers are useless. They can only give you answers." - Pablo Picasso The Code Net
Getox Posted September 17, 2005 Author Posted September 17, 2005 Hmm, it froze the app for 5 seconds... Quote Page Edit 2.0 Alpha 2 OUT NOW! - Download Now -
TripleB Posted September 17, 2005 Posted September 17, 2005 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 Quote
sjn78 Posted September 17, 2005 Posted September 17, 2005 I have found most Try-Catch's will freeze the app while in debug build. Once you build it in release, its fine. Quote
Leaders snarfblam Posted September 17, 2005 Leaders Posted September 17, 2005 (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 September 17, 2005 by snarfblam Quote [sIGPIC]e[/sIGPIC]
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.