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);
}
Not sure if it’s the “best”, but this is probably the most efficient. Change the classes to inherit from
UserControlinstead ofForm. 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
Formthat aren’t implemented inUserControl, you’ll have to think of a solution. Some properties (Iconfor 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.