一次操作即可更改多个按钮的文字 [英] Change text for multiple buttons in one operation

查看:113
本文介绍了一次操作即可更改多个按钮的文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含许多按钮(50+)的表单,除了后缀号外,它们都具有相同的名称. (btn_0btn_1btn_3等)

I have a form which consist of many buttons (50+) and they all have the same name except for the suffix number. (btn_0, btn_1, btn_3, etc.)

我想通过一次操作更改这些按钮的文本.

I want to change the text of those buttons in one operation.

是否有一种将按钮像数组一样对待的方法?

Is there a way of treating buttons like arrays?

btn_[i].Text = "something"? 

也许执行一个字符串?

"btn_{0}.Text=\"something\""

推荐答案

您将需要同时访问每个按钮.

you will need to access each button at a time to do this.

像这样循环进行

foreach(var btn in this.Controls)
{
    Button tmpbtn;
    try
    {
        tmpbtn = (Button) btn;
    }
    catch(InvalidCastException e)
    {
        //perform required exception handelling if any.
    }
    if(tmpbtn != null)
    {
       if(string.Compare(tmpbtn.Name,0,"btn_",0,4) == 0)
       {
            tmpbtn.Text = "Somthing"; //Place your text here
       }
    }
}

请查看所使用的重载比较方法.

这篇关于一次操作即可更改多个按钮的文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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