Solved

test if TextBox exists before writing to it?

Posted on 2011-09-07
4
249 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

Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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 …
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

691 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