Reputation: 31221
I know this question has been asked before or similar questions asked but none of the solutions has yet fixed my problem.
I have a winform that has a button named hitButton
. All I need to do is to disable this button from another class (i.e. not the form class).
My form class is:
public partial class BlackJackTable : Form
{
}
My separate class is:
class GameWon
{
}
I have tried exposing a public method in BlackJackTable
like this:
public void DisableButtons()
{
hitButton.Enabled = false;
}
and accessing from GameWon
constructor (it doesn't seem to matter where in the class I access it from though) like this:
public BlackJackTable blackJackTable = new BlackJackTable();
public GameWon()
{
blackJackTable.DisableButtons();
}
I have tried using a variable in GameWon
:
public Button hit;
and assigning from blackJackTable:
GameWon gameWon = new GameWon();
public void Assign()
{
gameWon.hit = this.hitButton; // or just = hitButton;
}
and disabling from GameWon
:
hit.Enabled = false;
I have tried a couple of other things but none of them work. I'm guessing I'm missing something obvious as none of the methods I've tried have worked.
Any thoughts appreciated!
Update
I have managed to do the latter method with textboxes and pictureboxes, what's the difference with a button?
Upvotes: 0
Views: 2312
Reputation: 5403
You are doing a lot of work in your constructors. This is often bad practice, particularly for forms. You seem to be modifying properties of objects on a form before the form is shown - I'm not surprised this produces unpredictable behaviour. If I were to guess, I'd imagine that the button state is being reset by form initialisation after your code has run. Forms are quite protective of their own lifecycle requirements, and I figure you must have run into an idiosyncrasy to do with how the form prepares itself for display.
I have to point out that you've presented quite a strange architecture here. I'm struggling to understand why you'd choose to arrange your application in this way. I think it would be preferable to communicate with the form and the game class from a controller class, possibly using events raised in the controller to allow the form to modify itself. Making a form modify its own state while it is visible on screen should always work.
Upvotes: 0
Reputation: 31221
I ended up fixing this using the latter method, but making the Button variable static.
public static Button hit;
It must have been getting class intances mixed up. Thanks for your ideas.
Upvotes: 0
Reputation: 726539
The reason it does not work is that you are calling DisableButtons
on a wrong instance of BlackJackTable
. Your GameWon
class creates its own, invisible, BlackJackTable
object, stores it in a BlackJackTable blackJackTable
variable, and disables its buttons; the form object that is visible to you does not get modified.
To fix this, you should pass the instance of your BlackJackTable
form to the constructor of GameWon
, and set it to a variable:
private readonly BlackJackTable blackJackTable;
public GameWon(BlackJackTable blackJackTable) {
this.blackJackTable = blackJackTable;
}
The code that creates an instance of GameWon
class should pass the form as a parameter to the constructor:
GameWon gameWon = new GameWon(myBjTable);
Upvotes: 3
Reputation: 300
You could return a value to the original class then disable the button: Take a look at: How to call properties inside of a script runat at server
Upvotes: 0