Hashey100
Hashey100

Reputation: 984

I want to disable a button from clicking

Bassicly im creating a program that allows the user to enter values and if the values exceed a certain amount then disable a button that was on a different form. But im unsure how to access its buttons control. I thought it would be something like this? Thanx

 if(value>120)
 {
  Form3 form3 = new Form3();
  Button.Disable();
  this.close();
 }

Upvotes: 1

Views: 425

Answers (4)

Cristian Lupascu
Cristian Lupascu

Reputation: 40516

You can disable the button like this:

otherForm.Button.Enabled = false;

To be able to disable this button from another context (form), you need to declare it as public. You can do this as follows:

  • selecting the button in design view
  • in the properties window set Modifiers to Public

Then you can show the form with the disabled button, like so:

var newForm = new Form3();
newForm.Button.Enabled = false;
newForm.Show();

Upvotes: 1

C.Evenhuis
C.Evenhuis

Reputation: 26436

Your request is to disable a button that was on another form - from reading that I assume the form already exists. By creating a new instance:

Form3 form3 = new Form3();

You're creating a new instance of Form3 so you'll never disable a button on a form that was already visible.

You'll have to make the current form aware of the instance of Form3 to be able to change anything there. Here are a few ways to make them interact:

  • Store these values in a separate object and make your forms react on changes through events for example
  • Provide a reference to the instance of Form3 upon creating or Show()ing "this" form
  • Keep a reference to each created form in a static class

Also keep in mind having multiple related forms active at the same time may confuse your end-user.

Upvotes: 3

Matt Burland
Matt Burland

Reputation: 45135

You need a reference to an instance of Form3. You are creating a new instance of Form3 which is probably not what you wanted. Then your Form3 needs to expose the button you are interested in as a public property so that you can access it from outside of the class. Then you should be able to set the Disabled property to true.

Upvotes: 1

Nudier Mena
Nudier Mena

Reputation: 3274

I guess you have to do something like this.

update

 if(value>120)
 {
 Form3 form3 = new Form3();
 form3.Button.Enabled = false;
 this.close();
 }

update

Upvotes: 0

Related Questions