Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# WindowsForms UserControl's Controls Designer Support

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.

like image 359
user1887120 Avatar asked Oct 21 '22 19:10

user1887120


1 Answers

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");
        }
    }
like image 120
user1887120 Avatar answered Oct 24 '22 11:10

user1887120