Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
277 views
in Technique[技术] by (71.8m points)

c# - Custom Class for dealing with embedding in Forms

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 like generics(I think) I could greatly condense this class.

As an inexperienced solo dev I would greatly appreciate any direction or constructive critism any can provide.

Don't be gentle! I appreciate your time and have a blessed day! I am preemptively sorry for the length.

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

I have the darnedest time posting code here and it turning out looking decent!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...