关于Validating事件的使用联系。以及相关的文本编辑操作。参考自c#经典入门
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace Validated
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public partial class Form1 : Form
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public Form1()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
InitializeComponent();
this.btnOk.Enabled = false;
this.txtBoxAddress.Tag = false;
this.txtBoxAge.Tag = false;
this.txtBoxName.Tag = false;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.txtBoxAddress.TextChanged += new System.EventHandler(this.txtBox_Change);
this.txtBoxAge.TextChanged += new System.EventHandler(this.txtBox_Change);
//this.txtBoxName.TextChanged += new System.EventHandler(this.txtBox_Change);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void txtBoxAge_KeyPress(object sender, KeyPressEventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8)
e.Handled = true;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void txtBox_Change(object sender, System.EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
TextBox tx = (TextBox)sender;
if (tx.Text.Length == 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
tx.Tag = false;
tx.BackColor = Color.Red;
}
else if (tx.Text.Length != 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
tx.Tag = true;
tx.BackColor = SystemColors.Window;
}
ValidateOk();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void ValidateOk()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.btnOk.Enabled = (bool)txtBoxName.Tag && (bool)txtBoxAddress.Tag && (bool)txtBoxAge.Tag;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void btnOk_Click(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string outPut;
outPut = "Name:" + this.txtBoxName.Text + "\r\n";
outPut += "Age:" + this.txtBoxAge.Text + "\r\n";
outPut += "Address:" + this.txtBoxAddress.Text + "\r\n";
richTextBox1.Text = outPut;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void btnHelp_Click(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string outPut;
outPut = "Name:" + this.txtBoxName.Text + "\r\n";
outPut += "Age:" + this.txtBoxAge.Text + "\r\n";
outPut += "Address:" +" "+ this.txtBoxAddress.Text + "\r\n";
richTextBox1.Text = outPut;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void txtBoxName_Validating(object sender, CancelEventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (txtBoxName.Text.Length == 0 || txtBoxName.Text.CompareTo("chen") != 0)
txtBoxName.Tag = false;
else txtBoxName.Tag = true;
ValidateOk();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void btnBold_Click(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Font oldFont;
Font newFont;
oldFont = this.richTextBox1.SelectionFont;
if (oldFont.Bold)
newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Bold);
else newFont = new Font(oldFont, oldFont.Style | FontStyle.Bold);
this.richTextBox1.SelectionFont = newFont;
this.richTextBox1.Focus();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void btnCenter_Click(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (richTextBox1.SelectionAlignment == HorizontalAlignment.Center)
this.richTextBox1.SelectionAlignment = HorizontalAlignment.Left;
else this.richTextBox1.SelectionAlignment = HorizontalAlignment.Center;
this.richTextBox1.Focus();
}
private void ApplyTextSize(string textSize)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// Convert the text to a float because we'll be needing a float shortly
float newSize = Convert.ToSingle(textSize);
FontFamily currentFontFamily;
Font newFont;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Create a new font of the same family but with the new size
currentFontFamily = this.richTextBox1.SelectionFont.FontFamily;
newFont = new Font(currentFontFamily, newSize);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Set the font of the selected text to the new font
this.richTextBox1.SelectionFont = newFont;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void richTextBox1_LinkClicked(object sender, LinkClickedEventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
System.Diagnostics.Process.Start(e.LinkText);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void txtBoxSize_Validating(object sender, CancelEventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
TextBox txt = (TextBox)sender;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ApplyTextSize(txt.Text);
this.richTextBox1.Focus();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void txtBoxSize_KeyPress(object sender, KeyPressEventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// Remove all characters that are not numbers, backspace and enter
if ((e.KeyChar < 48 || e.KeyChar > 57) &&
e.KeyChar != 8 && e.KeyChar != 13)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
e.Handled = true;
}
else if (e.KeyChar == 13)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// Apply size if the user hits enter
TextBox txt = (TextBox)sender;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (txt.Text.Length > 0)
ApplyTextSize(txt.Text);
e.Handled = true;
this.richTextBox1.Focus();
}
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)