Mocco
Mocco

Reputation: 1223

Could user control be stand alone as a form?

I cannot find a way how to do that. I assume it needs to be child of different control?

Upvotes: 0

Views: 134

Answers (2)

TcKs
TcKs

Reputation: 26632

@Justin is right. If you want, you can create extension methods:

new MyUserControl().ShowInForm();
new MyUserControl().ShowInForm((frm)=>{
    frm.Text = "My user control";
});

public static class MyExtensions {
    public static void ShowInForm(this Control ctl) {
        ShowInForm(ctl, (Action<Form>)null);
    }
    public static void ShowInForm(this Control ctl, Action<Form> initFormAction) {
        // removes control from previous container
        ctl.Parent = null;

        var frm = new Form();
        frm.ClientSize = ctl.Size;
        ctl.Dock = DockStyle.Fill;
        frm.Controls.Add(ctl);
        frm.Show();
    }
}

You can also create extension methods for show in dialog windows and whatever else variants.

Upvotes: 1

Justin
Justin

Reputation: 6711

You could just add it to an empty form, which is simple if you use the designer. Assuming you don't want to use the designer, you can still do this using code. Something like:

MyUserControl control = new MyUserControl();

Form containerForm = new Form();
containerForm.ClientSize = control.Size;
containerForm.Controls.Add(control);

control.Dock = DockStyle.Fill;
containerForm.Show();

Upvotes: 2

Related Questions