日期:2014-05-18  浏览次数:20461 次

求简单代码
protected   void   btn1_Click(object   sender,   EventArgs   e)
        {
                btn1.ForeColor   =   System.Drawing.Color.FromName( "blue ");
                btn2.ForeColor   =   System.Drawing.Color.FromName( "white ");
                btn3.ForeColor   =   System.Drawing.Color.FromName( "white ");
                btn4.ForeColor   =   System.Drawing.Color.FromName( "white ");
                btn5.ForeColor   =   System.Drawing.Color.FromName( "white ");                
        }
怎样实现批处理?其他LinkButton有同样的功能

------解决方案--------------------
你想要怎样的批处理?
------解决方案--------------------
如果是完成同样的功能,用同一个事件委托,或是写成一个方法,分别调用
------解决方案--------------------
for(int i=1;i <10;i++)
{
(Button)this.FindControl( "btn1 ").ForeColor = System.Drawing.Color.FromName( "blue ");
}
------解决方案--------------------
如果是改变所有按钮的属性的话,委托可以
------解决方案--------------------
如果完成同样的功能,用同一个事件处理程序即可
------解决方案--------------------
这样好像没有办法批处理把
------解决方案--------------------
public Form1()
{
InitializeComponent();
foreach (Button btn in Controls)
{
if (btn != null)
{
btn.Click+=new EventHandler(ButtonClick);
}
}
}

private void ButtonClick(object sender, EventArgs e)
{
foreach (Button btn in Controls)
{
if(btn!=null)
{
btn.ForeColor = System.Drawing.Color.FromName( "white ");
}
}
(sender as Button).ForeColor = System.Drawing.Color.FromName( "blue ");
}
------解决方案--------------------
for(int i=1;i <10;i++)
{
(Button)this.FindControl( "btn1 ").ForeColor = System.Drawing.Color.FromName( "blue ");
}
---------------------------------
人家是点击事件
------解决方案--------------------
我的是WinForm 忘了说了
------解决方案--------------------
protected void btn_Click(object sender, EventArgs e)
{
(sender as LinkButton).ForeColor = System.Drawing.Color.FromName( "blue ");
foreach(Control c in this.Controls)
{
if(c is LinkButton)
(c as LinkButton).ForeColor = System.Drawing.Color.FromName( "white ");
}
}

然后所有的 LinkButton.Click += new EventHandler(btn_Click);