你用的是MDI吗?你可以通过设置和判断控件的某些共有的属性,如tag,
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace WinApp
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.ToolBar toolBar1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent(); //
// TODO: Add any constructor code after InitializeComponent call
//
} /// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.panel1 = new System.Windows.Forms.Panel();
this.toolBar1 = new System.Windows.Forms.ToolBar();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(120, 200);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// panel1
//
this.panel1.Location = new System.Drawing.Point(48, 80);
this.panel1.Name = "panel1";
this.panel1.TabIndex = 1;
//
// toolBar1
//
this.toolBar1.DropDownArrows = true;
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, 42);
this.toolBar1.TabIndex = 2;
this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.toolBar1);
this.Controls.Add(this.panel1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion /// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void mybut_Click(object sender, System.EventArgs e)
{
clicks(((Button)sender).Tag);
}
private void button1_Click(object sender, System.EventArgs e)
{
System.Windows.Forms.ToolBarButton mytoolBut=new ToolBarButton("按钮"+this.toolBar1.Buttons.Count.ToString());
mytoolBut.Tag="But"+this.toolBar1.Buttons.Count.ToString();
this.toolBar1.Buttons.Add(mytoolBut);
System.Windows.Forms.Button but=new Button();
but.Text="Button"+this.panel1.Controls.Count;
but.Tag="But"+this.panel1.Controls.Count;
but.Location=new Point(this.button1.Width*this.panel1.Controls.Count,0);
but.Click+=new EventHandler(mybut_Click);
this.panel1.Controls.Add(but);
}
private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
clicks(e.Button.Tag);
} private void clicks(object Controlvalue)
{
MessageBox.Show("你单击了"+Controlvalue.ToString());
} }
}
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace WinApp
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.ToolBar toolBar1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent(); //
// TODO: Add any constructor code after InitializeComponent call
//
} /// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.panel1 = new System.Windows.Forms.Panel();
this.toolBar1 = new System.Windows.Forms.ToolBar();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(120, 200);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// panel1
//
this.panel1.Location = new System.Drawing.Point(48, 80);
this.panel1.Name = "panel1";
this.panel1.TabIndex = 1;
//
// toolBar1
//
this.toolBar1.DropDownArrows = true;
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, 42);
this.toolBar1.TabIndex = 2;
this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.toolBar1);
this.Controls.Add(this.panel1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion /// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void mybut_Click(object sender, System.EventArgs e)
{
clicks(((Button)sender).Tag);
}
private void button1_Click(object sender, System.EventArgs e)
{
System.Windows.Forms.ToolBarButton mytoolBut=new ToolBarButton("按钮"+this.toolBar1.Buttons.Count.ToString());
mytoolBut.Tag="But"+this.toolBar1.Buttons.Count.ToString();
this.toolBar1.Buttons.Add(mytoolBut);
System.Windows.Forms.Button but=new Button();
but.Text="Button"+this.panel1.Controls.Count;
but.Tag="But"+this.panel1.Controls.Count;
but.Location=new Point(this.button1.Width*this.panel1.Controls.Count,0);
but.Click+=new EventHandler(mybut_Click);
this.panel1.Controls.Add(but);
}
private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
clicks(e.Button.Tag);
} private void clicks(object Controlvalue)
{
MessageBox.Show("你单击了"+Controlvalue.ToString());
} }
}
但要求toolbar上觸發的是主窗口上
正開啟的子窗口上的button1的事件﹗﹗
但實際上new EventHandler(fchild.button1_Click);
無法執行﹐該怎么辦?
private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
Form fchild=this.ActiveMdiChild;
switch (toolBar1.Buttons.IndexOf(e.Button))
{
case 0:
this.toolBar1.Click += new EventHandler(fchild.button1_Click);
break;
case 1:
this.toolBar1.Click += new EventHandler(fchild.button2_Click);
break;
}
}