Fail to update interface...(winforms)
class2
{
public void ExecuteAll(int rowStart,int columnStart,int rowEnd,int columnEnd)
{
ChessBoard chess = new ChessBoard();
chess.YourTurn();
counter++;
}
}
public static int counter;
i have got this code, i want to print which players turn it is in the chess board. The counter is a static int, each time it is executed, the counter increases by 1.
YourTurn is a method in the partial class called ChessBoard. it looks like this:
public void YourTurn()
{
if (Class2.counter % 2 == 0)// if counter is an equal number
{
PlayerA.Text = "Black turn";// PlayerA label
PlayerA.Text = "White inactive";
}
else
{
PlayerB.Text = "White turn";// PlayerB label
PlayerB.Text = "Black inactive";
}
}
what doesnt happen, is an update of the labels, each time i make a move. 开发者_Go百科why is that?
even such design wont work, which means , it isnt because of a new instance created each time.
public static string whitesTurn = "White turn";
public static string blacksTurn = "Black turn";
public void YourTurn()
{
if (Class2.counter % 2 == 0)
{
PlayerA.Text = blacksTurn;
PlayerA.Text = "White inactive";
PlayerA.Invalidate();
}
else
{
PlayerB.Text = whitesTurn;
PlayerB.Text = "Black inactive";
PlayerA.Invalidate();
}
}
Debug your code and see if all the functions you've mentioned are called.
See if you are not trying to update values from non-UI thread.
Invalidating the controls would certainly help, but I am not 100% certain that you are updating the same instance of the class.
A better design would be to keep two string values inside your ChessBoard class (maybe PlayerAText
and PlayerBText
) that you would update instead.
Then, assuming class1
is your WinForm, you could add a few pieces:
class2
{
public void ExecuteAll(int rowStart,int columnStart,int rowEnd,int columnEnd)
{
ChessBoard chess = new ChessBoard();
chess.YourTurn();
counter++;
PlayerA.Text = chess.PlayerAText;
PlayerA.Text = chess.PlayerBText;
}
}
public static int counter;
What kind of controls are PlayerA and PlayerB? If they're System.Windows.Forms.Label controls, try calling Invalidate() after you set the text:
PlayerA.Text = "Black turn";
PlayerA.Invalidate();
This will force the label to be redrawn.
You are not effectively changing anything
if (Class2.counter % 2 == 0)// if counter is an equal number
{
PlayerA.Text = "Black turn";// PlayerA label
PlayerA.Text = "White inactive";
}
else
{
PlayerB.Text = "White turn";// PlayerB label
PlayerB.Text = "Black inactive";
}
PlayerA.Text will always be "White inactive" and PlayerB.Text will "Black inactive"
I think the following will solve your problem
if (Class2.counter % 2 == 0)// if counter is an equal number
{
PlayerA.Text = "Black turn";// PlayerA label
PlayerB.Text = "White inactive";
}
else
{
PlayerB.Text = "White turn";// PlayerB label
PlayerA.Text = "Black inactive";
}
You should change labels A and B inside if and else both blocks
精彩评论