C# WindowsForms UserControl's Controls Designer Support

末鹿安然 提交于 2019-12-07 11:33:49

问题


What I am looking for is the same type of designer support for controls inside a usercontrol. ie - resizing a textbox, moving a label, that are inside a usercontrol after placeing the usercontrol on to a form.

What I've been able to do...

  1. create a usercontrol
  2. use the designer to add controls to the it
  3. create a new window forms app
  4. add the usercontrol to the toolbox
  5. drag and drop the control on the a form

where I am stuck...

  1. edit the usercontrols controls. IE - being able to resize a textbox that is inside the usercontrol using the designer.

I found a similar question on stack that was never answered. So if I am being too vague you can follow this link https://stackoverflow.com/questions/10359772/example-make-constituent-controls-in-a-usercontrol-editable.

Thank you.


回答1:


After reading Nikita's comment I was able to find Microsoft support page on creating a custom designer for controls.

Here's a quote if your interested on how the designed-time support works

The design-time support for components in the .NET Framework, however, is not defined exclusively by a design tool such as Microsoft Visual Studio .NET. Rather, the development environment supports the extension and definition of design-time behavior by classes such as designers that provide design-time support for components. Support for extensible and customizable design mode behavior is an integrated part of the .NET Framework. Tools such as Visual Studio .NET also provide a range of design-time services that designers can use.

This is the webpage if you like to continue reading and view samples from Microsoft

Enhancing Design-Time Support

Everything seems complicated when you just start learning it, heres a working code sample for a UserControl that has a PictureBox and a Label on it. Both controls can be edited during design time, ie. resizing and repositioning, and expose all their events and properties if you click on them.

You will need to add a reference to System.Design, which can only be referenced if you are not targeting ".Net Client Profile." You can change you target profile in Proprieties/Application/TargetFramework.

Add a usercontrol to your project and add a class to handle it's designer. Double click the usercontrol and then add a label and picture box from the toolbar.

Next open that class you create to be it's designer. Add this...

using System.Windows.Forms;
using System.Windows.Forms.Design;

    public override void Initialize(IComponent component)
    {
        base.Initialize(component);

        if (this.Control is MyUserControl)  // replace this with your usercontrol type
        {
            // cast this.Control to you type of usercontrol to get at it's
            // controls easier
            var i = this.Control as MyUserControl; // replace ***

            this.EnableDesignMode(i.label1, "unique_name1");
            this.EnableDesignMode(i.pictureBox1, "unique_name2");
        }
    }


来源:https://stackoverflow.com/questions/14330625/c-sharp-windowsforms-usercontrols-controls-designer-support

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!