How to debug winforms designer

后端 未结 5 1183
余生分开走
余生分开走 2020-12-11 06:56

My question is not how to debug during design time. I actually want to debug through the events available in designer. I know that the form has load and other type of events

5条回答
  •  难免孤独
    2020-12-11 07:27

    Found a really old(almost 12 years) blog post through the following stack overflow question How does the Winforms Designer instantiate my form?

    Blog: https://blogs.msdn.microsoft.com/rprabhu/2004/12/12/how-does-the-windows-forms-designer-in-visual-studio-load-a-form/

    A quote from above blog that points me in the right direction

    Well, the designer is not really instantiating Form1 at all. It is creating an instance of the base class of Form1

    As per the above quote, follow the below steps and find the code that makes the designer slow

    1) Create a class that extends Form and add it to your winforms project

    public class DebuggableForm : Form
    {
        public DebuggableForm()
        {
            //Put your code in InitializeComponent method here.
            //Through line-by-line debugging you can find 
            //which line is making the designer slow
        }
    }
    

    2) Extend the above class instead of directly extending Form in Form1.cs. Here Form1 is the name of a form, check the appropriate form name in your project

    public class Form1: DebuggableForm
    {
    //Your actual form code
    }
    

    3) Place a breakpoint in the constructor of class created in step-1 (DebuggableForm)

    4) Attach your project to another instance of VS and open a project in new VS

    5) Open form1.cs in newly opened project and perform step-2 again.

    6) Open the designer or double click on Form1 in solution explorer. Your debugger in DebuggableForm will be hit

    Note: Form1.cs in above steps refer to a form in windows forms project. The name may vary in your project

提交回复
热议问题