C# 单击1次后禁用按钮
我正在尝试为游戏制作简单的网格。我对c#和编程非常熟悉。有人能帮我限制按钮的点击吗。我只想在我的网格中创建一个玩家,所以在单击一次后,我想限制用户创建更多玩家。我需要你的专家帮助C# 单击1次后禁用按钮,c#,winforms,C#,Winforms,我正在尝试为游戏制作简单的网格。我对c#和编程非常熟悉。有人能帮我限制按钮的点击吗。我只想在我的网格中创建一个玩家,所以在单击一次后,我想限制用户创建更多玩家。我需要你的专家帮助 protected void SetClicks() { foreach ( Control c in this.panel1.Controls) { if ( c is Button ) { But
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);
}
protectedvoid SetClicks()
{
foreach(此面板1.控件中的控件c)
{
如果(c是按钮)
{
按钮who=c作为按钮;
who.Click+=新事件处理程序(WhoClicked);
}
}
}
受保护的void MakeButtons()
{
rowNum=UpDownRow.Text;
intnr=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);
对于(int行=0;行
我想你会想要:
假设按钮2
以启用状态启动
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";
}
正如在评论中提到的,可能您的网格有许多按钮,并且都与同一事件挂钩
在本例中,您将使用发送方
,这是触发事件的按钮
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;
}
}
我想你会想要的: 假设
按钮2
以启用状态启动
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";
}
正如在评论中提到的,可能您的网格有许多按钮,并且都与同一事件挂钩
在本例中,您将使用发送方
,这是触发事件的按钮
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;
}
}
当用户第二次单击“已启用”按钮时,必须在禁用按钮或显示消息框之间进行选择。禁用的按钮不响应用户活动,因此您无法响应第二次单击
大多数WinForms用户非常习惯于禁用控件-您可以禁用按钮,并将其
文本更改为“游戏完整”,以便用户了解禁用的原因。您必须在禁用按钮或在用户第二次单击启用按钮时显示消息框之间进行选择。禁用的按钮不响应用户活动,因此您无法响应第二次单击
大多数WinForms用户非常习惯于禁用控件-您可以做的是禁用按钮,并将其文本更改为“游戏完整”,以便用户了解禁用的原因。如果表单上只有按钮在网格中,您可以这样做
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;
}
}
}
受保护的void MakeButtons()
{
rowNum=UpDownRow.Text;
intnr=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);
对于(int行=0;行
如果表单上只有按钮在网格中,则可以执行此操作
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;
}
}
}
受保护的void MakeButtons()
{
rowNum=UpDownRow.Text;
intnr=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);
对于(int行=0;行