如何从窗体中获取按钮控件的名称 [英] how to get button controls name from form
本文介绍了如何从窗体中获取按钮控件的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从form获取按钮控件名称.使用该名称,我必须更改背景颜色
how to get button controls name from form . using that name i have to change background color
推荐答案
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 namedbtnRed, 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();
}
这篇关于如何从窗体中获取按钮控件的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文