views:

315

answers:

1

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.

  • Now I am wondering how I canaccomplish this as quick as possible.

  • Is it as simple as copy and paste?

  • Has anyone ever done something like this?

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);
    }
+8  A: 

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.

lc
Nice idea, but DON'T create a copy as a form. If you want to use it as a form, create a new form that embeds the UserControl.
MiffTheFox
@MiffTheFox: Good point. It's a little late...
lc
Can you clarify, What's to late? See my EDIT for how I embed forms and now user controls.
Refracted Paladin
@Mr_Mom: The time. It's 2 in the morning here. I edited my post to reflect MiffTheFox's suggestion.
lc
LOL, sorry, I thought you meant something else!
Refracted Paladin