using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace WindowsApplication1
{
/// <summary>
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.ComponentModel.IContainer components;
private System.Windows.Forms.ImageList imageList1;
private System.Windows.Forms.ToolBar toolBar1;
private System.Windows.Forms.ToolBarButton toolBarButton1; private object CurObject = new object(); public Form1()
{
//
//  //
InitializeComponent(); //
// TODO: InitializeComponent 
//
} /// <summary>
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null) 
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region 
/// <summary>
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.textBox1 = new System.Windows.Forms.TextBox();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.imageList1 = new System.Windows.Forms.ImageList(this.components);
this.toolBar1 = new System.Windows.Forms.ToolBar();
this.toolBarButton1 = new System.Windows.Forms.ToolBarButton();
this.SuspendLayout();
// 
// textBox1
// 
this.textBox1.Location = new System.Drawing.Point(152, 88);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(128, 19);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
this.textBox1.Leave += new System.EventHandler(this.textBox1_Leave);
this.textBox1.Enter += new System.EventHandler(this.textBox1_Enter);
// 
// comboBox1
// 
this.comboBox1.Location = new System.Drawing.Point(152, 136);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(128, 20);
this.comboBox1.TabIndex = 1;
this.comboBox1.Text = "comboBox1";
this.comboBox1.Leave += new System.EventHandler(this.comboBox1_Leave);
this.comboBox1.Enter += new System.EventHandler(this.comboBox1_Enter);
// 
// mainMenu1
// 
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  this.menuItem1});
// 
// menuItem1
// 
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  this.menuItem2});
this.menuItem1.Text = "File(&F)";
// 
// menuItem2
// 
this.menuItem2.Index = 0;
this.menuItem2.Text = "Undo(&U)";
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
// 
// imageList1
// 
this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
// 
// toolBar1
// 
this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
this.toolBarButton1});
this.toolBar1.DropDownArrows = true;
this.toolBar1.ImageList = this.imageList1;
this.toolBar1.Location = new System.Drawing.Point(0, 0);
this.toolBar1.Name = "toolBar1";
this.toolBar1.ShowToolTips = true;
this.toolBar1.Size = new System.Drawing.Size(292, 41);
this.toolBar1.TabIndex = 2;
this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
// 
// toolBarButton1
// 
this.toolBarButton1.ImageIndex = 0;
this.toolBarButton1.Text = "Undo";
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
this.ClientSize = new System.Drawing.Size(292, 270);
this.Controls.Add(this.toolBar1);
this.Controls.Add(this.comboBox1);
this.Controls.Add(this.textBox1);
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion /// <summary>
/// 
/// </summary>
[STAThread]
static void Main() 
{
Application.Run(new Form1());
} private void menuItem2_Click(object sender, System.EventArgs e)
{
Type PreType = CurObject.GetType();
if ( PreType == typeof(System.Windows.Forms.TextBox))
{
// Undo the last operation.
((System.Windows.Forms.TextBox)CurObject).Undo();
// Clear the undo buffer to prevent last action from being redone.
((System.Windows.Forms.TextBox)CurObject).ClearUndo();
}
else if ( PreType == typeof(System.Windows.Forms.ComboBox))
{
//object bb = new object();
//bb = ((System.Windows.Forms.ComboBox)CurObject).SelectedItem;
//Type bbb = bb.GetType();
//object aa = this.ActiveControl;
}
} private void textBox1_Enter(object sender, System.EventArgs e)
{
CurObject = this.textBox1;
} private void textBox1_Leave(object sender, System.EventArgs e)
{
CurObject = null;
} private void comboBox1_Enter(object sender, System.EventArgs e)
{
CurObject = this.comboBox1;
} private void comboBox1_Leave(object sender, System.EventArgs e)
{
CurObject = null;
} private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
switch(toolBar1.Buttons.IndexOf(e.Button))
{
case 0:
menuItem2_Click(null, null);
break; 
default:
break;
}
} }
}

解决方案 »

  1.   

    我生成了一个画面,1个ToolBar里面的“Undo”按钮,1个Text输入框,1个ComboBox框,在Undo点击响应事件menuItem2_Click里我做了一个对当前选中控件的类型判断(用了个全局变量CurObject),当选中为TextBox的时候,做((System.Windows.Forms.TextBox)CurObject).Undo();可以将Text输入框的状态返回到之前的状态;但是在Combox的时候,好像没有这个方法了,不知道如何实现Undo这个功能了,但是当我在画面里面右键点击Combox弹出的子菜单中有 Undo的操作选项,我想这个功能应该可以实现的吧,在此请教各位了。
      

  2.   

    用一个ArrayList来记录上几次的操作,当调用Undo时就返回ArrayList[ArrayList.Length-1];
    如果单击一次Undo返回ArrayList[ArrayList.Length-1];
    单击二次Undo返回ArrayList[ArrayList.Length-2];
    ....
      

  3.   

    谢谢。
    有没有稍微简单点的方法,比如取出Combox里面Text输入框的句柄,对它进行Undo操作?