C# 经典入门15章-TextBoxControl
第一步:设计界面如下:
第二步为其空间添加属性:因时间限制,附上截图
} /// <summary> /// buttonOK的事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button3_Click(object sender, EventArgs e) { string output; output = "Name:" + this.textBoxName.Text + "\r\n"; output += "Address:" + this.textBoxAddress.Text + "\r\n"; output += "职业:" + this.textBoxOccupation.Text + "\r\n"; output += "年龄:" + this.textBoxAge.Text + "\r\n"; this.textBoxOutPut.Text = output; }
/// <summary> /// buttonHelp的事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button4_Click(object sender, EventArgs e) { string output; output = "Name=YourName\r\n"; output += "Address=Your address\r\n"; this.textBoxOutPut.Text = output; }
/// <summary> /// 文本框不能为空事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void textBox_Empty_Validating(object sender, CancelEventArgs e) { TextBox tb = (TextBox)sender; if (tb.Text.Length == 0) tb.BackColor = Color.Red; else tb.BackColor = System.Drawing.SystemColors.Window; ValidateOK(); }
/// <summary> /// 职业文本框的内容 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void textBoxOccupation_Validating(object sender, CancelEventArgs e) { TextBox tb = (TextBox)sender; if (tb.Text == "Programmer" || tb.Text.Length == 0) tb.BackColor = System.Drawing.SystemColors.Window; else tb.BackColor = Color.Red; ValidateOK(
/// <summary> /// 控制年龄 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void textBoxAge_KeyPress(object sender, KeyPressEventArgs e) {//0-9之间数字的ASCII值是48-57 ASCII 8是退格键 if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8) e.Handled = true; }
/// <summary> /// 启用或禁用Ok按钮的ValidateOK方法 /// </summary> private void ValidateOK() { button3.Enabled=(textBoxName.BackColor!=Color.Red&& textBoxAddress.BackColor!=Color.Red&& textBoxOccupation.BackColor!=Color.Red&& textBoxAge.BackColor!=Color.Red); }