1

Is it possible to move the get set methods in another class ?

I'm using an options form which basically reflects all the changes directly in the main form (mostly for changing controls colors,fonts and so on.

The issue starts when you start modifying quite a lot of controls since the main class fills with get set methods, so I was wondering if it's possible to refactor the code to increase the readability of the class a bit, or even better, if it's possible to move the methods in another class somehow (partial classes ?)

Here's a small example of just two controls

public Font TreeFont
{
    get { return customTreeView1.Font; }
    set { customTreeView1.Font = value; }
}

public Font TextBoxFont
{
    get { return customTextBox1.Font; }
    set { customTextBox1.Font = value; }
}

public Font MenusFont
{
    get { return menuStrip1.Font; }
    set
    {
        menuStrip1.Font = value;
        statusStrip1.Font = value;
        contextMenuStripForSnippetContent.Font = value;
        contextMenuStripTreeViewMenu.Font = value;
    }
}

public Color TreeFontForeColor
{
    get { return customTreeView1.ForeColor; }
    set { customTreeView1.ForeColor = value; }
}

public Color TextBoxFontForeColor
{
    get { return customTextBox1.ForeColor; }
    set { customTextBox1.ForeColor = value; }
}

public Color TreeFontBackgroundColor
{
    get { return customTreeView1.BackColor; }
    set { customTreeView1.BackColor = value; }
}

public Color TextBoxFontBackgroundColor
{
    get { return customTextBox1.BackColor; }
    set { customTextBox1.BackColor = value; }
}

So as you can imagine since there are quite a lot of them that need to be changed the lines just pile up.

In addition, would it be a better practice to just return the control and just work on that instead on the other form or do get/set methods considered a better practice ?

Thanks in advance.

denied66
  • 644
  • 7
  • 18
  • You can make a partial class as well and group the properties in different files if that's easier for you. – bart s Jul 31 '12 at 05:43
  • That was my first idea, but when I tried using a partial class there was an issue accessing the controls properties in order to set or get them, except if I'm doing something terribly wrong. – denied66 Jul 31 '12 at 05:49

3 Answers3

1

You can use either C# Regions to make a large code file manageable or you can use Partial Classes to split a large code file into multiple manageable files.

logicnp
  • 5,796
  • 1
  • 28
  • 32
  • This is what I'm doing at the moment, but trying to find specific methods feels a bit harder than it should be, that's why I'm looking into a way of moving or refactoring them in a way that it will be more readable at first glance. Is it possible to give me an example of how a partial class can be used to send control properties, since I always had an issue with accessing the controls in the partial class. – denied66 Jul 31 '12 at 05:53
1

If I understand you correctly - the problem is not the "class" but the "file". So you can simply split the class into two files (just like Visual Studio does with the InitializeComponent method) using Partial Classes.

Make sure the namespace is the same (If you create the file in a sub-folder you'll get a nested namespace. Simply change it.) Also, make sure your class begins with public partial class in both files. And don't have the same property declared in both classes.

Step by step instructions: Right-click on your project in "Solution Explorer". Click "Add". Click "New Item". Click "class". Change class Class1 to public partial class Form1 : Form. Add using System.Windows.Forms; at the top of your file. Add your properties.

ispiro
  • 26,556
  • 38
  • 136
  • 291
  • That's what I'm doing, but the new partial class can't access the already set controls. Am I missing a step somewhere ? – denied66 Jul 31 '12 at 19:38
  • @denied66 Did you do all of the steps in my "Step by step instructions"? If so - please explain what exactly you mean by "can't access the already set controls". Do you mean that when you try to refer to them _in a method_ Intellisense doesn't offer them? – ispiro Aug 03 '12 at 12:08
  • @denied66 Perhaps the controls you mention have been declared in a method (such as the constructor) instead of in the class. If so - they become local variables, and can't be accessed from outside that method. Declare them in the class instead. – ispiro Aug 03 '12 at 12:33
  • Yeap that is exactly what I mean (your first comment). I was hoping that the controls in the main form would be accessible in the partial class. The problem is, as you described, Intellisense doesn't show them when typing their name. Edit: I would swear that an error appeared every single time I tried creating a partial class. This time I tried again just so I can give you an example as to what was happening and everything worked just fine... Marking your answer as accepted since you were the only person that actually replied back till I found a solution. Thanks. – denied66 Aug 03 '12 at 15:06
0

You could use a different kind of function that allows for Page.FindControl("controlNameHere"), and cast it in the right light. This is more for ASP.NET pages, not for Windows form, but you could find the same resolution here Find control by name from Windows Forms controls. This way you could pull the control name and manipulate without ever having to return anything.

Community
  • 1
  • 1
jamesbar2
  • 614
  • 1
  • 9
  • 20
  • This is my current thought, that's why I asked if it's considered a good practice to send controls like this. – denied66 Jul 31 '12 at 05:55
  • Depending on how dynamic your content is, I think it's acceptable and good practice. I also think it's the best way to be dynamic without having to implement your own version using reflection. – jamesbar2 Jul 31 '12 at 06:06