且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从窗体中获取按钮控件的名称

更新时间:2023-12-06 11:03:28

this.button_name.BackgroundColor = your_color_name_here; //get from enumeration :)





Button button = this.FindControl("name_of_yourcontrol");
button.BackgroundColor = your_color_here; //get from enumeration



希望对您有所帮助:)



Hope it helps :)


假设您的按钮名为btnRed, btnBlue, btnGreen,依此类推,然后您更改了表单的背景色,只需添加一个通用的Click事件处理程序即可:
Supposing that your buttons are named btnRed, btnBlue, btnGreen, and so on, and you change the background color of the form, just add a common Click event handler:
void ColorButtonClick(object sender, EventArgs e)
{
    this.BackColor = Color.FromName((sender as Button).Name.Substring(3));
}


private List<string> GetAllButtonNames()
{
    return (from ctrl in this.Controls.OfType<Button>() 
                              select ctrl.Name).ToList();
}