步驟 6:命名您的按鈕控制項Step 6: Name your button controls

表單上只有一個 PictureBoxThere's only one PictureBox on your form. 加入它時,IDE 會自動將它命名為 pictureBox1When you added it, the IDE automatically named it pictureBox1. 只有一個 CheckBox,名稱為 checkBox1There's only one CheckBox, which is named checkBox1. 很快,您將編寫一些代碼,該代碼將引用 CheckBox 和圖片框。Soon, you'll write some code, and that code will refer to the CheckBox and PictureBox. 由於每個控制項中只有一個,因此當您在代碼中看到圖片Box1checkBox1時,您將知道這意味著什麼。Because there's only one of each of these controls, you'll know what it means when you see pictureBox1 or checkBox1 in your code.

提示

在 Visual Basic 中,任何控制項名稱的預設第一個字母都是大寫,所以是 PictureBox1CheckBox1,依此類推。In Visual Basic, the default first letter of any control name is initial cap, so the names are PictureBox1, CheckBox1, and so on.

表單上有四個按鈕,IDE 將它們分別命名為 button1button2button3button4There are four buttons on your form, and the IDE named them button1, button2, button3, and button4. 只從目前的名稱來看,並無法得知哪一個按鈕才是 [關閉] **** 按鈕,以及哪一個是 [顯示圖片] **** 按鈕。By just looking at their current names, you don't know which button is the Close button and which one is the Show a picture button. 這就是為按鈕控制項指定更具資訊性名稱會很有用的原因。That's why giving your button controls more informative names is helpful.

命名您的按鈕控制項To name your button controls

  1. 在表單上,選擇 [關閉] **** 按鈕。On the form, choose the Close button. (如果仍然選擇了所有按鈕,請選擇Esc鍵以取消選擇。在 "屬性" 視窗中滾動,直到看到 (名稱) 屬性。(If you still have all the buttons selected, choose the Esc key to cancel the selection.) Scroll in the Properties window until you see the (Name) property. (名稱 屬性在屬性按字母順序排列時靠近頂部。將名稱更改為關閉Button,如以下螢幕截圖所示。(The (Name) property is near the top when the properties are alphabetical.) Change the name to closeButton, as shown in the following screenshot.

    包含 closeButton 名稱的 [屬性] 視窗Properties window with closeButton name
    *** ****具有****關閉按鈕****名稱*的屬性視窗Properties window with closeButton name

    注意

    嘗試更改按鈕的名稱以關閉"按鈕",在"關閉"和"按鈕"兩個單詞之間有一個空格。Try changing the name of your button to close Button, with a space between the words "close" and "Button". 執行此操作時,IDE 將顯示一條錯誤訊息:"屬性值無效。When you do so, the IDE displays an error message: "Property value is not valid." 控制項名稱中不允許空格 (和其他一些字元)。Spaces (and a few other characters) are not allowed in control names.

  2. 將其他三個按鈕分別重新命名為 backgroundButtonclearButtonshowButtonRename the other three buttons to backgroundButton, clearButton, and showButton. 您可以選擇 [屬性] **** 視窗中的控制項選取器下拉式清單,以驗證這些名稱。You can verify the names by choosing the control selector drop-down list in the Properties window. 新按鈕名稱隨即出現。The new button names appear.

  3. 按兩下表單上的 [顯示圖片] **** 按鈕。Double-click the Show a picture button on the form. 作為替代方法,請選擇表單上的 "顯示圖片"按鈕,然後按Enter鍵。As an alternative, choose the Show a picture button on the form, and then press the Enter key. 執行此操作時,IDE 將在名為Form1.cs的主視窗中打開一個附加選項卡。When you do, the IDE opens an additional tab in the main window named Form1.cs. (如果您使用的是視覺化基本,則選項卡名為Form1.vb(If you're using Visual Basic, the tab is named Form1.vb).

    此選項卡顯示表單後面的代碼檔,如以下螢幕截圖所示。This tab displays the code file behind the form, as shown in the following screenshot.

    包含 Visual C# 程式碼的 [Form1.cs] 索引標籤Form1.cs tab with Visual C# code
    **Form1.cs****選項卡,帶有 C# 代碼Form1.cs tab with C# code

    注意

    您的Form1.cs或 Form1.vb 選項卡可能會將 "顯示按鈕" 顯示為 "顯示按鈕"。Your Form1.cs or Form1.vb tab might display showButton as ShowButton instead.

  4. 注意這部分程式碼。Focus on this part of the code.

        private void ShowButton_Click(object sender, EventArgs e)
    {
    }
    
        Private Sub showButton_Click() Handles showButton.Click
    
    End Sub
    

    重要

    使用此頁面右上角的程式設計語言控制項查看 C# 程式碼片段或 Visual Basic 程式碼片段。Use the programming language control at the top right of this page to view either the C# code snippet or the Visual Basic code snippet.

    程式設計語言控制Docs.Microsoft.comProgramming language control for Docs.Microsoft.com

    您正在查看名為showButton_Click()(或者, ShowButton_Click()) 的代碼。You're looking at code called showButton_Click() (alternatively, ShowButton_Click()). 當您開啟 showButton 按鈕的程式碼檔時,IDE 已將此加入至表單的程式碼。The IDE added this to the form's code when you opened the code file for the showButton button. 在設計階段,當您開啟表單中的控制項程式碼檔案時,如果該控制項的程式碼不存在,會產生程式碼。At design-time, when you open the code file for a control in a form, code is generated for the control if it doesn't already exist. 此代碼(稱為方法)在運行應用並選擇控制項時運行 - 在這種情況下,請顯示圖片按鈕。This code, known as a method, runs when you run your app and choose the control - in this case, the Show a picture button.

  5. 再次選擇Windows 表單設計器選項卡 (Form1.cs [設計]), 然後打開 "清除圖片"按鈕的代碼檔,在表單的代碼中為其創建方法。Choose the Windows Forms Designer tab again (Form1.cs [Design]), and then open the code file for the Clear the picture button to create a method for it in the form's code. 針對其他兩個按鈕重複此步驟。Repeat this for the remaining two buttons. IDE 每一次都會將新的方法加入至表單的程式碼檔案。Each time, the IDE adds a new method to the form's code file.

  6. 要再添加一種方法,請在Windows 表單設計器中打開CheckBox控制項的代碼檔,以使checkBox1_CheckedChanged()IDE 添加方法。To add one more method, open the code file for the CheckBox control in Windows Forms Designer to make the IDE add a checkBox1_CheckedChanged() method. 每當使用者選取或清除核取方塊時就會呼叫該方法。That method is called whenever the user selects or clears the check box.

    提示

    處理應用時,您經常在代碼編輯器和Windows 表單設計器之間移動。When working on an app, you often move between the code editor and Windows Forms Designer. IDE 可讓您在專案中輕鬆巡覽。The IDE makes it easy to navigate in your project. 使用解決方案資源管理器通過按兩下"視覺基礎"中的 C# 或Form1.vb中的Form1.cs, 或在功能表列上按一下"查看 > 設計器",從而打開Windows 表單設計器Use Solution Explorer to open Windows Forms Designer by double-clicking Form1.cs in C# or Form1.vb in Visual Basic, or on the menu bar, choose View > Designer.

    下列顯示您在程式碼編輯器中看到的新程式碼。The following shows the new code that you see in the code editor.

    private void clearButton_Click(object sender, EventArgs e)
    {
    }
    
    private void backgroundButton_Click(object sender, EventArgs e)
    {
    }
    
    private void closeButton_Click(object sender, EventArgs e)
    {
    }
    
    private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
    }
    
    Private Sub clearButton_Click() Handles clearButton.Click
    
    End Sub
    
    Private Sub backgroundButton_Click() Handles backgroundButton.Click
    
    End Sub
    
    Private Sub closeButton_Click() Handles closeButton.Click
    
    End Sub
    
    Private Sub CheckBox1_CheckedChanged() Handles CheckBox1.CheckedChanged
    
    End Sub
    

    注意

    您的代碼可能不會以"camelCase"字母顯示事件處理常式。Your code might not display event handlers in "camelCase" letters.

    您添加的五種方法稱為事件處理常式,因為每當發生事件(如使用者選擇按鈕或選擇框)時,應用程式都會調用它們。The five methods that you added are called event handlers, because your application calls them whenever an event (like a user choosing a button or selecting a box) happens.

    當您在設計階段於 IDE 檢視控制項的程式碼時,Visual Studio 會加入控制項的事件處理常式方法 (如果不存在)。When you view the code for a control in the IDE at design time, Visual Studio adds an event handler method for the control if one isn't there. 例如,當您按兩下按鈕時,IDE 會為其 Click 事件加入事件處理常式 (每當使用者選擇按鈕時,就會呼叫)。For example, when you double-click a button, the IDE adds an event handler for its Click event (which is called whenever the user chooses the button). 當您按兩下核取方塊時,IDE 會為其 CheckedChanged 事件加入事件處理常式 (每當使用者選取或清除方塊時,就會呼叫)。When you double-click a check box, the IDE adds an event handler for its CheckedChanged event (which is called whenever the user selects or clears the box).

    為控制項加入事件處理常式之後,您隨時可以按兩下控制項,或是在功能表列上依序選擇 [檢視]**** > [程式碼]****,即可從 Windows Forms 設計工具回到此控制項。After you add an event handler for a control, you can return to it at any time from Windows Forms Designer by double-clicking the control, or on the menu bar, choosing View > Code.

    當您建置程式時,名稱很重要,而方法 (包括事件處理常式) 則可以具有您想要的任何名稱。Names are important when you build programs, and methods (including event handlers) can have any name that you want. 當您使用 IDE 加入事件處理常式時,IDE 會根據控制項的名稱和所處理的事件來建立名稱。When you add an event handler with the IDE, it creates a name based on the control's name and the event being handled.

    例如,名為showButton的按鈕的 Click 事件稱為showButton_Click()(或者ShowButton_Click()) 事件處理常式方法。For example, the Click event for a button named showButton is called the showButton_Click() (alternatively, ShowButton_Click()) event handler method. 此外,方法名稱後面通常會加上左括弧和右括弧 (),以表示目前所討論的是方法。Also, opening and closing parentheses () are usually added after the method name to indicate that methods are being discussed.

    如果您決定要更改代碼變數名稱,請按右鍵代碼中的變數,然後選擇重構 > 重命名If you decide you want to change a code variable name, right-click the variable in the code and then choose Refactor > Rename. 會重新命名該變數在程式碼中的所有執行個體。All instances of that variable in the code are renamed. 有關詳細資訊,請參閱重命名重構For more information, see Rename refactoring.

後續步驟Next steps

另請參閱See also