Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create custom winforms container

I want to create a control in winforms with same behavior as the container controls. I mean: in design mode, when I drop controls in it, it will group then, just like a groupbox.

This control I'm creating contains some other controls AND a GroupBox. All I need is: when a control is droped in design mode over my custom control, I'll just put it inside the nested GroupBox.

But I can't figure out how make my control respond to that kind of action in design mode.

like image 527
Daniel Möller Avatar asked May 24 '13 12:05

Daniel Möller


People also ask

Is winform free?

Windows Forms (WinForms) is a free and open-source graphical (GUI) class library included as a part of Microsoft .

What is WinForms good for?

Windows Forms is a UI framework for building Windows desktop apps. It provides one of the most productive ways to create desktop apps based on the visual designer provided in Visual Studio. Functionality such as drag-and-drop placement of visual controls makes it easy to build desktop apps.

Is it possible to resize a control within the form design window if yes how?

Resize with the designerBy dragging either the right edge, bottom edge, or the corner, you can resize the form. The second way you can resize the form while the designer is open, is through the properties pane. Select the form, then find the Properties pane in Visual Studio. Scroll down to size and expand it.


2 Answers

Maybe this is what you need, I found it at CodeProject a time ago:

Designing Nested Controls:

This article demonstrates how to allow a Control, which is a child of another Control to accept having controls dropped onto it at design time. It is not a large article, there is not much by way of code, and this may not be either the 'official' or best way to do this. It does, however, work, and is stable as far as I have been able to test it.

like image 92
Henry Rodriguez Avatar answered Oct 09 '22 02:10

Henry Rodriguez


You need to add a Designer attribute to your control, and use a type that derives from or is the ParentControlDesigner Class (needs a reference to the System.Design.dll assembly), like this:

[Designer(typeof(MyCustomControlDesigner1))]
public partial class CustomControl1 : Control
{
    public CustomControl1()
    {
        MyBox = new GroupBox();
        InitializeComponent();
        MyBox.Text = "hello world";
        Controls.Add(MyBox);
    }

    public GroupBox MyBox { get; private set; }
}

public class MyCustomControlDesigner1 : ParentControlDesigner
{
    // When a control is parented, tell the parent is the GroupBox, not the control itself
    protected override Control GetParentForComponent(IComponent component)
    {
        CustomControl1 cc = (CustomControl1)Control;
        return cc.MyBox;
    }
}
like image 38
Simon Mourier Avatar answered Oct 09 '22 02:10

Simon Mourier