更改窗体上的所有按钮 [英] Change all buttons on a form

查看:125
本文介绍了更改窗体上的所有按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经非常接近找到解决这一个;只是在这一点上缺少一个小细节。

I have come very close to finding a solution to this one; just missing one minor detail at this point.

我所试图做的事:

我想改变每个按钮的光标样式通过code我的窗体(Form1)上。我知道如何通过所有控件使用的的foreach 的搜索我的表上,但我不知道如何控制这种传递通过我写的程序的参数。我会告诉什么,我做下面的例子。

What I am trying to do:
I want to change the cursor style of every button on my Form (Form1) through code. I know how to search through all controls on my form using foreach, but I'm not sure how to pass this control as a parameter through the routine that I wrote. I will show an example of what I am doing below.

private void Form1_Load(object sender, EventArgs e)
{
    foreach (Button b in this.Controls)
    {
        ChangeCursor(b);  // Here is where I'm trying to pass the button as a parameter.  Clearly this is not acceptable.
    }     
}

private void ChangeCursor(System.Windows.Forms.Button Btn)
{
    Btn.Cursor = Cursors.Hand;
}

可能有人在提示我?

Might anyone have a tip for me?

非常感谢你。

埃文

推荐答案

我看到的唯一的事情是,如果你有嵌套控件,this.Controls不会挑选那些了。你可以试试这个

The only thing I see is that if you have nested controls, this.Controls will not pick those up. you can try this

public IEnumerable<Control> GetSelfAndChildrenRecursive(Control parent)
{
    List<Control> controls = new List<Control>();

    foreach(Control child in parent.Controls)
    {
        controls.AddRange(GetSelfAndChildrenRecursive(child));
    }

    controls.Add(parent);

    return controls;
}

和电话

GetSelfAndChildrenRecursive(this).OfType<Button>.ToList()
                  .ForEach( b => b.Cursor = Cursors.Hand);

这篇关于更改窗体上的所有按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆