Refracted Paladin
Refracted Paladin

Reputation: 12216

move from form to user control

I have a bunch of Forms that I embed in tabpages(some are embedded two and three layers deep) that I now suspect are giving me trouble. I have been told that User Control's are the better approach.

I have about 40 forms that I embedd that would need to be moved and not a lot of time to do it so any help is greatly appreciated.

EDIT 1

This is how I embed forms:

        public static void ShowFormInContainerControl(Control ctl, Form frm)
    {
        frm.TopLevel = false;
        frm.FormBorderStyle = FormBorderStyle.None;
        frm.Dock = DockStyle.Fill;
        frm.Visible = true;
        ctl.Controls.Add(frm);
    }

        public static void DockControl(this Control control, UserControl userControl)
    {
        userControl.Dock = DockStyle.Fill;
        control.Controls.Clear();
        control.Controls.Add(userControl);
    }

Upvotes: 1

Views: 1363

Answers (1)

lc.
lc.

Reputation: 116468

Not sure if it's the "best", but this is probably the most efficient. Change the classes to inherit from UserControl instead of Form. Then fix the compiler errors if/when you get any (see NOTE 2 below).

NOTE 1: If you're not using version control, start using it before doing something drastic like this. You'll want to be able to go back if something goes too far south.

NOTE 2: If you use any particular events or properties of Form that aren't implemented in UserControl, you'll have to think of a solution. Some properties (Icon for example) you can safely just ignore (= delete the line from the designer file).

NOTE 3: If you use the forms as an actual form somewhere, you'll want to also have a form that uses the newly created UserControl. You're most likely to get in trouble here with naming, so keep a sharp eye.

Upvotes: 8

Related Questions