一、窗體的事件
每一個窗體都有一個事件,這個窗體加載完成之后執行哪一段代碼
位置:(1)右鍵屬性→事件→load 雙擊進入。(2)雙擊窗體任意一個位置進入
public partial class Form1 : Form//構造方法
{
public Form1()
{
InitializeComponent();
}
控件在工具箱里面找,找到之后雙擊即可添加進來,也可以點住拖進來
每個工具,控件,窗體都有一個name,相當於id,用來標識該對象的名稱,name值不允許重復
六大常用控件
1、Label -- 文本顯示工具
Text:需要顯示的文字——屬性
Label的取值賦值:
private void Form1_Load(object sender, EventArgs e) { label1.Text = "請輸入你需要購買的東西:"; MessageBox.Show(label1.Text); }
2、TextBox -- 文本框
TextBox的取值賦值:
private void Form1_Load(object sender, EventArgs e) { textBox1.Text = "請輸入配菜"; MessageBox.Show(textBox1.Text); }
3、radioButton -- 單選按鈕
text:文字
Checked:是否選中
// // radioButton1 // this.radioButton1.AutoSize = true; this.radioButton1.BackColor = System.Drawing.Color.Transparent; this.radioButton1.Checked = true;//決定是否先選中其中一個 this.radioButton1.Font = new System.Drawing.Font("微軟雅黑", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.radioButton1.Location = new System.Drawing.Point(150, 130); this.radioButton1.Name = "radioButton1"; this.radioButton1.Size = new System.Drawing.Size(53, 23); this.radioButton1.TabIndex = 4; this.radioButton1.TabStop = true; this.radioButton1.Text = "薯條"; this.radioButton1.UseVisualStyleBackColor = false;
4 combobox--下拉列表
用於制作下拉菜單
// // comboBox1 // this.comboBox1.BackColor = System.Drawing.SystemColors.Info; //決定着默認無法輸入文字,為灰色區域DropDownList this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.comboBox1.Font = new System.Drawing.Font("微軟雅黑", 8F); this.comboBox1.FormattingEnabled = true; this.comboBox1.Items.AddRange(new object[] { "香辣雞腿堡", "半雞半蝦堡", "BBQ手撕豬肉堡", "老北京雞肉卷", "黑椒牛柳飯", "日式咖喱飯"}); this.comboBox1.Location = new System.Drawing.Point(150, 81); this.comboBox1.Margin = new System.Windows.Forms.Padding(4); this.comboBox1.Name = "comboBox1"; this.comboBox1.Size = new System.Drawing.Size(168, 24); this.comboBox1.TabIndex = 2;
5、checkbox -- 復選框組
Checked屬性:是否選中
Tag屬性:可以存儲自定義數,用戶自己定義
設置全選或者全都不選
private void checkBox3_CheckedChanged(object sender, EventArgs e) { foreach (Control ctr in panel2.Controls) { if (ctr is CheckBox) { CheckBox ck = ctr as CheckBox; ck.Checked = checkBox3.Checked; } } }
6、Button 按鈕
name:設置按鈕名
Text:設置按鈕顯示的文字
雙擊進入設置按鈕的事件(默認為點擊事件)
練習:肯德基訂餐

namespace WindowsFormsApplication1 { partial class Form1 { /// <summary> /// 必需的設計器變量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的資源。 /// </summary> /// <param name="disposing">如果應釋放托管資源,為 true;否則為 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗體設計器生成的代碼 /// <summary> /// 設計器支持所需的方法 - 不要 /// 使用代碼編輯器修改此方法的內容。 /// </summary> private void InitializeComponent() { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1)); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.comboBox1 = new System.Windows.Forms.ComboBox(); this.label3 = new System.Windows.Forms.Label(); this.radioButton1 = new System.Windows.Forms.RadioButton(); this.radioButton2 = new System.Windows.Forms.RadioButton(); this.radioButton3 = new System.Windows.Forms.RadioButton(); this.label4 = new System.Windows.Forms.Label(); this.checkBox1 = new System.Windows.Forms.CheckBox(); this.checkBox2 = new System.Windows.Forms.CheckBox(); this.checkBox3 = new System.Windows.Forms.CheckBox(); this.checkBox4 = new System.Windows.Forms.CheckBox(); this.checkBox5 = new System.Windows.Forms.CheckBox(); this.checkBox6 = new System.Windows.Forms.CheckBox(); this.label5 = new System.Windows.Forms.Label(); this.label6 = new System.Windows.Forms.Label(); this.textBox1 = new System.Windows.Forms.TextBox(); this.textBox2 = new System.Windows.Forms.TextBox(); this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // label1 // this.label1.AutoSize = true; this.label1.BackColor = System.Drawing.Color.Transparent; this.label1.Font = new System.Drawing.Font("微軟雅黑", 20F); this.label1.ForeColor = System.Drawing.Color.Yellow; this.label1.Location = new System.Drawing.Point(164, 9); this.label1.Margin = new System.Windows.Forms.Padding(8, 0, 8, 0); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(204, 35); this.label1.TabIndex = 0; this.label1.Text = "肯德基點餐系統"; this.label1.Click += new System.EventHandler(this.label1_Click); // // label2 // this.label2.AutoSize = true; this.label2.BackColor = System.Drawing.Color.Transparent; this.label2.Font = new System.Drawing.Font("微軟雅黑", 10F); this.label2.ForeColor = System.Drawing.Color.White; this.label2.Location = new System.Drawing.Point(36, 81); this.label2.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(93, 20); this.label2.TabIndex = 1; this.label2.Text = "請選擇主食:"; this.label2.Click += new System.EventHandler(this.label2_Click); // // comboBox1 // this.comboBox1.BackColor = System.Drawing.SystemColors.Info; //決定着默認無法輸入文字,為灰色區域DropDownList this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.comboBox1.Font = new System.Drawing.Font("微軟雅黑", 8F); this.comboBox1.FormattingEnabled = true; this.comboBox1.Items.AddRange(new object[] { "香辣雞腿堡", "半雞半蝦堡", "BBQ手撕豬肉堡", "老北京雞肉卷", "黑椒牛柳飯", "日式咖喱飯"}); this.comboBox1.Location = new System.Drawing.Point(150, 81); this.comboBox1.Margin = new System.Windows.Forms.Padding(4); this.comboBox1.Name = "comboBox1"; this.comboBox1.Size = new System.Drawing.Size(168, 24); this.comboBox1.TabIndex = 2; // // label3 // this.label3.AutoSize = true; this.label3.BackColor = System.Drawing.Color.Transparent; this.label3.Font = new System.Drawing.Font("微軟雅黑", 10F); this.label3.ForeColor = System.Drawing.Color.White; this.label3.Location = new System.Drawing.Point(36, 132); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(93, 20); this.label3.TabIndex = 3; this.label3.Text = "請選擇配餐:"; // // radioButton1 // this.radioButton1.AutoSize = true; this.radioButton1.BackColor = System.Drawing.Color.Transparent; this.radioButton1.Checked = true;//決定是否先選中其中一個 this.radioButton1.Font = new System.Drawing.Font("微軟雅黑", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.radioButton1.Location = new System.Drawing.Point(150, 130); this.radioButton1.Name = "radioButton1"; this.radioButton1.Size = new System.Drawing.Size(53, 23); this.radioButton1.TabIndex = 4; this.radioButton1.TabStop = true; this.radioButton1.Text = "薯條"; this.radioButton1.UseVisualStyleBackColor = false; // // radioButton2 // this.radioButton2.AutoSize = true; this.radioButton2.BackColor = System.Drawing.Color.Transparent; this.radioButton2.Font = new System.Drawing.Font("微軟雅黑", 10F); this.radioButton2.Location = new System.Drawing.Point(237, 129); this.radioButton2.Name = "radioButton2"; this.radioButton2.Size = new System.Drawing.Size(69, 24); this.radioButton2.TabIndex = 5; this.radioButton2.Text = "玉米棒"; this.radioButton2.UseVisualStyleBackColor = false; this.radioButton2.CheckedChanged += new System.EventHandler(this.radioButton2_CheckedChanged); // // radioButton3 // this.radioButton3.AutoSize = true; this.radioButton3.BackColor = System.Drawing.Color.Transparent; this.radioButton3.Font = new System.Drawing.Font("微軟雅黑", 10F); this.radioButton3.Location = new System.Drawing.Point(344, 129); this.radioButton3.Name = "radioButton3"; this.radioButton3.Size = new System.Drawing.Size(69, 24); this.radioButton3.TabIndex = 6; this.radioButton3.Text = "雞米花"; this.radioButton3.UseVisualStyleBackColor = false; this.radioButton3.CheckedChanged += new System.EventHandler(this.radioButton3_CheckedChanged); // // label4 // this.label4.AutoSize = true; this.label4.BackColor = System.Drawing.Color.Transparent; this.label4.Font = new System.Drawing.Font("微軟雅黑", 10F); this.label4.ForeColor = System.Drawing.Color.White; this.label4.Location = new System.Drawing.Point(36, 184); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(93, 20); this.label4.TabIndex = 7; this.label4.Text = "請選擇飲品:"; this.label4.Click += new System.EventHandler(this.label4_Click); // // checkBox1 // this.checkBox1.AutoSize = true; this.checkBox1.BackColor = System.Drawing.Color.Transparent; this.checkBox1.Font = new System.Drawing.Font("微軟雅黑", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.checkBox1.ForeColor = System.Drawing.Color.Black; this.checkBox1.Location = new System.Drawing.Point(149, 183); this.checkBox1.Name = "checkBox1"; this.checkBox1.Size = new System.Drawing.Size(54, 23); this.checkBox1.TabIndex = 8; this.checkBox1.Text = "奶茶"; this.checkBox1.UseVisualStyleBackColor = false; // // checkBox2 // this.checkBox2.AutoSize = true; this.checkBox2.BackColor = System.Drawing.Color.Transparent; this.checkBox2.Font = new System.Drawing.Font("微軟雅黑", 10F); this.checkBox2.ForeColor = System.Drawing.Color.Black; this.checkBox2.Location = new System.Drawing.Point(237, 182); this.checkBox2.Name = "checkBox2"; this.checkBox2.Size = new System.Drawing.Size(56, 24); this.checkBox2.TabIndex = 9; this.checkBox2.Text = "可樂"; this.checkBox2.UseVisualStyleBackColor = false; // // checkBox3 // this.checkBox3.AutoSize = true; this.checkBox3.BackColor = System.Drawing.Color.Transparent; this.checkBox3.Font = new System.Drawing.Font("微軟雅黑", 10F); this.checkBox3.Location = new System.Drawing.Point(150, 212); this.checkBox3.Name = "checkBox3"; this.checkBox3.Size = new System.Drawing.Size(56, 24); this.checkBox3.TabIndex = 10; this.checkBox3.Text = "豆漿"; this.checkBox3.UseVisualStyleBackColor = false; // // checkBox4 // this.checkBox4.AutoSize = true; this.checkBox4.BackColor = System.Drawing.Color.Transparent; this.checkBox4.Font = new System.Drawing.Font("微軟雅黑", 10F); this.checkBox4.Location = new System.Drawing.Point(344, 212); this.checkBox4.Name = "checkBox4"; this.checkBox4.Size = new System.Drawing.Size(56, 24); this.checkBox4.TabIndex = 11; this.checkBox4.Text = "聖代"; this.checkBox4.UseVisualStyleBackColor = false; this.checkBox4.CheckedChanged += new System.EventHandler(this.checkBox4_CheckedChanged); // // checkBox5 // this.checkBox5.AutoSize = true; this.checkBox5.BackColor = System.Drawing.Color.Transparent; this.checkBox5.Font = new System.Drawing.Font("微軟雅黑", 10F); this.checkBox5.Location = new System.Drawing.Point(236, 212); this.checkBox5.Name = "checkBox5"; this.checkBox5.Size = new System.Drawing.Size(70, 24); this.checkBox5.TabIndex = 12; this.checkBox5.Text = "蔬菜湯"; this.checkBox5.UseVisualStyleBackColor = false; // // checkBox6 // this.checkBox6.AutoSize = true; this.checkBox6.BackColor = System.Drawing.Color.Transparent; this.checkBox6.Font = new System.Drawing.Font("微軟雅黑", 10F); this.checkBox6.Location = new System.Drawing.Point(344, 182); this.checkBox6.Name = "checkBox6"; this.checkBox6.Size = new System.Drawing.Size(56, 24); this.checkBox6.TabIndex = 13; this.checkBox6.Text = "牛奶"; this.checkBox6.UseVisualStyleBackColor = false; // // label5 // this.label5.AutoSize = true; this.label5.BackColor = System.Drawing.Color.Transparent; this.label5.Font = new System.Drawing.Font("微軟雅黑", 10F); this.label5.ForeColor = System.Drawing.Color.White; this.label5.Location = new System.Drawing.Point(36, 258); this.label5.Name = "label5"; this.label5.Size = new System.Drawing.Size(93, 20); this.label5.TabIndex = 14; this.label5.Text = "請輸入地址:"; // // label6 // this.label6.AutoSize = true; this.label6.BackColor = System.Drawing.Color.Transparent; this.label6.Font = new System.Drawing.Font("微軟雅黑", 10F); this.label6.ForeColor = System.Drawing.Color.White; this.label6.Location = new System.Drawing.Point(36, 331); this.label6.Name = "label6"; this.label6.Size = new System.Drawing.Size(93, 20); this.label6.TabIndex = 15; this.label6.Text = "請輸入電話:"; this.label6.Click += new System.EventHandler(this.label6_Click); // // textBox1 // this.textBox1.Font = new System.Drawing.Font("微軟雅黑", 10F); this.textBox1.Location = new System.Drawing.Point(149, 258); this.textBox1.Multiline = true; this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(264, 55); this.textBox1.TabIndex = 16; // // textBox2 // this.textBox2.Font = new System.Drawing.Font("微軟雅黑", 10F); this.textBox2.Location = new System.Drawing.Point(149, 331); this.textBox2.Multiline = true; this.textBox2.Name = "textBox2"; this.textBox2.Size = new System.Drawing.Size(263, 24); this.textBox2.TabIndex = 17; // // button1 // this.button1.BackColor = System.Drawing.Color.Transparent; this.button1.Font = new System.Drawing.Font("微軟雅黑", 15F); this.button1.Location = new System.Drawing.Point(179, 367); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(126, 37); this.button1.TabIndex = 18; this.button1.Text = "提交"; this.button1.UseVisualStyleBackColor = false; this.button1.Click += new System.EventHandler(this.button1_Click); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(16F, 35F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.BackgroundImage = global::WindowsFormsApplication1.Properties.Resources._81093c0c839ca9eff9a92b5c38ad3686; this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; this.ClientSize = new System.Drawing.Size(517, 418); this.Controls.Add(this.button1); this.Controls.Add(this.textBox2); this.Controls.Add(this.textBox1); this.Controls.Add(this.label6); this.Controls.Add(this.label5); this.Controls.Add(this.checkBox6); this.Controls.Add(this.checkBox5); this.Controls.Add(this.checkBox4); this.Controls.Add(this.checkBox3); this.Controls.Add(this.checkBox2); this.Controls.Add(this.checkBox1); this.Controls.Add(this.label4); this.Controls.Add(this.radioButton3); this.Controls.Add(this.radioButton2); this.Controls.Add(this.radioButton1); this.Controls.Add(this.label3); this.Controls.Add(this.comboBox1); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Font = new System.Drawing.Font("微軟雅黑", 20F); this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.Margin = new System.Windows.Forms.Padding(8, 7, 8, 7); this.Name = "Form1"; this.Text = "KFC"; this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.ComboBox comboBox1; private System.Windows.Forms.Label label3; private System.Windows.Forms.RadioButton radioButton1; private System.Windows.Forms.RadioButton radioButton2; private System.Windows.Forms.RadioButton radioButton3; private System.Windows.Forms.Label label4; private System.Windows.Forms.CheckBox checkBox1; private System.Windows.Forms.CheckBox checkBox2; private System.Windows.Forms.CheckBox checkBox3; private System.Windows.Forms.CheckBox checkBox4; private System.Windows.Forms.CheckBox checkBox5; private System.Windows.Forms.CheckBox checkBox6; private System.Windows.Forms.Label label5; private System.Windows.Forms.Label label6; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.Button button1; } }

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); comboBox1.SelectedIndex = 0;//設置下拉框的默認值為第一行數據而不是為空; } private void label1_Click(object sender, EventArgs e) { } private void radioButton3_CheckedChanged(object sender, EventArgs e) { } private void radioButton2_CheckedChanged(object sender, EventArgs e) { } private void label4_Click(object sender, EventArgs e) { } private void checkBox4_CheckedChanged(object sender, EventArgs e) { } private void label6_Click(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { //主食 string zs = comboBox1.SelectedItem.ToString(); //配餐 string pc = ""; if (radioButton1.Checked)//判斷是否選中 pc = radioButton1.Text; else if (radioButton2.Checked) pc = radioButton2.Text; else pc = radioButton3.Text; //飲品 string yp = ""; if (checkBox1.Checked)//判斷飲品1是否選中 yp += checkBox1.Text; if (checkBox2.Checked) { if (yp != "")//如果前面已經有選擇過的飲品就加逗號 { yp += ","; } yp += checkBox2.Text; } if (checkBox3.Checked) { if (yp != "") { yp += ","; } yp += checkBox3.Text; } if (checkBox4.Checked) { if (yp != "") { yp += ","; } yp += checkBox4.Text; } if (checkBox5.Checked) { if (yp != "") { yp += ","; } yp += checkBox5.Text; if (checkBox6.Checked) { if (yp != "") { yp += ","; } yp += checkBox6.Text; } } //地址 string dz = textBox1.Text; //電話 string tel = textBox2.Text; MessageBox.Show("您選的的主食是:" + zs + "\r您選擇的配餐是:" + pc + "\r您選擇的飲品是:" + yp + "\r配送地址:" + dz + "\r聯系電話:" + tel); } private void label2_Click(object sender, EventArgs e) { } } }