tags:

views:

820

answers:

6

Hello,

I want to either enable or disable a button from another file,what should I do?

This is the form class declaration:

public partial class Form1 : Form

I tried with

Form.btnName.enabled = false/true

but there's no btnName member.

Thanks in advance!

A: 

You'll have to specify it on your specific instance of Form1.

Ie: If you have something like Form1 myForm = new Form1(...);, then you can do myForm.btnName.Enabled = false;

This will also require that btnName is public. It would be "better" to make a property or accessor to retrieve it than directly provide public access to the, by default, private button field member.

Reed Copsey
A: 

You need to add a public property, or method to set the button.

public void DisableBtnName()
{
  this.btnName.Enabled=false;
}

public Button BtnName
{
    get { return this.btnName;}
}
JoshBerke
+1  A: 

This is because by default, the controls on a form are not public (unlike in VB6 which all controls were exposed publicly).

I believe you can change the visibility accessor in the designer to public, but that's generally a bad idea.

Rather, you should expose a method on your form that will perform the action on the button, and make that method accessible to whatever code you want to call it from. This allows for greater encapsulation and will help prevent side effects from occurring in your code.

casperOne
The reason it's a bad idea: this is generated code. That means your change might be rolled back if the code is re-generated.
Joel Coehoorn
+6  A: 

Simply expose a public method:

public void EnableButton(bool enable)
{
    this.myButton.Enabled = enable;
}
SirDemon
Well,i'm not new,but these basic things is something I usually stuck at.Thanks everyone,especially you and casperOne!
John
@John it might be a typo, but the code you posted was also doing: "Form.btnName" - see my answer
eglasius
+3  A: 

You need to expose the btnName member to other classes by making it public or using a property of sorts. For example add the following code to Form1

public Button ButtonName { get { return btnName; } }

Now you can use form.ButtonName for any instance of Form1

JaredPar
@JaredPar: While it answers the question, it's a horrible suggestion, as it violates encapsulation in a big way.
casperOne
@casperOne, true in general this is a bad idea. But given such a basic question, it's obvious this is a new user so I wanted to give the most straight forward answer possible.
JaredPar
+1  A: 

I really suggest to read more information on how forms fit in .net. You have a couple issues in that sample code "Form.btnName.enabled = false/true"

  • Your form is called Form1, it inherits from Form.
  • Forms are instances, in fact you can have different form instances in an application belonging to the same class.
  • Because of the above, it would not make sense to access Form1.btnName. You have to do it through the specific instance.
  • Form's controls are not public by default, define a method for that.
  • Windows forms projects, usually have a main that runs the form. There you can access the form instance and hand it to something else in the app.
  • The above answers the specific question. Note that there are multiple ways to achieve different scenarios, and what you really want to do might not need the above approach.
eglasius
Actually,none of the answers(except yours) worked,I just tried them.This is the problem:I added the procedure in the form1 unit,but if i make the procedure "static" it wont recognise this nor any buttons on the form.This is my problem,I want to access those buttons outside the main unit.
John
That is because if you make it static, the method doesn't have access to the instance. Please post the more of the code you are trying to use, particularly something that shows more of the context in which you are trying to enable the button.
eglasius