偵錯準備:Windows Forms 應用程式Debugging Preparation: Windows Forms Applications

Windows Form 專案範本會建立 Windows Forms 應用程式。The Windows Forms project template creates a Windows Forms application. Visual StudioVisual Studio 中可以直接偵錯這種類型的應用程式。Debugging this type of application in Visual StudioVisual Studio is straightforward. 如需詳細資訊,請參閱建立 Windows 應用程式專案For more information, see Creating a Windows Application Project.

當您以專案範本建立 Windows Form 專案時,Visual StudioVisual Studio 會自動建立偵錯和發行組態所需要的設定。When you create a Windows Forms project with the project template, Visual StudioVisual Studio automatically creates required settings for the Debug and Release configurations. 若有需要,您可以變更這些設定。If necessary, you can change these settings. 這些設定可以在 [<專案名稱> 屬性頁] 對話方塊 (在 Visual Basic 中為 [我的專案]) 中進行變更。These settings can be changed in the <project name> Property Pages dialog box (My Project in Visual Basic).

如需詳細資訊,請參閱建議的屬性設定For more information, see Recommended Property Settings.

下表顯示一個額外的建議屬性設定。The following table displays one additional recommended property setting.

偵錯索引標籤的組態屬性Configuration Properties in Debug tab

屬性名稱Property Name 設定Setting
起始動作Start Action - 通常會設定為 [起始專案]。- Set to Start project, most of the time. 如果您開始偵錯 (通常是偵錯 DLL) 時想要啟動另外一個可執行檔,請設定為 [啟動外部程式]。Set to Start external program if you want to start another executable when you start debugging (usually for debugging DLLs).

您可以從 Visual StudioVisual Studio 內部偵錯 Windows Forms 應用程式,或附加至正在執行的應用程式進行偵錯。You can debug Windows Forms applications from inside Visual StudioVisual Studio, or by attaching to an already running application. 如需附加的詳細資訊,請參閱附加至執行中的進程。For more information about attaching, see Attach to Running Processes.

若要偵錯 C#、F# 或 Visual Basic Windows Form 應用程式To debug a C#, F#, or Visual Basic Windows Forms application

  1. Visual StudioVisual Studio 中開啟專案。Open the project in Visual StudioVisual Studio.

  2. 建立需要的中斷點。Create breakpoints as needed.

    因為 Windows Form 應用程式是事件驅動的,您的中斷點會進入事件處理常式程式碼中,或事件處理常式程式碼所呼叫的方法中。Because Windows Forms applications are event-driven, your breakpoints will go into event handler code, or into methods called by event handler code. 通常放置中斷點的事件包括:Typical events in which to place breakpoints include:

    1. 與控制項相關的事件,例如點選、輸入等等。Events associated with a control, such as Click, Enter, etc.

    2. 與啟動和關閉應用程式有關的事件,例如載入、啟動等等。Events associated with application startup and shutdown, such as Load, Activated, etc.

    3. 焦點和驗證事件。Focus and Validation Events.

      如需詳細資訊,請參閱在 Windows Forms 中建立事件處理常式For more information, see Creating Event Handlers in Windows Forms.

  3. 按一下 [偵錯] 功能表上的 [開始]。On the Debug menu, click Start.

  4. 使用第一次查看偵錯工具中所討論的技術來進行 Debug。Debug using the techniques discussed in First look at the debugger.

請參閱See also