using System; using System.Drawing; using System.Windows.Forms; public class CForm : Form { private Label label1; private RadioButton radioButton1; private RadioButton radioButton2; private RadioButton radioButton3; private RadioButton radioButton4; private GroupBox groupBox1; private GroupBox GropList2; private CheckBox checkBox4; private CheckBox checkBox3; private CheckBox checkBox2; private CheckBox checkBox1; private Label label2; private ListBox listBox1; private Label label3; private int m_nLabelClick = 0; public CForm() { InitializeComponent(); radioButton1.Checked = true; } public static void Main() { CForm objForm = new CForm(); Application.Run(objForm); } private void InitializeComponent() { this.label1 = 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.radioButton4 = new System.Windows.Forms.RadioButton(); this.groupBox1 = new System.Windows.Forms.GroupBox(); this.GropList2 = new System.Windows.Forms.GroupBox(); this.checkBox4 = new System.Windows.Forms.CheckBox(); this.checkBox3 = new System.Windows.Forms.CheckBox(); this.checkBox2 = new System.Windows.Forms.CheckBox(); this.checkBox1 = new System.Windows.Forms.CheckBox(); this.label2 = new System.Windows.Forms.Label(); this.listBox1 = new System.Windows.Forms.ListBox(); this.label3 = new System.Windows.Forms.Label(); this.groupBox1.SuspendLayout(); this.GropList2.SuspendLayout(); this.SuspendLayout(); // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(181, 46); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(77, 12); this.label1.TabIndex = 0; this.label1.Text = "字体设置示例"; this.label1.Click += new System.EventHandler(this.label1_Click); // // radioButton1 // this.radioButton1.AutoSize = true; this.radioButton1.Location = new System.Drawing.Point(29, 39); this.radioButton1.Name = "radioButton1"; this.radioButton1.Size = new System.Drawing.Size(47, 16); this.radioButton1.TabIndex = 1; this.radioButton1.Text = "宋体"; this.radioButton1.UseVisualStyleBackColor = true; this.radioButton1.CheckedChanged += new System.EventHandler(this.RadioClick); // // radioButton2 // this.radioButton2.AutoSize = true; this.radioButton2.Location = new System.Drawing.Point(29, 61); this.radioButton2.Name = "radioButton2"; this.radioButton2.Size = new System.Drawing.Size(47, 16); this.radioButton2.TabIndex = 2; this.radioButton2.Text = "仿宋"; this.radioButton2.UseVisualStyleBackColor = true; this.radioButton2.CheckedChanged += new System.EventHandler(this.RadioClick); // // radioButton3 // this.radioButton3.AutoSize = true; this.radioButton3.Location = new System.Drawing.Point(29, 83); this.radioButton3.Name = "radioButton3"; this.radioButton3.Size = new System.Drawing.Size(47, 16); this.radioButton3.TabIndex = 3; this.radioButton3.TabStop = true; this.radioButton3.Text = "楷体"; this.radioButton3.UseVisualStyleBackColor = true; this.radioButton3.CheckedChanged += new System.EventHandler(this.RadioClick); // // radioButton4 // this.radioButton4.AutoSize = true; this.radioButton4.Location = new System.Drawing.Point(29, 105); this.radioButton4.Name = "radioButton4"; this.radioButton4.Size = new System.Drawing.Size(47, 16); this.radioButton4.TabIndex = 4; this.radioButton4.TabStop = true; this.radioButton4.Text = "隶书"; this.radioButton4.UseVisualStyleBackColor = true; this.radioButton4.CheckedChanged += new System.EventHandler(this.RadioClick); // // groupBox1 // this.groupBox1.Controls.Add(this.radioButton2); this.groupBox1.Controls.Add(this.radioButton4); this.groupBox1.Controls.Add(this.radioButton3); this.groupBox1.Controls.Add(this.radioButton1); this.groupBox1.Location = new System.Drawing.Point(197, 116); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(200, 146); this.groupBox1.TabIndex = 2; this.groupBox1.TabStop = false; this.groupBox1.Text = "Radio List"; // // GropList2 // this.GropList2.Controls.Add(this.checkBox4); this.GropList2.Controls.Add(this.checkBox3); this.GropList2.Controls.Add(this.checkBox2); this.GropList2.Controls.Add(this.checkBox1); this.GropList2.Location = new System.Drawing.Point(403, 116); this.GropList2.Name = "GropList2"; this.GropList2.Size = new System.Drawing.Size(200, 146); this.GropList2.TabIndex = 3; this.GropList2.TabStop = false; this.GropList2.Text = "爱好"; // // checkBox4 // this.checkBox4.AutoSize = true; this.checkBox4.Location = new System.Drawing.Point(37, 104); this.checkBox4.Name = "checkBox4"; this.checkBox4.Size = new System.Drawing.Size(48, 16); this.checkBox4.TabIndex = 0; this.checkBox4.Text = "游泳"; this.checkBox4.UseVisualStyleBackColor = true; this.checkBox4.CheckedChanged += new System.EventHandler(this.CheckClick); // // checkBox3 // this.checkBox3.AutoSize = true; this.checkBox3.Location = new System.Drawing.Point(37, 82); this.checkBox3.Name = "checkBox3"; this.checkBox3.Size = new System.Drawing.Size(48, 16); this.checkBox3.TabIndex = 0; this.checkBox3.Text = "跑步"; this.checkBox3.UseVisualStyleBackColor = true; this.checkBox3.CheckedChanged += new System.EventHandler(this.CheckClick); // // checkBox2 // this.checkBox2.AutoSize = true; this.checkBox2.Location = new System.Drawing.Point(37, 60); this.checkBox2.Name = "checkBox2"; this.checkBox2.Size = new System.Drawing.Size(48, 16); this.checkBox2.TabIndex = 0; this.checkBox2.Text = "足球"; this.checkBox2.UseVisualStyleBackColor = true; this.checkBox2.CheckedChanged += new System.EventHandler(this.CheckClick); // // checkBox1 // this.checkBox1.AutoSize = true; this.checkBox1.Location = new System.Drawing.Point(37, 38); this.checkBox1.Name = "checkBox1"; this.checkBox1.Size = new System.Drawing.Size(48, 16); this.checkBox1.TabIndex = 0; this.checkBox1.Text = "篮球"; this.checkBox1.UseVisualStyleBackColor = true; this.checkBox1.CheckedChanged += new System.EventHandler(this.CheckClick); // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(181, 70); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(0, 12); this.label2.TabIndex = 0; this.label2.Click += new System.EventHandler(this.label1_Click); // // listBox1 // this.listBox1.FormattingEnabled = true; this.listBox1.ItemHeight = 12; this.listBox1.Items.AddRange(new object[] { "粗体", "斜体", "下划线", "删除线"}); this.listBox1.Location = new System.Drawing.Point(24, 116); this.listBox1.MultiColumn = true; this.listBox1.Name = "listBox1"; this.listBox1.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended; this.listBox1.Size = new System.Drawing.Size(167, 148); this.listBox1.TabIndex = 4; this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged); // // label3 // this.label3.AutoSize = true; this.label3.Location = new System.Drawing.Point(22, 46); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(101, 12); this.label3.TabIndex = 0; this.label3.Text = "listbox 设置字体"; this.label3.Click += new System.EventHandler(this.label1_Click); // // CForm // this.ClientSize = new System.Drawing.Size(614, 375); this.Controls.Add(this.listBox1); this.Controls.Add(this.GropList2); this.Controls.Add(this.groupBox1); this.Controls.Add(this.label2); this.Controls.Add(this.label3); this.Controls.Add(this.label1); this.Name = "CForm"; this.Text = "My Form"; this.groupBox1.ResumeLayout(false); this.groupBox1.PerformLayout(); this.GropList2.ResumeLayout(false); this.GropList2.PerformLayout(); this.ResumeLayout(false); this.PerformLayout(); } private void label1_Click(object sender, EventArgs e) { m_nLabelClick++; switch (m_nLabelClick % 3) { case 0: this.label1.ForeColor = Color.Red; this.label1.Text = "my color is red"; break; case 1: this.label1.ForeColor = Color.Green; this.label1.Text = "my color is green"; break; default: this.label1.ForeColor = Color.Blue; this.label1.Text = "my color is blue"; break; } } private void RadioClick(object sender, EventArgs e) { if (radioButton1 == sender) { this.label1.Font = new Font("宋体", label1.Font.Size); } else if (radioButton2 == sender) { this.label1.Font = new Font("仿宋", label1.Font.Size); } else if (radioButton3 == sender) { this.label1.Font = new Font("楷体_GB2312", label1.Font.Size); } else { this.label1.Font = new Font("隶书", label1.Font.Size); } } private void CheckClick(object sender, EventArgs e) { this.label2.Text = "我的爱好: "; if (checkBox1.Checked) { this.label2.Text += "篮球 "; } if (checkBox2.Checked) { this.label2.Text += "足球 "; } if (checkBox3.Checked) { this.label2.Text += "跑步 "; } if (checkBox4.Checked) { this.label2.Text += "游泳 "; } } private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { int nStyle = 0; int i = 0; int k = 1; for (i = 0; i < listBox1.Items.Count; i++) { if (listBox1.GetSelected(i)) { nStyle |= k; } else { nStyle &= ~k; } k *= 2; } FontStyle styleFont = (FontStyle)nStyle; label3.Font = new Font(label1.Font.Name,9, styleFont); } }