简体   繁体   English

C#,容器的火灾事件

[英]C#, fire event of container

I have a MyButton class that inherits from Button. 我有一个继承自Button的MyButton类。 On this class I have placed several other controls (Labels, Progessbar). 在这个课程中,我已经放置了其他几个控件(Labels,Progessbar)。 The problem with this is that the controls on the Button make it impossible to fire the Button.Click or Button.MouseHover event. 这样做的问题是Button上的控件使得无法触发Button.Click或Button.MouseHover事件。 How can I achieve it that the controls on the Button are only displayed but are "event transparent": A click/hover on the label and progessbar is the same as if I clicked/hover directly on the Button (including sender and everything). 我怎样才能实现按钮上的控件只显示但是“事件透明”:标签和progessbar上的单击/悬停与我直接点击/悬停在按钮上(包括发件人和所有内容)相同。 Something like "inheriting the events from the parent". 像“继承父母的事件”之类的东西。

class MyButton : Button
{
    Label foo = new Label();
    ProgressBar bar = new ProgessBar();
}

You should derive from UserControl then have the button as a child control, and bubble up the button child's on click event. 您应该从UserControl派生,然后将按钮作为子控件,并在click事件上冒泡按钮子项。

This link is probably more than what you need, but it's a good starting point. 这个链接可能比你需要的更多,但它是一个很好的起点。

UPDATE As pointed out, you may not be using ASP.NET. 更新如前所述,您可能没有使用ASP.NET。 So here is another post that talks about different custom user controls, specifically what you're after is a Composite Control . 所以是另一篇文章,讨论了不同的自定义用户控件,特别是你所追求的Composite Control This is for Windows Forms. 这适用于Windows窗体。

Write Click event handlers for the label and PB, have them call PerformClick(). 为标签和PB编写Click事件处理程序,让它们调用PerformClick()。

Making controls transparent to the mouse is possible but is ugly to do. 使控件对鼠标透明是可能的,但是很难做到。 You'd have to override WndProc() to catch WM_NCHITTEST and return HTTRANSPARENT. 您必须覆盖WndProc()以捕获WM_NCHITTEST并返回HTTRANSPARENT。 The all-around better solution is to not use controls. 全方位更好的解决方案是不使用控件。 A Label is just TextRenderer.DrawText() in the button's Paint event. Label只是按钮的Paint事件中的TextRenderer.DrawText()。 ProgressBar isn't hard either, e.Graphics.FillRectangle(). ProgressBar也不难,e.Graphics.FillRectangle()。

Having the child controls be real controls in front of the button (either in a class inheriting from Button or from UserControl ) may make it hard to get button-specific events working properly, as you have found. 让子控件成为按钮前面的实际控件(在继承自Button或来自UserControl )可能会使得很难让特定于按钮的事件正常工作,如您所发现的那样。 (Edit: Although it's hard, it's not impossible -- see Hans Passant's answer) (编辑:虽然很难,但并非不可能 - 请参阅Hans Passant的回答)

As a workaround, instead of using child controls, you could custom-paint them onto the button surface, since you don't need most of the functionality of the controls (events, focusing, etc.), just their display. 作为一种解决方法,您可以将它们自定义绘制到按钮表面上,而不是使用子控件,因为您不需要控件的大部分功能(事件,聚焦等),只需要显示它们。

You can do the additional painting in the OnPaint method of your class. 您可以在类的OnPaint方法中执行其他绘制。 Something like: 就像是:

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);
    e.Graphics.DrawString("My fake label", Font, 
                          SystemBrushes.ControlText, new Point(10, 10))
    // draw progressbar
}

To update the display, you would need to trigger a repaint of the Button using Invalidate() . 要更新显示,您需要使用Invalidate()触发Button的重绘。

Take a look at Custom Bitmap Button Using C# on CodeProject for a more complete example. 在CodeProject上使用C#查看自定义位图按钮以获得更完整的示例。

This answer was here a moment ago, but got deleted: 这个答案刚才在这里,但被删除了:

Can you inherit from UserControl instead? 你可以继承UserControl吗? This will allow you to place other controls on the control surface, including your button and subscribe to their events. 这将允许您在控制界面上放置其他控件,包括您的按钮并订阅他们的事件。

If you're using WPF, I guess what you're looking for would be something called Bubbled Events . 如果你正在使用WPF,我猜你正在寻找的东西叫做Bubbled Events It's a feature in WPF by which events are bubbled from a control to its parent (in your case, it would be from your ProgressBar and Label to your button). 它是WPF中的一项功能,通过该功能将事件从控件冒泡到其父级(在您的情况下,它将从您的ProgressBar和Label到您的按钮)。 I found this article on the matter which I think would be of help to you. 我发现这篇关于此事的文章我觉得对你有帮助。

Hope this helps :) 希望这可以帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM