当我的鼠标指向C#中的另一个按钮时,我想禁用其他按钮
private void Form1_Load(object sender, EventArgs e){
foreach (Button btn in Controls.OfType<Button>())
{
btn.MouseEnter += new System.EventHandler(btn_MouseEnter);
btn.MouseLeave += new System.EventHandler(btn_MouseLeave );
}
}
private void btn_MouseEnter(object sender, System.EventArgs e)
{
var senderButton = (Button)sender;
senderButton.Enable = true; // not working
}
private void btn_MouseLeave (object sender, System.EventArgs e)
{
var senderButton = (Button)sender;
senderButton.Enable = false; // not working
}
答案 0 :(得分:5)
这可能不是最佳解决方案,但无论如何我都会尝试。
private void btn_MouseEnter(object sender, System.EventArgs e)
{
var senderButton = (Button)sender;
foreach(var btn in this.Controls.OfType<Button>())
{
if (btn != senderButton)
btn.Enabled = false;
}
}
private void btn_MouseLeave(object sender, System.EventArgs e)
{
foreach(var btn in this.Controls.OfType<Button>())
{
btn.Enabled = true;
}
}
答案 1 :(得分:2)
而不是
senderButton.Enable = true;
你必须遍历所有按钮,如
foreach(Button btn in this.Controls.OfType<Button>())
{
if (btn != senderButton)
btn.Enable = false;
}
答案 2 :(得分:1)
private void btn_MouseEnter(object sender, System.EventArgs e)
{
foreach (Button btn in Controls.OfType<Button>())
{
if (btn == ((Button)sender))
{
btn.Enabled = true;
}
else
{
btn.Enabled = false;
}
}
}
private void btn_MouseLeave(object sender, System.EventArgs e)
{
foreach (Button btn in Controls.OfType<Button>())
{
btn.Enabled = true;
}
}