用于处理在Forms中嵌入的自定义类

我有一个C#中的自定义类文件,我inheritance并部分扩展。 我现在正试图重新考虑它,因为我有足够的专业知识来了解generics (我认为)我可以大大浓缩这个类。

作为一个缺乏经验的独奏开发者,我将非常感谢任何可以提供的任何方向或建设性批评。

别温柔! 我很感激你的时间,祝福你! 我是先发制人地抱歉。

using System.Windows.Forms; using DevExpress.XtraEditors; using DevExpress.XtraTab; namespace psWinForms { public static class WinFormCustomHandling { public static void ShowXFormInControl(Form frm, ref XtraTabPage ctl, FormBorderStyle style) { frm.TopLevel = false; frm.ControlBox = false; frm.Parent = ctl; frm.FormBorderStyle = style; frm.Left = 0; frm.Top = 0; frm.Width = ctl.Width + 4; frm.Dock = DockStyle.Fill; frm.Show(); //IMPORTANT: .Show() fires a form load event frm.BringToFront(); } public static void ShowXFormInControl(Form frm, ref XtraPanel ctl, FormBorderStyle style) { frm.TopLevel = false; frm.ControlBox = false; frm.Parent = ctl; frm.FormBorderStyle = style; frm.Left = 0; frm.Top = 0; frm.Width = ctl.Width + 4; frm.Dock = DockStyle.Fill; frm.Show(); //IMPORTANT: .Show() fires a form load event frm.BringToFront(); } public static void ShowXFormInControl(XtraForm Xfrm, ref XtraTabPage ctl, FormBorderStyle style) { Xfrm.TopLevel = false; Xfrm.ControlBox = false; Xfrm.Parent = ctl; Xfrm.FormBorderStyle = style; Xfrm.Left = 0; Xfrm.Top = 0; Xfrm.Width = ctl.Width + 4; Xfrm.Dock = DockStyle.Fill; Xfrm.Show(); //IMPORTANT: .Show() fires a form load event Xfrm.BringToFront(); } public static void ShowXFormInControl(XtraForm Xfrm, ref XtraPanel ctl, FormBorderStyle style) { Xfrm.TopLevel = false; Xfrm.ControlBox = false; Xfrm.Parent = ctl; Xfrm.FormBorderStyle = style; Xfrm.Left = 0; Xfrm.Top = 0; Xfrm.Width = ctl.Width + 4; Xfrm.Dock = DockStyle.Fill; Xfrm.Show(); //IMPORTANT: .Show() fires a form load event Xfrm.BringToFront(); } public static void ShowFormInControl(Form frm, ref Panel ctl, FormBorderStyle style) { { frm.TopLevel = false; frm.ControlBox = false; frm.Parent = ctl; frm.FormBorderStyle = style; frm.Left = 0; frm.Top = 0; frm.Width = ctl.Width + 4; frm.Dock = DockStyle.Fill; frm.Show(); //IMPORTANT: .Show() fires a form load event frm.BringToFront(); } //.SetBounds(ctl.Left, ctl.Top, ctl.Width, ctl.Height) } public static void ShowFormInControl(Form frm, ref TabPage ctl, FormBorderStyle style) { { frm.TopLevel = false; frm.ControlBox = false; frm.Parent = ctl; frm.FormBorderStyle = style; frm.Left = 0; frm.Top = 0; frm.Width = ctl.Width + 4; frm.Dock = DockStyle.Fill; frm.Show(); //IMPORTANT: .Show() fires a form load event frm.BringToFront(); } } public static void ShowFormInControl(Form frm, Panel ctl, FormBorderStyle style, FormWindowState state) { { frm.TopLevel = false; frm.ControlBox = false; frm.Parent = ctl; frm.FormBorderStyle = style; frm.Left = 0; frm.Top = 0; frm.Width = ctl.Width + 4; frm.WindowState = state; frm.Dock = DockStyle.Fill; frm.Show(); //IMPORTANT: .Show() fires a form load event frm.BringToFront(); } //.SetBounds(ctl.Left, ctl.Top, ctl.Width, ctl.Height) } public static void ShowFormInControl(Form frm, TabPage ctl, FormBorderStyle style, FormWindowState state) { { frm.TopLevel = false; frm.ControlBox = false; frm.Parent = ctl; frm.FormBorderStyle = style; frm.Left = 0; frm.Top = 0; frm.Width = ctl.Width + 4; frm.WindowState = state; frm.Dock = DockStyle.Fill; frm.Show(); //IMPORTANT: .Show() fires a form load event frm.BringToFront(); } } public static void ShowFormInControl(Form frm, ref Panel ctl, FormBorderStyle style, int left, int top) { { frm.TopLevel = false; frm.ControlBox = false; frm.Parent = ctl; frm.FormBorderStyle = style; frm.Left = left; frm.Top = top; frm.Width = ctl.Width + 4; frm.Dock = DockStyle.Fill; frm.Show(); //IMPORTANT: .Show() fires a form load event //.Activate() frm.BringToFront(); } //.SetBounds(ctl.Left, ctl.Top, ctl.Width, ctl.Height) } public static void ShowFormInControl(Form frm, ref TabPage ctl, FormBorderStyle style, int left, int top) { { frm.TopLevel = false; frm.ControlBox = false; frm.Parent = ctl; frm.FormBorderStyle = style; frm.Left = left; frm.Top = top; frm.Width = ctl.Width + 4; frm.Dock = DockStyle.Fill; frm.Show(); //IMPORTANT: .Show() fires a form load event //.Activate() frm.BringToFront(); } } public static void ShowFormInControl(Form frm, Panel ctl, FormBorderStyle style, int left, int top, string title) { { frm.TopLevel = false; frm.ControlBox = false; frm.Parent = ctl; frm.FormBorderStyle = style; frm.Left = left; frm.Top = top; frm.Width = ctl.Width + 4; frm.Text = title; frm.Dock = DockStyle.Fill; frm.Show(); //IMPORTANT: .Show() fires a form load event frm.BringToFront(); } //.SetBounds(ctl.Left, ctl.Top, ctl.Width, ctl.Height) } public static void ShowFormInControl(Form frm, TabPage ctl, FormBorderStyle style, int left, int top, string title) { { frm.TopLevel = false; frm.ControlBox = false; frm.Parent = ctl; frm.FormBorderStyle = style; frm.Left = left; frm.Top = top; frm.Width = ctl.Width + 4; frm.Text = title; frm.Dock = DockStyle.Fill; frm.Show(); //IMPORTANT: .Show() fires a form load event frm.BringToFront(); } } } } 

我有最难以在这里发布代码的时间,结果看起来不错!

您不需要通用语言来执行此操作:

 public static void ShowFormInControl(Control ctl, Form frm) { frm.TopLevel = false; frm.FormBorderStyle = FormBorderStyle.None; // Others rarely make sense frm.Dock = DockStyle.Fill; frm.Visible = true; ctl.Controls.Add(frm); } 

样品用法:

 public Form1() { InitializeComponent(); ShowFormInControl(this.panel1, new Form2()); } 

你试过ReSharper吗? http://www.jetbrains.com/resharper/

我不是100%确定我从那里得到了所有可能的案例,但是通过generics和重载,你可以将其压缩到更容易维护的东西。 这是我的理由:

 using System.Windows.Forms; using DevExpress.XtraEditors; using DevExpress.XtraTab; namespace psWinForms { public static class WinFormCustomHandling { public static void ShowFormInControl (FormType frm, ref ControlType ctl, FormBorderStyle style) where FormType : Form where ControlType : Control { ShowFormInControl(frm, ref ctl, style, 0, 0); } public static void ShowFormInControl (FormType frm, ref ControlType ctl, FormBorderStyle style, FormWindowState? state) where FormType : Form where ControlType : Control { if (state.HasValue) frm.WindowState = state; ShowFormInControl(frm, ref ctl, style, 0, 0); } public static void ShowFormInControl (FormType frm, ref ControlType ctl, FormBorderStyle style, int left, int top) where FormType : Form where ControlType : Control { ShowFormInControl (frm, ref ctl, style, left, top, null); } public static void ShowFormInControl (FormType frm, ref ControlType ctl, FormBorderStyle style, int left, int top, string title) where FormType : Form where ControlType : Control { frm.TopLevel = false; frm.ControlBox = false; frm.Parent = ctl; frm.FormBorderStyle = style; frm.Left = left; frm.Top = top; frm.Width = ctl.Width + 4; if (null != title) frm.Text = title; frm.Dock = DockStyle.Fill; frm.Show(); //IMPORTANT: .Show() fires a form load event frm.BringToFront(); } } }