c# - Control input via button -


I made a Tic Tac toe game and I am trying to add some features. I was used to control buttons that took me

  button.enabled = false;  

The problem is that the text gets gray on the button.

So I clicked one button for each button: A1_Click, A2_Click, and so on

This is my code in A1_click, and it's the same for other buttons, only one The thing that changes is "A1", "A2", and so on

  button b = (button) this; If (! A1.Text.Equals ("")) {Messagebox.Show ("Not Valid Input"); }  

I do not get a valid input when I click on a button that I had already clicked, I just want to be able to click on a different button. If I click on the already taken button I do not want to lose my turn

I What do I have read? I think I know what you want. You are looking for a way to force that user to lose a turn, when he clicks the first taken button, and you do not want to set the competent property incorrectly if you want the same I can have some code that can help Firstly, if you are specifying the same click event on several buttons with different functions, then you should try to do something like this:

 < Code> For (int i = 1; i & lt; 10; i ++) {string currentButtonName = "A" + i; Control currentButton = this.Controls.Find (currentButtonName, true) .FirstOrDefault (); CurrentButton.Click + = OnGameButton_Click; }  

What this is doing is searching your form for a control that has a specific name, and since your buttons have similar names, so we can easily search for them. can do. So we can tie them all to a specific task so that instead of changing 9 functions, there is only 1, and you can validate that they all do the same thing. Here is the OnGameButton_Click () event code:

  Private Zero OnGameButton_Click (Object Sender, EventArgs e) {If (Hedgehart Started || shouldLoseTurn) comes back ; // This is the current button pressed user button b = (sender in the form of buttons); If (b.kantance ('a') and b is enabled) {if (! B.Text.Equals ("")) {Messagebox.Show ("is not a valid input! Lost the turn. "); ShouldLoseTurn = True; } And {B. Text = current player; ShouldLoseTurn = false; }}}  

As you can see with some flags, we can monitor the game, and force the button to respond according to the look of the function at the beginning of the function. You can. We must validate that the game is busy, and that the current user has not lost his turn due to pressing the same button. From there we need to revise so much that what you will have so much that if they press the same button twice, then we can modify the shouldLoseTurn flag as necessary.

The second way is to use it only by using the button to disable the button. Enabled Property I know you do not call the button to be grayed out, but if you make your own style guide for the button then how can you do it, though it can be challenging because You'll need to modify the default style template for the button to get it. Here is another question that discusses that


Comments

Popular posts from this blog

apache - 504 Gateway Time-out The server didn't respond in time. How to fix it? -

c# - .net WebSocket: CloseOutputAsync vs CloseAsync -

c++ - How to properly scale qgroupbox title with stylesheet for high resolution display? -