[英]How to add UNDO and REDO buttons to TableLayout panel in C#
I'm making game where you put 8 queens on chess board so no queen attack any other.我正在制作游戏,您将 8 个皇后放在棋盘上,这样皇后就不会攻击任何其他皇后。 It is for my homework.
这是为了我的家庭作业。
I used TableLayoutPanel
8x8 with PictureBox
in every cell.我在每个单元格中使用了
TableLayoutPanel
8x8 和PictureBox
。 They change color or picture on click and show user which one cell is available.它们在单击时更改颜色或图片,并向用户显示哪个单元格可用。 But I wanted to implement Undo, so user can go step back if he does not like where he positioned his queen.
但是我想实现撤消,所以如果用户不喜欢他放置皇后的位置,他可以后退一步。 I googled a lot, but every single undo-redo solution is about integers and strings, and I do not have idea how to use it for my table panel.
我搜索了很多,但每个撤消重做解决方案都是关于整数和字符串的,我不知道如何将它用于我的表格面板。 Do you guys have any idea how to do that?
你们知道怎么做吗? I tried to make another copy-panel and copy original one into it, before changing color happens, and if I want to undo, to actually show copy-panel but for some reason it does not work.
在更改颜色之前,我尝试制作另一个复制面板并将原始面板复制到其中,如果我想撤消,则实际显示复制面板,但由于某种原因它不起作用。
If it is already answered somewhere how to make undo for TableLayoutPanel
can you please guide me to that guide?如果已经在某个地方回答了如何撤消
TableLayoutPanel
,您能否指导我查看该指南?
private void pictureBox1_Click(object sender, EventArgs e)
{
PictureBox picture = (PictureBox)sender;
//checking if cell is empty
if(picture.BackColor==Color.White)
{
picture.Image = Properties.Resources.kraljica;
int column = tableLayoutPanel1.GetCellPosition(picture).Column;
int row = tableLayoutPanel1.GetCellPosition(picture).Row;
int j = column;
int i = row;
//changing color of down right diagonal according to current position of queen
while (i <= 7 && j <= 7)
{
tableLayoutPanel1.GetControlFromPosition(j, i).BackColor = Color.DarkRed;
i++;
j++;
}
Welcome to the StackOverFlow.欢迎来到 StackOverFlow。
Normally, we'd ask you to provide some code to help us out in solving your problem, but I'll try and give you some pointers without.通常,我们会要求您提供一些代码来帮助我们解决您的问题,但我会尝试在没有的情况下给您一些指示。
You need to keep a history of past moves, in this case this would be a history of cell locations for where a queen started to where it ended up.你需要保留过去移动的历史,在这种情况下,这将是一个蜂后开始到它结束的单元位置的历史。
I suspect that you must be using an OnClick event or something similar to determine which queen is selected and where it is moved to - if not then the logic would still be the same.我怀疑您必须使用 OnClick 事件或类似的事件来确定选择了哪个女王以及将其移动到哪里 - 如果不是,那么逻辑仍然是相同的。
You want to use the GetRow and GetColumn methods of the TableLayoutPanel control and store the result in a List control eg您想使用 TableLayoutPanel 控件的 GetRow 和 GetColumn 方法并将结果存储在 List 控件中,例如
public class Move
{
int startRow;
int startColumn;
int endRow;
int endColumn;
}
then in your code have properties然后在你的代码中有属性
public List<Move> ListOfMoves = new List<Move>();
public SelectedMoveIndex {get;set;}
Everytime a new move is made add a new Move instance with populated values to the List每次进行新的移动时,将具有填充值的新移动实例添加到列表中
eg例如
ListOfMoves.Add(new Move(){startRow = x1, startColumn=y1, endRow=x2, endColumn=y2};
SelectedMoveIndex = ListOfMoves.Count -1;
where x1, y1, x2, y2 have values taken from GetRow()
& GetColumn()
on move start and move end.其中 x1, y1, x2, y2 的值取自移动开始和移动结束时的
GetRow()
和GetColumn()
。
To undo a move use the Move
values stored at SelectedMoveIndex
and play the backwards eg endRow
and endColumn
become the start values then decrement the value of SelectedMoveIndex
.要撤消移动,请使用存储在
SelectedMoveIndex
中的Move
值并向后播放,例如endRow
和endColumn
成为起始值,然后递减SelectedMoveIndex
的值。 Redo would use the values as stored at SelectedMoveIndex
and then increment SelectedMoveIndex
afterwards.重做将使用存储在
SelectedMoveIndex
中的值,然后增加SelectedMoveIndex
。
If you make a new move that isn't stored then you'll need to remove elements after SelectedMoveIndex
before adding a new one.如果您进行未存储的新移动,则需要在
SelectedMoveIndex
之后删除元素,然后再添加新移动。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.