C# 获取单击按钮旁边的按钮
我正在用C#在windows窗体上创建一个跳棋游戏。 我用的是按钮(我想这不是最好的选择) 假设黄色按钮是被点击的按钮,我想禁用除C1和C3之外的所有按钮 我仍然是一个初学者,所以我不确定这是否可能,但我想要的是找到按钮旁边的一个被点击 以下是我的代码,用于检查单击了哪个按钮:C# 获取单击按钮旁边的按钮,c#,visual-studio-2013,C#,Visual Studio 2013,我正在用C#在windows窗体上创建一个跳棋游戏。 我用的是按钮(我想这不是最好的选择) 假设黄色按钮是被点击的按钮,我想禁用除C1和C3之外的所有按钮 我仍然是一个初学者,所以我不确定这是否可能,但我想要的是找到按钮旁边的一个被点击 以下是我的代码,用于检查单击了哪个按钮: private void buttonClick(object sender, EventArgs e) { Button b = (Button)sender; if ((b
private void buttonClick(object sender, EventArgs e)
{
Button b = (Button)sender;
if ((b.Text == "W") && (turn == true))
{
b.BackColor = Color.Yellow;
}
}
试图在屏幕上的某个地方找到按钮是绝对可能的,但不是最简单的方法。最好在创建它们时生成它们并存储到一些方便的结构中:
int xCount = 4;
int yCount = 4;
int buttonSize = 40;
Dictionary<Button, Point> pointByButton = new Dictionary<Button, Point>();
for (int x = 0; x < xCount; ++x)
{
for (int y = 0; y < yCount; ++y)
{
Button newButton = new Button();
newButton.Location = new Point(100 + x * buttonSize, 150 + y * buttonSize);
newButton.Size = new Size(buttonSize, buttonSize);
newButton.Name = "" + (char)(y + 'A') + (x + 1);
newButton.Text = newButton.Name;
this.Controls.Add(newButton);
pointByButton.Add(newButton, new Point(x, y));
newButton.Click += new EventHandler((s, ev) =>
{
Point thisPoint = pointByButton[(Button)s];
foreach (var btn in pointByButton)
{
if (btn.Value.Y - thisPoint.Y != -1 || Math.Abs(btn.Value.X - thisPoint.X) != 1)
{
btn.Key.Enabled = false;
}
}
});
}
}
intxcount=4;
int yCount=4;
int按钮大小=40;
Dictionary pointByButton=新建字典();
对于(整数x=0;x
{
Point thisPoint=pointByButton[(按钮)s];
foreach(pointByButton中的var btn)
{
if(btn.Value.Y-thisPoint.Y!=-1 | | Math.Abs(btn.Value.X-thisPoint.X)!=1)
{
btn.Key.Enabled=false;
}
}
});
}
}
虽然您已经得到了答案(这是正确的答案),但我强烈建议您学习面向对象的编程方法。在OOP
中,将对象可视化为真实世界的对象。简言之,它们具有状态(属性)、表现行为(方法)和对刺激(事件)的反应。您还可以获取一个对象,加入更多属性/方法/事件,并从中分割出一个新对象(继承)
当我学习和编程我的第一盘国际象棋时,我是这样做的:
在我们的项目中添加一个新类,该类表示游戏板上的单元格。我们把它称为CheckersCell。我们让它继承自按钮
(我在原始版本中使用了标签
)。然后我们向其添加一个新属性,以保持其在板上的位置(坐标)
接下来,我们添加另一个类(checkerboard
)来表示游戏板。它包含所有64个单元格(CheckersCell)。SetupBoard()
方法创建单元格并按顺序排列单元格。getAdjuscentCell
方法为您提供与电路板上某个单元相关的周围单元的信息
保存所有内容,编译并运行。单击电路板上的任何单元格(不要单击角行或角列,因为这只是为了演示)。它应该显示一个类似以下内容的消息框:
HTH.您的网格中的按钮是否有命名约定,允许您识别单击的行/列?…然后还开发一个对应于特定位置的字符串(如“btn_02_03”)?非常感谢。我试过了,效果和我想的一样。问题是我只是一个初学者,所以我在理解这段代码时有点困难,尤其是第二部分。但无论如何,谢谢你,这真的很有帮助,我想只要我把我的头绕在它周围,我就可以用它工作了。很抱歉回答得太晚了。我试着使用你写的这段代码,它运行得非常好,问题是我之前已经说过,我是一个完全的初学者,我不理解你在那里写的大部分东西,但我确信这是完成我的任务的正确方式。问题是我的日程排得很紧,所以我有一周的时间来完成这个项目,我不确定我能学到足够的OOP知识来做任何事情,所以我只是为我的标签使用了一个锯齿数组,它工作得很好(至少现在是这样)。谢谢你的意见。
using System.Drawing;
using System.Windows.Forms;
class CheckersCell : Button
{
public Point Position { get; set; }
}
using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.ComponentModel;
[DefaultEvent("CellClick")]
class CheckersBoard : Panel
{
public const int BoardSize = 8; // for standard 8x8 board
public enum Direction { Left, Right, Up, Down, LeftUp, LeftDown, RightUp, RightDown }
public event EventHandler CellClick = new EventHandler((s, e) => { });
public CheckersCell[] Cells { get; set; }
public CheckersBoard()
{
SetupBoard();
}
public CheckersCell GetCellByPosition(int x, int y)
{
foreach (CheckersCell cell in Cells)
{
if (cell.Position.X == x && cell.Position.Y == y) return cell;
}
return null;
}
protected void SetupBoard()
{
// setup the board
Cells = new CheckersCell[BoardSize * BoardSize];
int cellNumber = 0;
for (int x = 0; x < BoardSize; x++)
{
for (int y = 0; y < BoardSize; y++)
{
CheckersCell currentCell = new CheckersCell();
Cells[cellNumber++] = currentCell;
this.Controls.Add(currentCell);
currentCell.Name = string.Concat(this.Name, "Cell", x, "x", y);
currentCell.Position = new Point(x + 1, y + 1);
currentCell.BackColor = ((x + y) % 2 == 0) ? Color.Brown : Color.White;
currentCell.Click += (s, e) => { CellClick(s, e); };
}
}
OnResize(null);
}
public CheckersCell GetAdjescentCell(CheckersCell referenceCell, Direction d)
{
return GetAdjescentCell(referenceCell, d, 1);
}
public CheckersCell GetAdjescentCell(CheckersCell referenceCell, Direction d, int steps)
{
// find the co-ordinates of adjescent cell we want to go to
Point newPosition = referenceCell.Position;
switch (d)
{
case Direction.Left:
newPosition.X -= steps; break;
case Direction.Right:
newPosition.X += steps; break;
case Direction.Up:
newPosition.Y -= steps; break;
case Direction.Down:
newPosition.Y += steps; break;
case Direction.LeftUp:
newPosition.X -= steps; newPosition.Y -= steps; break;
case Direction.RightUp:
newPosition.X += steps; newPosition.Y -= steps; break;
case Direction.LeftDown:
newPosition.X -= steps; newPosition.Y += steps; break;
case Direction.RightDown:
newPosition.X += steps; newPosition.Y += steps; break;
}
foreach (CheckersCell cell in Cells)
{
if (cell.Position == newPosition) return cell;
}
return null;
}
protected override void OnResize(System.EventArgs eventargs)
{
base.OnResize(eventargs);
int cellWidth = this.Width / BoardSize;
int cellHeight = this.Height / BoardSize;
foreach (CheckersCell cell in Cells)
{
cell.Size = new Size(cellWidth, cellHeight);
cell.Location = new Point((cell.Position.X - 1) * cellWidth, (cell.Position.Y - 1) * cellHeight);
}
}
}
private void checkersBoard1_CellClick(object sender, EventArgs e)
{
CheckersCell cell = (CheckersCell)sender;
string msg = "";
msg += "You clicked on Cell : " + cell.Position.ToString() + "\n";
msg += "Left : " + checkersBoard1.GetAdjescentCell(cell, CheckersBoard.Direction.Left).Position.ToString() + "\n";
msg += "Right : " + checkersBoard1.GetAdjescentCell(cell, CheckersBoard.Direction.Right).Position.ToString() + "\n";
msg += "Up : " + checkersBoard1.GetAdjescentCell(cell, CheckersBoard.Direction.Up).Position.ToString() + "\n";
msg += "Down : " + checkersBoard1.GetAdjescentCell(cell, CheckersBoard.Direction.Down).Position.ToString() + "\n";
msg += "UpLeft : " + checkersBoard1.GetAdjescentCell(cell, CheckersBoard.Direction.LeftUp).Position.ToString() + "\n";
msg += "UpRight : " + checkersBoard1.GetAdjescentCell(cell, CheckersBoard.Direction.RightUp).Position.ToString() + "\n";
msg += "DownLeft : " + checkersBoard1.GetAdjescentCell(cell, CheckersBoard.Direction.LeftDown).Position.ToString() + "\n";
msg += "DownRight : " + checkersBoard1.GetAdjescentCell(cell, CheckersBoard.Direction.RightDown).Position.ToString() + "\n";
MessageBox.Show(msg);
}