怎么样在主窗体触发自定义控件的事件

今天需要做个菜单   因为要拿给别人用  所以就做了个自定义控件 

用的时候只要拖拉上去 或者是动态加载上去就可以了

我这里是需要动态加载上去的 所以有点小郁闷的是需要根据点击不同的控件 传回不同的值

根据此值触发不同的事件

起初是准备给用户控件一个属性,让主窗体根据此值执行不同的事件  并且是点击该用户控件

因为我是在主窗体初始化的时候加载的控件  就算取到该值 也不能实现

上网找了下资料 先将解决方法写下,以备后用:

首先在用户控件中声明一个自定义事件

public event EventHandler menu_listView1_SelectedIndexChanged;

其次 在自定义控件的点击事件中触发改事件 

 private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.menu_listView1_SelectedIndexChanged != null)
menu_listView1_SelectedIndexChanged(sender, e);
}

最后 要在主窗体中挂接该事件并实现该事件   在初始化的时候 

this.menu.menu_listView1_SelectedIndexChanged += new EventHandler(menu_listView1_SelectedIndexChanged);

 

实现该事件 

这样的话 只要点击自定义控件中的不同控件 就可以触发不同的事件了

具体的实现代码如下:

//用户自定义控件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace MenuTest.UCcontrol
{
public partial class Menu : UserControl
{
public Menu()
{
InitializeComponent();

}
void CreateFirst()
{
mylistView.Items.Clear();
mylistView.LargeImageList = imageListF;
mylistView.Items.Add("第一子菜单", 0);
mylistView.Items.Add("第二子菜单", 1);
}
void CreateSecond()
{
mylistView.Items.Clear();
mylistView.LargeImageList = imageListS;
mylistView.Items.Add("第一子菜单", 0);
mylistView.Items.Add("第二子菜单", 1);
mylistView.Items.Add("第三子菜单", 2);
mylistView.Items.Add("第四子菜单", 3);
}
void CreateThree()
{
mylistView.Items.Clear();
mylistView.LargeImageList = imageListT;
mylistView.Items.Add("第一子菜单", 0);
mylistView.Items.Add("第二子菜单", 1);
}
/// <summary>
/// 单击不同按钮 显示不同菜单
/// 原理:1 取到点击按钮 2 循环panel中的所有控件,如果是按钮
/// 再判断是否是点击的按钮,则设置其显示的位置
/// 3 别忘了所有控件显示在最前面 BringToFront()函数
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void ButtonClick(object sender, System.EventArgs e)
{
//获取点击的按钮
Button clickedButton = (Button)sender;
// 取到tab索引
int clickedButtonTabIndex = clickedButton.TabIndex;

//button显示的位置 最顶或者最底部
foreach (Control ctl in this.panel1.Controls) //取到panel1中的所有控件
{
if (ctl is Button) //如果是Button
{
Button btn = (Button)ctl;
//如果取到的Button的TabIndex大于点击按钮的TabIndex
//即循环到得按钮不是所点击的按钮
if (btn.TabIndex > clickedButtonTabIndex)
{
//如果循环到的按钮没有显示在底部
if (btn.Dock != DockStyle.Bottom)
{
//则让其显示在底部
btn.Dock = DockStyle.Bottom;
// 显示在最前
btn.BringToFront();
}
}
else //循环到点击按钮
{
//没在顶部就显示在顶部
if (btn.Dock != DockStyle.Top)
{
btn.Dock = DockStyle.Top;
// 显示在最前
btn.BringToFront();
}
}
}
}

switch (clickedButton.Text)
{
case "第一项":
CreateFirst();
break;

case "第二项":
CreateSecond();
break;

case "第三项":
CreateThree();
break;
}
mylistView.BringToFront();
}

//自定义自己的事件
public event EventHandler menu_listView1_SelectedIndexChanged;

//在此触发自己的自定义事件 以备在主窗体中挂接该事件
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.menu_listView1_SelectedIndexChanged != null)
menu_listView1_SelectedIndexChanged(sender, e);
}

}
}
//主窗体

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MenuTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//实例化用户控件
UCcontrol.Menu menu = new UCcontrol.Menu();
string Item;

private void Form1_Load(object sender, EventArgs e)
{


this.Controls.Add(menu);
//挂接用户控件事件
this.menu.menu_listView1_SelectedIndexChanged += new EventHandler(menu_listView1_SelectedIndexChanged);
}


//
#region //测试用户定义控件
void Do()
{
MessageBox.Show("第一子菜单");
}
//
void Do1()
{
MessageBox.Show("第二子菜单");
}

//根据不同的点击执行不同的事件
void Dosomething()
{
switch(Item)
{
case "第一子菜单":
Do();
break;
case "第二子菜单":
Do1();
break;
}
}


/// <summary>
/// 实现用户控件自定义事件 即自己定义的事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void menu_listView1_SelectedIndexChanged(object sender, EventArgs e)
{

ListView listView = (ListView)sender; //取到发送事件的控件
if (listView.SelectedIndices.Count > 0)
{
Item = listView.SelectedItems[0].Text;
Dosomething();
}
}
#endregion
}
}

PS:需要设置控件的相关属性  

//主窗体 系统自动生成 可对照
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.imageListF = new System.Windows.Forms.ImageList(this.components);
this.imageListS = new System.Windows.Forms.ImageList(this.components);
this.imageListT = new System.Windows.Forms.ImageList(this.components);
this.SuspendLayout();
//
// imageListF
//
this.imageListF.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageListF.ImageStream")));
this.imageListF.TransparentColor = System.Drawing.Color.Transparent;
this.imageListF.Images.SetKeyName(0, "11.jpg");
this.imageListF.Images.SetKeyName(1, "12.jpg");
//
// imageListS
//
this.imageListS.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageListS.ImageStream")));
this.imageListS.TransparentColor = System.Drawing.Color.Transparent;
this.imageListS.Images.SetKeyName(0, "21.jpg");
this.imageListS.Images.SetKeyName(1, "22.png");
this.imageListS.Images.SetKeyName(2, "23.jpg");
this.imageListS.Images.SetKeyName(3, "24.png");
//
// imageListT
//
this.imageListT.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageListT.ImageStream")));
this.imageListT.TransparentColor = System.Drawing.Color.Transparent;
this.imageListT.Images.SetKeyName(0, "31.jpg");
this.imageListT.Images.SetKeyName(1, "32.jpg");
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(209, 475);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}





private System.Windows.Forms.ImageList imageListF;
private System.Windows.Forms.ImageList imageListS;
private System.Windows.Forms.ImageList imageListT;
//用户自定义控件  可对照属性  

#region 组件设计器生成的代码

/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Menu));
this.mylistView = new System.Windows.Forms.ListView();
this.BtnThree = new System.Windows.Forms.Button();
this.BtnSecond = new System.Windows.Forms.Button();
this.BtnFirst = new System.Windows.Forms.Button();
this.imageListF = new System.Windows.Forms.ImageList(this.components);
this.imageListS = new System.Windows.Forms.ImageList(this.components);
this.imageListT = new System.Windows.Forms.ImageList(this.components);
this.panel1 = new System.Windows.Forms.Panel();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// mylistView
//
this.mylistView.Dock = System.Windows.Forms.DockStyle.Fill;
this.mylistView.Location = new System.Drawing.Point(0, 0);
this.mylistView.Name = "mylistView";
this.mylistView.Size = new System.Drawing.Size(217, 509);
this.mylistView.TabIndex = 1;
this.mylistView.UseCompatibleStateImageBehavior = false;
this.mylistView.View = System.Windows.Forms.View.Tile;
this.mylistView.SelectedIndexChanged += new System.EventHandler(this.listView1_SelectedIndexChanged);
//
// BtnThree
//
this.BtnThree.Location = new System.Drawing.Point(0, 46);
this.BtnThree.Margin = new System.Windows.Forms.Padding(0);
this.BtnThree.Name = "BtnThree";
this.BtnThree.Size = new System.Drawing.Size(216, 23);
this.BtnThree.TabIndex = 6;
this.BtnThree.Text = "第三项";
this.BtnThree.UseVisualStyleBackColor = true;
this.BtnThree.Click += new System.EventHandler(this.ButtonClick);
//
// BtnSecond
//
this.BtnSecond.Location = new System.Drawing.Point(0, 23);
this.BtnSecond.Margin = new System.Windows.Forms.Padding(0);
this.BtnSecond.Name = "BtnSecond";
this.BtnSecond.Size = new System.Drawing.Size(216, 23);
this.BtnSecond.TabIndex = 5;
this.BtnSecond.Text = "第二项";
this.BtnSecond.UseVisualStyleBackColor = true;
this.BtnSecond.Click += new System.EventHandler(this.ButtonClick);
//
// BtnFirst
//
this.BtnFirst.Location = new System.Drawing.Point(0, 0);
this.BtnFirst.Margin = new System.Windows.Forms.Padding(0);
this.BtnFirst.Name = "BtnFirst";
this.BtnFirst.Size = new System.Drawing.Size(216, 23);
this.BtnFirst.TabIndex = 4;
this.BtnFirst.Text = "第一项";
this.BtnFirst.UseVisualStyleBackColor = true;
this.BtnFirst.Click += new System.EventHandler(this.ButtonClick);
//
// imageListF
//
this.imageListF.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageListF.ImageStream")));
this.imageListF.TransparentColor = System.Drawing.Color.Transparent;
this.imageListF.Images.SetKeyName(0, "11.jpg");
this.imageListF.Images.SetKeyName(1, "12.jpg");
//
// imageListS
//
this.imageListS.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageListS.ImageStream")));
this.imageListS.TransparentColor = System.Drawing.Color.Transparent;
this.imageListS.Images.SetKeyName(0, "21.jpg");
this.imageListS.Images.SetKeyName(1, "22.png");
this.imageListS.Images.SetKeyName(2, "23.jpg");
this.imageListS.Images.SetKeyName(3, "24.png");
//
// imageListT
//
this.imageListT.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageListT.ImageStream")));
this.imageListT.TransparentColor = System.Drawing.Color.Transparent;
this.imageListT.Images.SetKeyName(0, "31.jpg");
this.imageListT.Images.SetKeyName(1, "32.jpg");
//
// panel1
//
this.panel1.Controls.Add(this.BtnFirst);
this.panel1.Controls.Add(this.BtnSecond);
this.panel1.Controls.Add(this.BtnThree);
this.panel1.Controls.Add(this.mylistView);
this.panel1.Location = new System.Drawing.Point(3, 3);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(217, 509);
this.panel1.TabIndex = 7;
//
// Menu
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.panel1);
this.Name = "Menu";
this.Size = new System.Drawing.Size(221, 512);
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.ListView mylistView;
private System.Windows.Forms.Button BtnThree;
private System.Windows.Forms.Button BtnSecond;
private System.Windows.Forms.Button BtnFirst;
private System.Windows.Forms.ImageList imageListF;
private System.Windows.Forms.ImageList imageListS;
private System.Windows.Forms.ImageList imageListT;
private System.Windows.Forms.Panel panel1;




转载于:https://www.cnblogs.com/ShuiMu/articles/2437316.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值