2

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!

casperOne
  • 73,706
  • 19
  • 184
  • 253
Refracted Paladin
  • 12,096
  • 33
  • 123
  • 233

3 Answers3

3

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());
}
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • 2 questions: What does this do --> ctl.Controls.Add(frm); & doesn't this leave me in the same place as I am now? Having to overload the method for each difference? Is that unavoidable? Thank you! – Refracted Paladin Feb 13 '09 at 14:24
  • It makes the form a child of the control, same thing as setting the Parent property. You are ahead with this code, it will work for any container control. Try it. – Hans Passant Feb 13 '09 at 16:09
  • Okay, you would know better then I. I just like to come away with some level of understanding. Is this similar to using the MDI settings? If so does that have any side effects? Thanks! – Refracted Paladin Feb 13 '09 at 18:27
  • This has nothing to do with MDI. I don't understand your hang-up. Maybe you could start another SO thread. – Hans Passant Feb 13 '09 at 21:12
  • I'll mess around with it and then maybe it will make sense. If I am still confused I'll start another thread. Thanks for the help! – Refracted Paladin Feb 14 '09 at 04:51
  • Is it acceptable for me to start a thread on this simply because I don't fully understand it? It works very well. Whether the control is a standard or DevExpress control. I would really like to understand this a little better. New thread or start Googling? – Refracted Paladin Feb 18 '09 at 15:56
  • I dunno, you have to be pretty brave these days to start a thread at SO. I'm guessing your hang-up is not seeing the class inheritance. That will give the down-voters a field day. It's up to you. – Hans Passant Feb 18 '09 at 16:39
  • Dully noted, thanks for everything. I agree as I look at this it is making more sense but unfortunately there are parts that are still "magic";-) – Refracted Paladin Feb 18 '09 at 17:19
1

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

Steven DeWitt
  • 1,829
  • 1
  • 21
  • 23
  • I have heard great things about it. Is it something I should try and sell to the "top?" Beneficial for a IT dept that has exactly 1 Applications person? $200 might be a tough sell. Is it that great? Thanks! – Refracted Paladin Feb 12 '09 at 20:41
  • I have been using both Resharper and CodeRush together for over a year now and they have easily paid for themselves in time savings. Depending on the financial status of your company, it may be a tough sell though... – scwagner Feb 12 '09 at 20:43
  • I think every C# or .NET programmer should have it in their toolbox, it gives tips on refactoring your code, and it greatly enhances Intellisense as well. – Steven DeWitt Feb 12 '09 at 20:43
  • Is it fair to say that as a solo dev this could "kinda" act like a Code Review/Lead Dev/Mentor? Maybe coupled with StyleCop? I need angles to sell this! – Refracted Paladin Feb 12 '09 at 20:47
  • Sure, although I'm not familiar with the corporate angle of programming since I just program as a hobby in my home. I just know it's a great tool, hands down. – Steven DeWitt Feb 12 '09 at 20:54
  • If I remember correctly StyleCop is not configurable at all, so you're stuck with whatever rules are programmed into it. Resharper, though, is great at finding dead code, unused variables, simple readability enhancements, and their refactoring support is amazing. – scwagner Feb 12 '09 at 21:05
1

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();
        }
    }
}
scwagner
  • 3,975
  • 21
  • 16
  • Yes, any time that you have the 2 functions do the same content operations with slightly different parameter lists, you probably have a good candidate for generics. – scwagner Feb 12 '09 at 21:06
  • Well time to read up on Generics then as I seem to find this a lot in the code base I inherited! Thanks +1! – Refracted Paladin Feb 12 '09 at 21:20