Custom Class for dealing with embedding in Forms

后端 未结 3 915
盖世英雄少女心
盖世英雄少女心 2020-12-11 11:35

I have a custom class file in C# that I inherited and partially extended. I am trying to re factor it now as I have just enough knowhow to know that with something

3条回答
  •  有刺的猬
    2020-12-11 12:17

    I'm not 100% sure that I got all the possible cases from there, but with generics and overloading you can compact this down to something that would be a lot easier to maintain. Here's my go at it:

    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();
            }
        }
    }
    

提交回复
热议问题