Custom Class for dealing with embedding in Forms

后端 未结 3 916
盖世英雄少女心
盖世英雄少女心 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

    You don't need generic to do this:

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

    Sample usage:

    public Form1() {
      InitializeComponent();
      ShowFormInControl(this.panel1, new Form2());
    }
    
    0 讨论(0)
  • 2020-12-11 12:17

    Have you tried ReSharper? http://www.jetbrains.com/resharper/

    0 讨论(0)
  • 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, ControlType> (FormType frm, ref ControlType ctl, FormBorderStyle style)
              where FormType : Form
              where ControlType : Control
            {
                 ShowFormInControl<ControlType>(frm, ref ctl, style, 0, 0);
            }
    
            public static void ShowFormInControl<FormType, ControlType> (FormType frm, ref ControlType ctl, FormBorderStyle style, FormWindowState? state)
              where FormType : Form
              where ControlType : Control
            {
                if (state.HasValue)
                    frm.WindowState = state;
                ShowFormInControl<ControlType>(frm, ref ctl, style, 0, 0);
            }
    
            public static void ShowFormInControl<FormType, ControlType> (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, ControlType> (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();
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题