Solved

test if TextBox exists before writing to it?

Posted on 2011-09-07
4
244 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
4 Comments
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 167 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
TextBox1 != null

Open in new window

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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 …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now