Ivan Prodanov
Ivan Prodanov

Reputation: 35532

C#: How to access a button outside the form class

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!

Upvotes: 1

Views: 29004

Answers (7)

User6667769
User6667769

Reputation: 805

In Form1, create a object for external class(add button name in the parameter)

Class1 obj_Class1 = new Class1(btnName);

In Class1 , create a private button

private System.Windows.Forms.Button btnName;

In Class1 Construct

 public Class1(System.Windows.Forms.Button btnName)
        {
            this. btnName = btnName;
        }

then you can access your button like,

btnName.enabled = false/true; 

Upvotes: 0

SirDemon
SirDemon

Reputation: 1758

Simply expose a public method:

public void EnableButton(bool enable)
{
    this.myButton.Enabled = enable;
}

Correction:

public void EnableButton()
{
    this.myButton.Enabled = true;
}

Upvotes: 8

eglasius
eglasius

Reputation: 36035

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.

Upvotes: 3

JaredPar
JaredPar

Reputation: 755587

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

Upvotes: 3

casperOne
casperOne

Reputation: 74560

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.

Upvotes: 1

JoshBerke
JoshBerke

Reputation: 67148

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;}
}

Upvotes: 0

Reed Copsey
Reed Copsey

Reputation: 564931

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.

Upvotes: 0

Related Questions