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;
      }
   }
}
posted @ 2024-01-29 16:08  苏沐~  阅读(377)  评论(0编辑  收藏  举报