C# ComboBox、TextBox获取改变前后值
/*存储改变前的值*/
private string ComboBox1_Beforevalue = String.Empty;
private string TextBox1_Beforevalue = String.Empty;
/*ComboBox1*/
this.ComboBox1.SelectedIndexChanged += new EventHandler(this.ComboBox1_SelectedIndexChanged);
this.ComboBox1.DropDown += new System.EventHandler(this.BeforeValueSet);
/*TextBox1*/
this.TextBox1.TextChanged += new EventHandler(this.TextBox1_TextChanged);
this.TextBox1.Validated += new EventHandler(this.BeforeValueSet);
//也可用Enter事件
//this.TextBox1.Enter+= new EventHandler(this.BeforeValueSet);
/*ComboBox1 SelectedIndexChanged事件*/
private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox comboBox = sender as ComboBox;
Console.WriteLine("ComboBox1改变后的值为:{0}", comboBox.Text );
Console.WriteLine("ComboBox1改变前的值为:{0}", ComboBox1_Beforevalue );
}
/*TextBox1 TextChanged事件*/
private void TextBox1_TextChanged(object sender, EventArgs e)
{
TextBox textBox = (TextBox)sender;
Console.WriteLine("TextBox1改变后的值为:{0}", textBox.Text );
Console.WriteLine("TextBox1改变前的值为:{0}", TextBox1_Beforevalue );
}
/*BeforeValueSet*/
private void BeforeValueSet(object sender, EventArgs e)
{
//ComboBox
if (sender is ComboBox)
{
ComboBox comboBox = sender as ComboBox;
switch (comboBox.Name)
{
case "ComboBox1":
ComboBox1_Beforevalue = comboBox.Text;
break;
}
}
//TextBox
if (sender is TextBox)
{
TextBox textBox = (TextBox)sender;
switch (textBox.Name)
{
case "TextBox1":
TextBox1_Beforevalue = textBox.Text;
break;
}
}
}
本文来自博客园,作者:苏沐~,转载请注明原文链接:https://www.cnblogs.com/sumu80/p/17994621