views:

627

answers:

3

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!

+1  A: 

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

M4dRefluX
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
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
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.
M4dRefluX
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
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.
M4dRefluX
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
+1  A: 

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
So I was on the right path with Generics? I wasn't sure.
Refracted Paladin
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
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
+3  A: 

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
Refracted Paladin
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
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
This has nothing to do with MDI. I don't understand your hang-up. Maybe you could start another SO thread.
Hans Passant
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
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
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
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