C# Accessing controls from an outside class without "public"

Posted by Kurt W on Stack Overflow See other posts from Stack Overflow or by Kurt W
Published on 2010-12-07T02:28:15Z Indexed on 2012/09/11 21:38 UTC
Read the original article Hit count: 373

Filed under:

I know this has been asked before but I believe my situation is a bit different -- or I don't understand the answers given. I have spent about 4 hours working on this solidly and finally realized, I just don't know what to do.

I have 2 Forms (Form1, Settings) and a class I created called Themes.

I have get/set properties that currently work but are all within Form1 and I would like to move as much code related to themeing as I can OUTSIDE of Form1 and into Themes.cs.

Changing Theme: To change the theme, the user opens up the Settings form and selects a theme from the dropdown menu and presses the 'Set' button -- this all works, but now I want to move it into my own class and I can't get the code to compile.

Here is example code that works before moving -- note that this is only 2 different controls I want to modify but there are about 30 total. I am abridging the code:

Form 1:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void btnSettings_Click(object sender, EventArgs e)
    {
        Settings frm = new Settings(this);
        frm.Show();
    }

    private Color txtRSSURLBGProperty;
    private Color txtRSSURLFGProperty;

    public Color TxtRSSURLBGProperty
    {
        get { return txtRSSURLBGProperty; }
        set { txtRSSURL.BackColor = value; }
    }

    public Color TxtRSSURLFGProperty
    {
        get { return txtRSSURLFGProperty; }
        set { txtRSSURL.ForeColor = value; }
    }

Settings Form:

public partial class Settings : Form
{
    public Settings()
    {
        InitializeComponent();
    }

    private Form1 rssReaderMain = null;

    public Settings(Form requestingForm)
    {
        rssReaderMain = requestingForm as Form1;
        InitializeComponent();
    }

    private void button2_Click(object sender, EventArgs args)
    {
        // Appearence settings for DEFAULT THEME

        if (cbThemeSelect.SelectedIndex == 1)
        {
            this.rssReaderMain.TxtRSSURLBGProperty = Color.DarkSeaGreen;
            this.rssReaderMain.TxtRSSURLFGProperty = Color.White;
            [......about 25 more of these....]
        }

The theme class is currently empty. Again, the goal is to move as much code into the themes class (specifically the get/set statements if at all possible!) and hopefully just use a method similar to this within the Settings form once the proper drowndown item is selected: SetTheme(Default);

I hope someone can help, and I hope I explained it right! I have been racking my brain and I need to have this done fairly soon! Much thanks in advance as I'm sure everyone says. I have teamviewer or logmein if someone wants to remote in -- that is just as easy.

I can also send my project as a zip if needed.

Thanks so much,

Kurt

Modified code for review:

Form1 form:

public partial class Form1 : ThemeableForm
{

    public Form1()
    {
        InitializeComponent();
    }

ThemeableForm form:

internal abstract class ThemeableForm : Form
{
    private Color rssLabelBGProperty;
    private Color rssLabelFGProperty;

    public Color RssLabelBGProperty
    {
        get { return rssLabelBGProperty; }
        set { lRSS.BackColor = value; }
    }

    public Color RssLabelFGProperty
    {
        get { return rssLabelFGProperty; }
        set { lRSS.ForeColor = value; }
    }

Settings form:

    public Settings(ThemeableForm requestingForm)
    {
        rssReaderMain = requestingForm as ThemeableForm;
        InitializeComponent();
    }

    private ThemeableForm rssReaderMain = null;

    private void button2_Click(object sender, EventArgs args)
    {

        // Appearence settings for DEFAULT THEME

        if (cbThemeSelect.SelectedIndex == 1)
        {
            this.rssReaderMain.LRSSBGProperty = Color.DarkSeaGreen;
            this.rssReaderMain.LRSSFGProperty = Color.White;
        }

Now the all the controls in my get/set (lRSS in the example code above) error out with does not exist in the current context. I also get the warning:

Warning 1The designer could not be shown for this file because none of the classes within it can be designed. The designer inspected the following classes in the file:

Form1 --- The base class 'RSSReader_BKRF.ThemeableForm' could not be loaded. Ensure the assembly has been referenced and that all projects have been built. 0 0

© Stack Overflow or respective owner

Related posts about c#