今天寫了一個WinForm的練習,將源代碼貼出來和大家一起學習學習。
首先:按照下圖將一個button控件、三個RadioButton控件、三個CheckBox控件、一個Label控件和一個TrackBar控件。
其次:如圖 進度條是用來控制 ① 的字體大小的。
代碼如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Ch12Ex04 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } /// <summary> /// 點擊button按鈕觸發事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { string RadioButton = null; string CheckBoxs = new string('\0', 0); if (radioButton1.Checked) { RadioButton = radioButton1.Text; } if (radioButton2.Checked) { RadioButton = radioButton2.Text; } if (radioButton3.Checked) { RadioButton = radioButton3.Text; } if (checkBox1.Checked) { CheckBoxs += checkBox1.Text + " "; } if (checkBox2.Checked) { CheckBoxs += checkBox2.Text + " "; } if (checkBox3.Checked) { CheckBoxs += checkBox3.Text + " "; } if (CheckBoxs.Length == 0) { CheckBoxs = "沒有復選框被選中"; } else { CheckBoxs = "復選框 " + CheckBoxs + " 被選中"; } if (RadioButton==null) { RadioButton = "沒有單選框被選中"; } else { RadioButton = "單選框 " + RadioButton + " 被選中"; } MessageBox.Show(RadioButton + Environment.NewLine + Environment.NewLine + CheckBoxs); this.Close(); } /// <summary> /// trackBar1_Scroll方法是用來控制text文本的大小的 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void trackBar1_Scroll(object sender, EventArgs e) { //文本字體 FontFamily oldFontFamily = this.label1.Font.FontFamily; //樣式 FontStyle oldFontStyle = this.label1.Font.Style; //獲得滾動條當前的值 float fontSize = this.trackBar1.Value; Font newFont = new Font(oldFontFamily, fontSize, oldFontStyle); this.label1.Font = newFont; } } }
結果如下圖:
到了這里基本上這個程序就運行完畢了。