Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

test if TextBox exists before writing to it?

Posted on 2011-09-07
4
Medium Priority
?
252 Views
Last Modified: 2012-05-12
I have a bit of code that writes to a log file. If there's an error it writes the message to TextBox1:
      try
      {
        myLog.Write(str);
      }
      catch (IOException exc)
      {
        AppendTextBox1(exc.Message + Environment.NewLine);
        return;
      }

Open in new window

Now my problem is, I have another bit of code that's called when the program ends:
    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
      AppendLog("PROGRAM CLOSE" + Environment.NewLine);
    }

Open in new window

Does TextBox1 still exist at this point? What happens if there's an error writing to the log file at this point. Does the program crash because TextBox1 might not exist anymore when AppendLog tries to write the error message to TextBox1?
0
Comment
Question by:deleyd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 668 total points
ID: 36499955
Why would you want to write to a TextBox if your Form was in the middle of closing anyway? Why not create a function to write to the log, a function to write to the TextBox (which calls the aforementioned write-to-log function), and on form close, only call the write-to-log function? Then you only write to the TextBox whenever you call the function to write to the TextBox--only the write-to-log fucntion is called on form close, so no need to worry about the TextBox.
0
 
LVL 1

Assisted Solution

by:amr-it
amr-it earned 664 total points
ID: 36501074
I'd recommend you have a look at Log4Net.
http://www.codeproject.com/KB/trace/log4net_intro.aspx

If you want to do it yourself, one way to go would be to directly write to a logfile and have the textbox to be updated with the contents of the file, rather than doing it the other way around.
0
 
LVL 6

Assisted Solution

by:Chuck Yetter
Chuck Yetter earned 668 total points
ID: 36502183
You could test the TextBox to see if it still exists, but your user will never see the text in the text box:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
      if (TextBox1 != null)
      {
           AppendLog("PROGRAM CLOSE" + Environment.NewLine);
      }
}
0
 

Author Comment

by:deleyd
ID: 36593895
TextBox1 != null

Open in new window

didn't work in certain cases, so I placed it in a try/catch instead.
0

Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question