Solved

test if TextBox exists before writing to it?

Posted on 2011-09-07
4
248 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 167 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 166 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 167 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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!
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

726 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