代碼如下: public partial class Textboxtext : Form ...{ public Textboxtext() ...{ InitializeComponent(); btnClose.CausesValidation = false;//當單擊按鈕時不考慮驗証狀態 txtUsename.Tag = false; txtAddress.Tag = false; txtAge.Tag = false; txtOccupation.Tag = false; btnOK.Enabled = false; txtUsename.Validating += new CancelEventHandler(txtBoxEmpty_Validating); txtAddress.Validating += new CancelEventHandler(txtBoxEmpty_Validating); txtOccupation.Validating += new CancelEventHandler(txtOccupation_Validating); txtAge.KeyPress += new KeyPressEventHandler(txtAge_KeyPress); txtAge.Validating += new CancelEventHandler(txtBoxEmpty_Validating); txtUsename.TextChanged += new EventHandler(txt_TextChanged); txtAddress.TextChanged += new EventHandler(txt_TextChanged); txtOccupation.TextChanged += new EventHandler(txt_TextChanged); txtAge.TextChanged += new EventHandler(txt_TextChanged); Combox.KeyDown += new KeyEventHandler(Combox_KeyDown); LoadCombox(); } void Combox_KeyDown(object sender, KeyEventArgs e) ...{ int index = 0; ComboBox cbo = (ComboBox)sender; if (e.KeyCode == Keys.Enter) ...{ index = cbo.FindStringExact(cbo.Text); if (index < 0) cbo.Items.Add(cbo.Text); //else cbo.SelectedIndex = index; } if (e.KeyCode == Keys.Delete) ...{ index = cbo.FindStringExact(cbo.Text); if (index > -1) cbo.Items.Remove(cbo.Text); } } void LoadCombox() ...{ string FilePath = Application.StartupPath + @" xt.txt"; bool exist = File.Exists(FilePath); if (exist == false) ...{ Directory.CreateDirectory(Application.StartupPath); FileStream fs = new FileStream(FilePath, FileMode.Create); fs.Close(); } System.IO.StreamReader sr = new System.IO.StreamReader(FilePath); try ...{ string input; do ...{ input = sr.ReadLine(); if (input != ""&&input!=null) Combox.Items.Add(input); } while (sr.Peek() != -1); sr.Close(); } catch (Exception) ...{ MessageBox.Show("沒有找到相關文件"); } //finally //{ } } void SaveCombox() ...{ try ...{ System.IO.StreamWriter sw = new System.IO.StreamWriter(Application.StartupPath + @" xt.txt"); foreach (string item in Combox.Items) sw.WriteLine(item); sw.Flush(); sw.Close(); } catch (Exception err) ...{ MessageBox.Show(err.Message); } } void txt_TextChanged(object sender, EventArgs e) ...{ TextBox tb = (TextBox)sender; if (tb.Text.Length == 0 && tb != txtOccupation) ...{ tb.BackColor = Color.Purple; tb.Tag = false; } else if (tb == txtOccupation && (txtOccupation.Text.Length != 0 && tb.Text.CompareTo("程序員") != 0)) ...{ tb.BackColor = Color.Purple; tb.Tag = false; } else ...{ tb.BackColor = SystemColors.Window; tb.Tag = true; } ValidateAll(); } void txtOccupation_Validating(object sender, CancelEventArgs e) ...{ TextBox tb = (TextBox)sender; if (tb.Text.CompareTo("程序員") == 0 || tb.Text.Length == 0) ...{ tb.BackColor = SystemColors.Window; tb.Tag = true; } else ...{ tb.BackColor = Color.Purple; tb.Tag = false; } ValidateAll(); } void txtAge_KeyPress(object sender, KeyPressEventArgs e) ...{ if ((e.KeyChar < 48 || e.KeyChar > 57)&&e.KeyChar!=8)//ASC值8表示退位鍵 e.Handled = true; } void txtBoxEmpty_Validating(object sender, CancelEventArgs e) ...{ TextBox tb = (TextBox)sender; if (tb.Text.Length == 0) ...{ tb.BackColor = Color.Purple; tb.Tag = false; //e.Cancel = true; } else ...{ tb.BackColor = SystemColors.Window; tb.Tag = true; } ValidateAll(); } private void btnOK_Click(object sender, EventArgs e) ...{ string outputtxt; outputtxt = "Usename:" + txtUsename.Text + Environment.NewLine; outputtxt += "Address:" + txtAddress.Text + " "; outputtxt += "Occupation:" + txtOccupation.Text + " "; outputtxt += "Age:" + txtAge.Text + " "; outputtxt += "Sex:" +(string)(RdoMale.Checked ? "Male" : "Female") + " "; outputtxt += "星座:" + Combox.Text + " "; txtOutput.Text += outputtxt; } private void btnHelp_Click(object sender, EventArgs e) ...{ string outputtxt; outputtxt = "Usename: 用戶名(不能為空)" + Environment.NewLine; outputtxt += "Address: 地址(不能空空) "; outputtxt += "Occupation: 職業(必須是程序員或為空) "; outputtxt += "Age: 年齡(大於或等於零的一個數字) "; outputtxt += "Sex: 性別 "; outputtxt += "星座: 所屬星座 "; txtOutput.Text += outputtxt; } void ValidateAll() ...{ btnOK.Enabled = (bool)txtUsename.Tag && (bool)txtAddress.Tag && (bool)txtAge.Tag && (bool)txtOccupation.Tag; } private void btnClose_Click(object sender, EventArgs e) ...{ Application.Exit(); } } 運行效果: