?
Solved

test if TextBox exists before writing to it?

Posted on 2011-09-07
4
Medium Priority
?
250 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

DFW AZURE MEETUP TONIGHT FRI 6PM

We will be discussing what Azure Stack is, how does it fit into the suit of offerings that Azure has currently, and where can it fit into your organizations technology stack. We will also be discussing limitations of the platform while covering various applicable scenarios.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

764 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