Question Reset button color from another form.

AlexJames

Well-known member
Joined
Mar 20, 2020
Messages
65
Programming Experience
10+
Hi All

I'm building a C# winForms app, I have my main form with with some buttons as a menu. I have a docked panel which i show my forms in with the code below.

C#:
Form frm = new FrmSetupExchanges
            {
                TopLevel = false,
                ControlBox = false,
                Dock = DockStyle.Fill
            };

PnlContainer.Controls.Add(frm);

When the user clicks on the button to show the above form I change the back color of the button so that the user can visually see which menu item he clicked. The problem i'm having is when i close the form i want to reset the button color back to normal on my main form but i cant figure out how to do it. All my coding experience has been in Visual basic, so would simply do

Visual Basic:
FrmMain.Cmdsetup.Backcolor = Color.Red

Me.Close

How do i achieve this in C# ?
many thanks in advance
AJ
 
Last edited by a moderator:
Here, you are declaring a local variable and assign the new form to it:
C#:
case true:
    Form frmSetup = new FrmSetupExchanges
                        {
                            TopLevel = false,
                            ControlBox = false,
                            Dock = DockStyle.Fill
                        };
    PnlContainer.Controls.Add(frmSetup);
    frmSetup.Show();
    break;
The whole point was that you don't assign the form to a local variable so that you can access that same form later and close it. You're supposed to be assigning the form to the member variable:
C#:
case true:
    frmSetup = new FrmSetupExchanges
                   {
                       TopLevel = false,
                       ControlBox = false,
                       Dock = DockStyle.Fill
                   };
    PnlContainer.Controls.Add(frmSetup);
    frmSetup.Show();
    break;
 
Hahaha, Oh my gosh !! I knew i was going to kick myself. One thing is for sure, I will never forget how to do this.

Thanks again for your help Jm, always appreciated.
 
Back
Top Bottom