Hello all,

I have seen a couple threads on this site in ref to enabling and disabling buttons on different forms, however, I still can't get it right.

I have went into the designer.cs and changed the button from private to public modifyer and placed

mainpage.adminpanelbtn.enabled == true;

but without doubt it didn't work.

In my app I have a form that opens and disables 4 buttons on the main page. When the user clicks the close button on the form the 4 buttons then become enabled.

I know how to disable the forms by

private void safetipsbtn_Click(object sender, EventArgs e)
        {
            mainpage mp = new mainpage();
            this.AddOwnedForm(mp);
            
            safetips safe = new safetips();
            safe.Show();
            internetsitebtn.Enabled = false;
            howtousebtn.Enabled = false;
            adminpanelbtn.Enabled = false;
        }

Everytime I try to enalbe them from the new form it throws errors

An object reference is required for the non-static field, method, or property 'Promo_SSP.mainpage.adminpanelbtn'

When I mess around with it and try to fix it on my own I get more errors (which means I don't know what I am doing...lol)

Can anyone help me?

I am new to C# and i am trying to convert from vb.net (which is a lot easier and I know how to fix the problem)

thanks in advanced


P.S. this is a windows form and I am using visual studio 2008 pro

daveofgv

Recommended Answers

I have implemented the desired behavior in your project. Let me know if this isn't what you were asking.

Jump to Post

All 9 Replies

Re: enable/disable from another form 80 80
private void safetipsbtn_Click(object sender, EventArgs e)
        {
            mainpage mp = new mainpage();
            this.AddOwnedForm(mp);
            
            safetips safe = new safetips([B]this[/B]);
            safe.Show();
            internetsitebtn.Enabled = false;
            howtousebtn.Enabled = false;
            adminpanelbtn.Enabled = false;
        }

then modify the safetips constructor

Form MP;//hold main form instance
public safetips(Form MMP)//this is the safetips constructor
{
MP = MMP;
}

now you can reference any public member on the first form from the safetips form by useing the form instance variable "MP" we just created. example.

MP.[I]buttoncontrol[/I].Enabled == True;

do that for all your buttons in the on closing even and you will get your desired effect.

Sorry its so concise. its late and I am tired of typing. Good luck.
Happy coding.

Re: enable/disable from another form 80 80

Thanks for the reply. I tried to do what you said, however, didn't work for me.

VB is so simple compared to c#.

could you show me what you were actually saying in case I didn't do it correctly?
I uploaded a zip file of a basic windows app. (Two forms which one button on form1 is disabled when form2 opens.)

Thank you in advanced.

I hope this isn't asking too much.

daveofgv

Re: enable/disable from another form 80 80

I have implemented the desired behavior in your project. Let me know if this isn't what you were asking.

commented: using a public method was a great idea for this +2
Re: enable/disable from another form 80 80

I am completely lost now.

I implemented it into my project, however, I am still stuck with the same problem.

I uploaded my original project and would like to know if you check it out and see what I am doing wrong......

When you start it up - click safe tips and the top internet sites button should be disabled. This part works. When you close safe tips the internet button stays disabled.

If you don't mind could you please check it out and let me know?

I appreciate all your help. I am planning on getting all buttons on the mainpage disabled when a button is clicked (safetips, admin panel etc....) except for the exit button.

daveofgv

Re: enable/disable from another form 80 80

On the "safetips" form you have the following code:

private void safetips_FormClosed(object sender, FormClosedEventArgs e)
        {
            //Enable the buttons when the form closes
            if (mainpage != null)
                mainpage.EnableButtons(true);
        }

That is an event. You need to click on the form, go to events, select the "FormClosed" event and wire it up to that method. Right now nothing is hooked on the closed event code.

Re: enable/disable from another form 80 80

I opened safesite form and clicked formclosed in the upper right side drop down. I placed me back to where the code was:

private void safetips_FormClosed(object sender, FormClosedEventArgs e)
        {
            //Enable the buttons when the form closes
            if (mainpage != null)
                mainpage.EnableButtons(true);
        }
    }

}

The events only gives me this option if I type it in.

private void safetips_FormClosed(object sender, FormClosedEventArgs e)

When I delete it and go back to the events in the upper right corner (visual studio 2008 pro) it's not there????

daveofgv

Re: enable/disable from another form 80 80

Here, I have made the modification for you.

Re: enable/disable from another form 80 80

Thank you

Is there a reason why I couldn't connect it to the FormClosed event.
They look the same, however, I had to type it instead of choosing it from a list of events.
I know in visual basic the events have a long list, but in C# it only places what has been used and not a choice to choose from.

Maybe mine isn't set up right or something....

Thank you again for all your help

daveofv

Re: enable/disable from another form 80 80

I'm not sure what the issue was -- and the behavior depends on your version of visual studio.

Please mark this thread as solved if I have answered your question. Good luck in future developments!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.