skip to Main Content

I’m developing a Windows forms app using C# in Visual Studio 2022 Version 17.11.5

When I try and edit a form using the GUI Visual Studio sends debug and trace messages to message boxes like that here.

Trace listener message:

Trace listener message

The output window shows the message –

trce: Notifying 1 'UI/ShowMessage' handlers

Getting one might be okay but it’s throwing dozens of these messages before it opens the form for editing.

Any help would be much appreciated.

I have tried porting code to new solution but it still has the problem.

To reproduce the problem

Create custom control using Windows Forms Control Library for .NET

Add control load event

private void UserControl1_Load(object sender, EventArgs e)
{
    Trace.WriteLine("Hello world!");
}

In same solution add a new Windows Forms App and using GUI add the custom control. Doesn’t need any code other than that produced by Visual Studio.

When you open the form for editing it displays the message from the Trace Listener.

Hello World message:

Hello World message

2

Answers


  1. This appears to be either how Windows Forms works now or an issue that they won’t fix – see this Github issue.

    You can work around it by removing the default trace listener and adding your own. So, in the Load event for your UserControl:

    Trace.Listeners.RemoveAt(0);
    Trace.Listeners.Add(new DefaultTraceListener { LogFileName = @"c:temptrace.log" });
    Trace.WriteLine($"{ DateTime.Now.ToString() }");
    

    That will just dump any Trace and Debug output into the file specified, which you can refer to or ignore as needed.

    Of course this would probably be better done in the initialisation stage of the whole application rather than everywhere you might want to call Trace or Debug.

    Login or Signup to reply.
  2. The problem is that Trace.WriteLine is used in the Load event of the custom control. When you add and open the control through the designer in a Windows Forms application, the designer will also trigger this Load event, causing the debug message pop-up window. After my test, if you want to avoid this phenomenon, you can add a DesignMode check in the UserControl1_Load event:

    private void UserControl1_Load(object sender, EventArgs e)
    {
       if (!this.DesignMode)
       {
           Trace.WriteLine("Hello world!");
       }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search