troubleshooting Question

Changing the enable/disable state of a form button from a Class

Avatar of Andy Brown
Andy BrownFlag for United Kingdom of Great Britain and Northern Ireland asked on
C#
2 Comments1 Solution70 ViewsLast Modified:
First off - I'm fairly new to C#.  I have a central form (MainForm), with a panel.  This panel is used to load other forms into it.  Along the bottom of MainForm, there are five buttons - and each one has code (see below) that loads the right form into the panel, changes the text/caption of MainForm, and enables/disables the five buttons depending on which form is selected/loaded (this is the bit I am having trouble with).

        private void JournalButton_Click(object sender, EventArgs e)
        {
            JournalForm var = new JournalForm();
            Nav(var, HomeContentPanel,"Journal");
        }

public void Nav(Form form, Panel panel, string formCaption)
        {
               //Loads form into Panel
            form.TopLevel = false;
            form.Size = panel.Size;
            panel.Controls.Clear();
            panel.Controls.Add(form);
            form.Show();

               //Sets Text/caption of MainForm
               this.Text = formCaption;

               //Enables/disables button
              HomeButton.Enabled = form.Name.ToString() != "HomeForm";
              JournalButton.Enabled = form.Name.ToString() != "JournalForm";
        }

This works perfectly.  However, I wanted to move the Nav method to another class, so that I can set the panel from somewhere other than the five buttons.

Just modifying the code slightly with the MainForm variable at the start - it all works.  Except I cannot enable/disable the five buttons.

public static void NavX(Form frmMain, Form form, Panel panel, string formCaption)
{
       frmMain.Text = formCaption;
       
       frmMain.HomeButton.Enabled = form.Name.ToString() != "HomeForm";
       frmMain.JournalButton.Enabled = form.Name.ToString() != "JournalForm";
}

I get the red squiggles at the start of the HomButton and JournalButton code and can't figure out how to update frmMain.  As said, I am new to c#, so there's probably a better/more correct way of doing this,  But just needed a bit of advice.
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros