我正在尝试为游戏制作简单的网格。我是c#和编程的新手。有人可以帮助我如何限制按钮的点击次数。我想在我的网格中只创建一个播放器,所以在单击后我想限制用户创建更多播放器。我需要你的专家帮助。
protected void SetClicks()
{
foreach ( Control c in this.panel1.Controls)
{
if ( c is Button )
{
Button who = c as Button;
who.Click += new EventHandler(WhoClicked);
}
}
}
protected void MakeButtons()
{
rowNum = UpDownRow.Text;
int nr = Int16.Parse(rowNum);
colNum = UpDownColumn.Text;
int nc = Int16.Parse(colNum);
int btnHeight = panel1.Height / Int16.Parse(rowNum);
int btnWidth = panel1.Width / Int16.Parse(colNum);
for (int row = 0; row < nr; row++)
{
for (int column = 0; column < nc; column++)
{
Button btnNew = new Button();
btnNew.Name = "btn_" + column + "_" + row;
btnNew.Height = btnHeight-5;
btnNew.Width = btnWidth-5;
btnNew.Font = new Font("Arial", 20);
// btnNew.Text = theSymbol;
btnNew.Image = Properties.Resources.backg;
btnNew.Visible = true;
// int CenterPoint = panel1.Width / 3;
btnNew.Location = new Point(10 + (column* btnNew.Width), 10 + (row* btnNew.Height));
//Controls.Add(btnNew);
panel1.Controls.Add(btnNew);
}
}
}
private void button2_Click(object sender, EventArgs e)
{
picSymbol = Properties.Resources.Player;
button2.Enabled = false;
// want some help here
MessageBox.Show("Too Many Player", "Player number exceed",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
答案 0 :(得分:2)
我想你会想要:
假设button2
启动为启用。
private void button2_Click(object sender, EventArgs e)
{
picSymbol = Properties.Resources.Player;
//Will only run if button is disabled, which is after the first player creation.
button2.Enabled = false;
btn.Text = "Game Disabled";
}
正如评论中所提到的,也许你的网格有很多按钮,所有按钮都挂在同一个事件上。
在这种情况下,您可以使用sender
,这是触发事件的按钮。
private void button2_Click(object sender, EventArgs e)
{
picSymbol = Properties.Resources.Player;
Button btn = sender as Button;
//Will only run if button is disabled, which is after the first player creation.
if (!btn.Enabled){
MessageBox.Show("Too Many Player", "Player number exceed",
MessageBoxButtons.OK, MessageBoxIcon.Error);
//or
btn.Text = "Game Disabled";
}
else
{
btn.Enabled = false;
}
}
答案 1 :(得分:2)
当用户第二次单击启用按钮时,您必须在禁用按钮或显示消息框之间进行选择。禁用按钮不响应用户活动,因此您无法响应第二次单击。
大多数WinForms用户都习惯于禁用控件 - 你可以做的是禁用按钮并将其Text
更改为“游戏已满”,以便用户了解禁用它的原因。
答案 2 :(得分:1)
如果表单上的唯一按钮位于网格中,则可以执行此操作;
private void button2_Click(object sender, EventArgs e)
{
picSymbol = Properties.Resources.Player;
foreach (var control in this.Controls)
{
if (control is Button)
{
control.Enabled = false;
}
}
}
它未经过测试,但它应该
修改强>
根据您更新的问题,我在按钮创建时添加了事件绑定并定义了事件处理程序;
protected void MakeButtons()
{
rowNum = UpDownRow.Text;
int nr = Int16.Parse(rowNum);
colNum = UpDownColumn.Text;
int nc = Int16.Parse(colNum);
int btnHeight = panel1.Height / Int16.Parse(rowNum);
int btnWidth = panel1.Width / Int16.Parse(colNum);
for (int row = 0; row < nr; row++)
{
for (int column = 0; column < nc; column++)
{
Button btnNew = new Button();
btnNew.Name = "btn_" + column + "_" + row;
btnNew.Height = btnHeight-5;
btnNew.Width = btnWidth-5;
btnNew.Font = new Font("Arial", 20);
// btnNew.Text = theSymbol;
btnNew.Image = Properties.Resources.backg;
btnNew.Visible = true;
// int CenterPoint = panel1.Width / 3;
btnNew.Location = new Point(10 + (column* btnNew.Width), 10 + (row* btnNew.Height));
// hook this button to a click event
btnNew.Click += new EventHandler(WhoClicked);
//Controls.Add(btnNew);
panel1.Controls.Add(btnNew);
}
}
}
private void WhoClicked(object sender, EventArgs e)
{
picSymbol = Properties.Resources.Player;
foreach (var control in this.panel1.Controls)
{
if (control is Button)
{
control.Enabled = false;
}
}
}