C# C-如何创建具有不同控件的两个单独按钮阵列,而不使它们与另一个产生问题?
我目前正在尝试在c windows窗体上开发一种战列舰。 这是我试图使用的代码。。我遇到的麻烦是如何创建第二组按钮,在另一组按钮后面再创建一组10x10,带有两组控件,以便我可以在这两组控件之间切换 我有像人工智能和自动设置的一切,我只需要有2个按钮控制。我希望有人能帮我解决这个问题!非常感谢C# C-如何创建具有不同控件的两个单独按钮阵列,而不使它们与另一个产生问题?,c#,button,grid,C#,Button,Grid,我目前正在尝试在c windows窗体上开发一种战列舰。 这是我试图使用的代码。。我遇到的麻烦是如何创建第二组按钮,在另一组按钮后面再创建一组10x10,带有两组控件,以便我可以在这两组控件之间切换 我有像人工智能和自动设置的一切,我只需要有2个按钮控制。我希望有人能帮我解决这个问题!非常感谢 private List<List<Button>> grid = new List<List<Button>>(); public UserForm()
private List<List<Button>> grid = new List<List<Button>>();
public UserForm()
{
InitializeComponent();
byte numRows = 10;
byte numCols = 10;
for (byte i = 0; i < numRows; i++)
{
grid.Add(ButtonRowCreator(numCols, 25, (i+1) * 50));
}
}
public List<Button> ButtonRowCreator(byte numOfBtnsNeeded, int x, int y)
{
List<Button> btns = new List<Button>();
for (int i = 0; i < numOfBtnsNeeded; i++)
{
Button btn = new Button();
btn.Size = new Size(50, 50);
btn.Location = new Point(x + (i * btn.Width), y);
btns.Add(btn);
btn.Font = new Font("Georiga", 10);
this.Controls.Add(btn);
btn.Click += new EventHandler(btn_Click);
}
return btns;
}
void btn_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
int curRow = -1, curCol = -1;
for(int i = 0; i < grid.Count; i++)
{
int index = grid[i].IndexOf(btn);
if (index != -1)
{
curRow = i;
curCol = index;
Console.WriteLine("curRow = " + curRow.ToString() + ", curCol = " + curCol.ToString());
}
}
// ... now you can use "curRow", "curCol" and "grid" to do something ...
foreach (List<Button> row in grid)
{
foreach (Button col in row)
{
col.ForeColor = Color.Gray;
}
}
if (board[curRow, curCol] == 1)
{
if (btn.Text == "Hit")
{
}
else
{
btn.Text = "Hit";
btn.BackColor = Color.Red;
hit++;
}
if (hit == 17)
{
MessageBox.Show("Congratulations, You Sunk Their Battleships!");
MessageBox.Show("Thanks For Playing!");
MessageBox.Show("Goodbye!");
}
}
else
{
btn.Text = "Miss!";
btn.BackColor = Color.Blue;
}
我想这就是你想要的 看起来您的许多代码都是用来确定单击了什么按钮。此信息可以存储在Tag属性中的button对象本身上,从而大大简化了代码
private Button[,] _grid1;
private Button[,] _grid2;
public UserForm()
{
InitializeComponent();
_grid1 = new Button[10, 10];
_grid2 = new Button[10, 10];
CreateGrid(_grid1, 10, 10, 25, 0, 20, true);
CreateGrid(_grid2, 10, 10, 25, 250, 20, false);
}
public void CreateGrid(Button[,] grid, int numOfRows, int numOfCols, int offsetX, int offsetY, int buttonSize, bool enabled)
{
for (byte i = 0; i < numOfRows; i++)
{
for (byte j = 0; j < numOfCols; j++)
{
grid[i,j] = ButtonCreator(i, j, offsetX, offsetY, buttonSize, enabled);
}
}
}
public Button ButtonCreator(int row, int col, int x, int y, int buttonSize, bool enabled)
{
Button btn = new Button();
btn.Size = new Size(buttonSize, buttonSize);
btn.Location = new Point(x + (col * buttonSize), y + (row * buttonSize));
btn.Font = new Font("Georiga", 10);
this.Controls.Add(btn);
btn.Click += new EventHandler(btn_Click);
btn.Tag = row + "," + col;
btn.Enabled = enabled;
return btn;
}
void btn_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
string[] coord = btn.Tag.ToString().Split(',');
int curRow = Convert.ToInt32(coord[0]);
int curCol = Convert.ToInt32(coord[1]);
Console.WriteLine(curRow = " + curRow + ", curCol = " + curCol);
// ... now you can use "curRow", "curCol" to do something ...
_grid1[curRow, curCol].BackColor = Color.Red;
}
你有什么问题?只是创建了第二组按钮还是更多?只是第二组按钮,每次我尝试时,都会出现问题,因为我无法找出如何分离不同的控件集。简单地说,我只想控制它,就好像它们是两个独立的项目。你尝试了什么,它导致了什么问题?你应该在问题中包括这些。我试着将代码加倍,这只是造成了很多名称问题。我还尝试将它转移到一个类中,并将其更改为能够调用它,这样我就可以从单独的类中调用它,但是由于语法的更改,这失败了。我尝试的代码要么无法访问,要么与第一组按钮的名称相矛盾。没有太多问题,我一直在尝试随机的东西并做一些研究,但我还没有发现任何对我有帮助的东西。重要的是我想同时控制它们,这样我就可以改变可见性来在两者之间切换。我仍然不清楚你想要实现什么?2个用户表单还是1个用户表单,带两个10x10按钮?网格应该如何定位?也许你应该画一张图表,有点像。我感谢你的努力。这很接近我想要的。然而,我想强调的是,我希望能够分别控制两个网格,以便让计算机控制其中一个,用户控制另一个。我想有一些方法来适应它,这样我就可以做到,但我想不出来。当我测试你向我建议的代码时,它工作了,但是两组按钮都连接到相同的控件,我可以想出如何以不同的方式控制一组。你说的以不同的方式控制一组是什么意思?您可以控制每个网格\u grid1、\u grid2来执行您想要的操作。你的意思是你不希望用户能够点击第二个网格?正是,第二个网格我希望人工智能与之交互,但不是用户。人工智能如何与网格交互?人工智能实际上就是我为计算机编写的任何代码,一旦用户轮到了他们。所以说交互,我只是指第二个网格用户无法使用,但编码计算机/AI本身仍然难以处理。