110,571
社区成员
发帖
与我相关
我的任务
分享
TextBox[] text = new TextBox[3];
Button[] button = new Button[3];
bool[] tb=new bool[3];
private void button1_Click(object sender, EventArgs e)
{
string textname =(sender as Button).Text;
int textint=int.Parse(textname.Substring(6,1));
if (!tb[textint])
{
text[textint].Text = textname;
}
else
{
text[textint].Text = "";
}
tb[textint] = !tb[textint];
}
private void Form1_Load(object sender, EventArgs e)
{
text[1] = textBox1;
text[2] = textBox2;
button[1] = button1;
button[2] = button2;
tb[1] = false;
tb[2] = false;
}
// 1. 将所有左侧所有的 button 绑定同一个 click event
// 2. 定义一个全局 button 变量 btnLeft
// 3. 公共 button_Click 事件方法如下:
public void button_Click(sender obj, EventArgs e)
{
btnLeft = (Button)obj;
}
// 4. 在右边的按钮事件方法中,如下实现:
public void button_Click2(sender obj, EventArgs e)
{
(sender as Button).Text = btnLeft.Text;
}
if (button1.Tag == null)
{
button1.Tag = true;
}
else
{
bool clicked = (bool)button1.Tag;
button1.Tag = !clicked;
}