Reputation: 984
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
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:
Then you can show the form with the disabled button, like so:
var newForm = new Form3();
newForm.Button.Enabled = false;
newForm.Show();
Upvotes: 1
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:
Form3
upon creating or Show()
ing "this" formAlso keep in mind having multiple related forms active at the same time may confuse your end-user.
Upvotes: 3
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
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