如何在设计器中单击用户控件子项?
我在创建一个自定义控件时遇到了一些麻烦。
我拥有的是用户控件中的一个列表框,我需要能够在设计器中单击列表项。这将使它的行为与选项卡控件非常相似。
我没有过多处理用户控件,但我尝试捕获一些覆盖事件,但没有成功。
protected override void OnClick(EventArgs e)
{
if (DesignMode)
{
InvokeOnClick(listBox1, e);
}
base.OnClick(e);
}
我在网上找不到任何东西..关于如何做到这一点有什么想法吗?
预先感谢 =)
I'm having a bit of trouble with creating one of my custom controls.
What I've got is a listbox within a usercontrol, and I need to be able to click on the lists items while still in the designer. This would make it act much like the tabcontrol.
I haven't dealt much with usercontrols but I've tried catching some overide events without success.
protected override void OnClick(EventArgs e)
{
if (DesignMode)
{
InvokeOnClick(listBox1, e);
}
base.OnClick(e);
}
I haven't been able to find anything on the web.. Any ideas on how I can do this?
Thanks in advance =)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
@Bradley:感谢您为我指明了正确的方向,
您将需要编写一个 ControlDesigner 类,然后在 UserControl 的
[Designer( ... )]
属性中使用它。请参阅此处的示例:
http://msdn.microsoft.com/en- us/library/sycctd1z(v=VS.90).aspx
对于实际点击:
http:// msdn.microsoft.com/en-us/library/system.windows.forms.design.controldesigner.gethittest(v=VS.90).aspx
ControlDesigner 有一个
protected bool GetHitTest(Point point)
方法 - 您可以在 ControlDesigner 中实现此方法,并在您希望控件根据单击在屏幕上的位置处理单击时返回true
屏幕。@Bradley: thanks for pointing me in the right direction
You will need to write a ControlDesigner class, then use it in a
[Designer( ... )]
attribute on your UserControl.See the example here:
http://msdn.microsoft.com/en-us/library/sycctd1z(v=VS.90).aspx
For the actual click:
http://msdn.microsoft.com/en-us/library/system.windows.forms.design.controldesigner.gethittest(v=VS.90).aspx
The ControlDesigner has a
protected bool GetHitTest(Point point)
method - you can implement this in your ControlDesigner and returntrue
when you want your control to handle a click, based on the click's location on the screen.我发现这个链接说您需要实现自定义设计器才能获得所需的行为,并解释了如何实现它。
http://social.msdn.microsoft.com/Forums/pl-PL/winforms/thread/0b6ed0cb-907c-4733-b245-ae5d0b0e6606
I found this link that says you need to implement a custom designer to get the desired behavior, and explains how to make it happen.
http://social.msdn.microsoft.com/Forums/pl-PL/winforms/thread/0b6ed0cb-907c-4733-b245-ae5d0b0e6606
您也许可以在自定义控件中捕获 MouseDown 事件并将其转发到内部控件。我不确定 MouseDown 在设计模式下的行为如何。
You may be able to get away with catching the MouseDown event in the custom control and forwarding it on to the inner control. I'm not sure how MouseDown behaves in design mode though.