自定义控件事件 [英] Custom control event

查看:70
本文介绍了自定义控件事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义控件。

这个自定义控件包含一个面板。

我想在自定义控件的属性窗口中公开Pannel click事件。

我如何在自定义控件中执行此操作。

I have a custom control.
This custom control contains one pannel.
I want to expose Pannel click event in property window of custom control.
How can i do that in custom control.

推荐答案

最容易用代码来说明这一点:
Easiest to illustrate this with code:
using System;
using System.Windows.Forms;

namespace YourNameSpace
{
    public partial class UserControl1 : UserControl
    {
        public event EventHandler Panel_Click;

        public Panel Panel1 { get; private set; }

        public UserControl1()
        {
            InitializeComponent();
            Panel1 = panel1;
        }

        private void UserControl1_Load(object sender, EventArgs e)
        {
            if (Panel_Click != null)
            {
                panel1.Click += Panel_Click;
            }
        }
    }
}

自定义控件或组件的属性,其访问修饰符设置为public将显示在Visual Studio中设计时属性编辑器(PropertyGrid)。在这种情况下,公开的Property是UserControl本身内的WinForms本机Control(Panel)。我想你已经知道了,你真正的问题是如何在UserControl中公开一个控件的事件。



上面显示的解决方案是在UserControl Load EventHandler代码中创建一个公共事件,类型'EventHandler的vanilla事件,然后连线,如果它不是null。



这种技术对我来说似乎不太优雅,因为:



1.点击事件仅作为所有之一列出UserControl实例的PropertyGrid Events选项卡本身。



2.如果您碰巧在UserControl的设计时编辑器中定义了Click EventHandler,并且通过选择在WinForm属性编辑器中为UserControl的选址实例公开的UserControl来定义另一个:你有两个有效的Click EventHandler,它们都将被执行:这是一件好事吗? imho,更可能是一个混乱的来源?



理想情况下,你有像WinForms ToolStripContainer Control所展示的那样的设计时行为,其内部组件是独立的窗口元素(ContentPanel等)不仅显示在属性视图中,而如果选中,则在切换到事件选项卡时正确显示其事件:whoa!



如果你想那个,imho,你是在艰难的旅程,我的猜测是你需要深入探索TypeDescirptors ,自定义类型编辑器,以及属性(元数据)的各个方面。几年前我试图在设计时PropertyGrid中尝试暴露UserControls中的控件事件,并且找不到办法:这并不意味着你不能!



并且,请注意,Microsoft本身并未实现类似于ToolStripContainer中的行为,因为其他本机WinForms控件/组件包含窗口元素:ContextMenuStrip将提供单独的顶级编辑 - 下拉式PropertyGrid设计时选择器中的-tab条目,对于Form:对于你创建的每个ToolStripMenuItem,这都非常混乱!



说完了这一切,如果CP中的一个更亮的灯泡出现,我会非常高兴,并且告诉我我绝对错了,并且在设计时WinForm属性网格中将控件事件暴露在UserControl中,就​​像ToolStripContainer那样,简单 ! :)



fyi:从创建自己的自定义PropertyGrid以在运行时使用的角度来看,关于PropertyGrid的CP上有很多很好的文章。 $>


祝你好运,Bill

Properties of a custom Control, or Component, that have their access modifier set to 'public will appear in Visual Studio's design-time Property Editor (PropertyGrid). In this case the Property that is exposed is a WinForms native Control (a Panel) within the UserControl itself. I think you know that, already, and your real issue is to how to expose an Event of a Control inside a UserControl.

The "solution" shown above is to create a public event, a "vanilla" event of Type 'EventHandler, and then wire-it-up, in the UserControl Load EventHandler code: if it's not null.

This technique really seems inelegant to me because:

1. the Click Event is only exposed as one of all the many Events listed in the PropertyGrid Events Tab for the UserControl instance, itself.

2. if you happen to define a Click EventHandler both in the design-time editor of the UserControl, and also define another one by selecting the UserControl as exposed in the WinForm property editor for your sited instance of the UserControl: you have two valid Click EventHandlers which will both be executed: is that a good thing ? imho, more likely a source of confusion ?

Ideally, you'd have design-time behavior like that exhibited by the WinForms ToolStripContainer Control, where its internal components with separate window elements (ContentPanel, etc.) not only show up in the Properties view, but if selected, correctly expose their Events when you switch to the Event Tab: whoa !

If you want that, imho, you are in for a rough ride, and my guess is you are going to need to explore deeply TypeDescirptors, custom Type Editors, and various aspects of Attributes (meta-data). I looked at trying to expose Events of Controls in UserControls in the design-time PropertyGrid a few years ago, and could not find a way to do it: that doesn't mean that you can't !

And, note that Microsoft itself doesn't implement behavior like that found in ToolStripContainer for other native WinForms Controls/Component that contain "windowed elements:" a ContextMenuStrip will put up a separate top-level edit-tab entry in the drop-down PropertyGrid design-time selector, for the Form: for each ToolStripMenuItem you created, which is quite messy !

Having said all this, I'd be so delighted if one of CP's brighter-bulbs came along, and told me I was absolutely wrong, and that exposing Events of Controls inside a UserControl in the design-time WinForm Property Grid, in the way that ToolStripContainer does, is easy ! :)

fyi: there are lots of good articles on CP about the PropertyGrid written from the viewpoint of creating your own custom PropertyGrid to use at run-time.

good luck, Bill


为此目的使用Meta属性。 [可浏览(真实)]

例如。

Use Meta attributes for this purpose. [Browsable(true)]
eg.
[Browsable(true)]
public string property
{
get;set;
}



我希望这有帮助


I hope this helps


这篇关于自定义控件事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆