ColorDialog 元件會顯示顏色的調色盤並傳回包含使用者所選取的顏色的屬性。
若要使用 ColorDialog 元件來選擇顏色
使用 ShowDialog 方法來顯示對話方塊。
使用 DialogResult 屬性來決定對話方塊的關閉方式。
使用 ColorDialog 元件的 Color 屬性來設定所選的顏色。
在下面的範例中,Button 控制項的 Click 事件處理常式會開啟 FontDialog 元件。 選擇顏色且使用者按一下 [確定] 後,Button 控制項的背景顏色會設定為所選的顏色。 這個範例假設您的表單有一個 Button 控制項和一個 ColorDialog 元件。
Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click If ColorDialog1.ShowDialog() = DialogResult.OK Then Button1.BackColor = ColorDialog1.Color End If End Subprivate void button1_Click(object sender, System.EventArgs e) { if(colorDialog1.ShowDialog() == DialogResult.OK) { button1.BackColor = colorDialog1.Color; } }private: void button1_Click(System::Object ^ sender, System::EventArgs ^ e) { if(colorDialog1->ShowDialog() == DialogResult::OK) { button1->BackColor = colorDialog1->Color; } }(Visual C#、Visual C++) 請將下列程式碼置於表單的建構函式中,以登錄事件處理常式。
this.button1.Click += new System.EventHandler(this.button1_Click);this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);