How do I click a usercontrols child in designer?

2020-03-28 17:04发布

问题:

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 =)

回答1:

@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 return true when you want your control to handle a click, based on the click's location on the screen.



回答2:

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



回答3:

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.