C# 单击时选择的Visual Studio Windows.Forms按钮背景色

C# 单击时选择的Visual Studio Windows.Forms按钮背景色,c#,winforms,visual-studio,button,C#,Winforms,Visual Studio,Button,//嗨, 我想写一个软件,但对“点击按钮时保持按钮处于选中状态(我指的是背景色),直到我点击另一个按钮”感到困惑 如果有人能帮助我,我会非常感激。 提前谢谢// namespace SoftwareUI { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void bu

//嗨, 我想写一个软件,但对“点击按钮时保持按钮处于选中状态(我指的是背景色),直到我点击另一个按钮”感到困惑

如果有人能帮助我,我会非常感激。 提前谢谢//

namespace SoftwareUI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            button1.ForeColor = Color.LightGray;
        }

        private void button1_Leave(object sender, EventArgs e)
        {
            button1.ForeColor = Color.GhostWhite;
        }

        private void button1_MouseEnter(object sender, EventArgs e)
        {
            button1.ForeColor = Color.LightSlateGray;
        }    
    }
}

为所有按钮的单击事件添加此代码:

Button b = (Button)sender;
b.BackColor = Color.LightGray;

foreach (Button bt in b.Parent.Controls.OfType<Button>())
{
    if (bt != b)
        bt.BackColor = Color.White;
}
按钮b=(按钮)发送器;
b、 背景色=颜色。浅灰色;
foreach(b.Parent.Controls.OfType()中的按钮bt)
{
如果(bt!=b)
bt.BackColor=Color.White;
}

我不确定这里是否有足够的信息来诊断问题。如果您希望在单击时设置按钮背景颜色(如在单击处理程序中),并保持该颜色,直到发生其他事情,然后删除其他鼠标事件处理程序。这些是您想要的:和。