步驟 10:撰寫其他按鈕和核取方塊的程式碼
現在,您可以準備完成其他四個方法。 您可以複製及貼上此程式碼,但如果想要充分學習本教學課程,請輸入程式碼並使用 IntelliSense。
本主題的影像版本,請參閱教學課程 1: 建立圖片檢視器中 Visual Basic-視訊 5 或 教學課程 1: 建立圖片檢視器中 C#-視訊 5。
注意事項 |
---|
最好的做法是永遠為程式碼加上註解。註解是供使用者閱讀的資訊,值得花時間讓您的程式碼變得更易於理解。程式會忽略註解行上的任何文字。在 Visual C# 中,在一行的開頭輸入兩個斜線 (//),即可將此行變成註解,在 Visual Basic 中,在一行的開頭加上單引號 (') 可將一行變成註解。 |
若要撰寫程式碼的其他按鈕和核取方塊
加入下列程式碼。
Private Sub clearButton_Click() Handles clearButton.Click ' Clear the picture. PictureBox1.Image = Nothing End Sub Private Sub backgroundButton_Click() Handles backgroundButton.Click ' Show the color dialog box. If the user clicks OK, change the ' PictureBox control's background to the color the user chose. If ColorDialog1.ShowDialog() = DialogResult.OK Then PictureBox1.BackColor = ColorDialog1.Color End If End Sub Private Sub closeButton_Click() Handles closeButton.Click ' Close the form. Close() End Sub Private Sub CheckBox1_CheckedChanged() Handles CheckBox1.CheckedChanged ' If the user selects the Stretch check box, change ' the PictureBox's SizeMode property to "Stretch". If the user ' clears the check box, change it to "Normal". If CheckBox1.Checked Then PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage Else PictureBox1.SizeMode = PictureBoxSizeMode.Normal End If End Sub
private void clearButton_Click(object sender, EventArgs e) { // Clear the picture. pictureBox1.Image = null; } private void backgroundButton_Click(object sender, EventArgs e) { // Show the color dialog box. If the user clicks OK, change the // PictureBox control's background to the color the user chose. if (colorDialog1.ShowDialog() == DialogResult.OK) pictureBox1.BackColor = colorDialog1.Color; } private void closeButton_Click(object sender, EventArgs e) { // Close the form. this.Close(); } private void checkBox1_CheckedChanged(object sender, EventArgs e) { // If the user selects the Stretch check box, // change the PictureBox's // SizeMode property to "Stretch". If the user clears // the check box, change it to "Normal". if (checkBox1.Checked) pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; else pictureBox1.SizeMode = PictureBoxSizeMode.Normal; }
若要繼續或檢視
若要移到下一個教學課程步驟,請參閱步驟 11:執行您的程式並嘗試其他功能。
若要回到上一個教學課程步驟,請參閱步驟 9:檢閱、註解和測試您的程式碼。