如何在WinForms运行时设置多个按钮文本?

发布于 2024-12-28 23:39:23 字数 887 浏览 1 评论 0原文

我用的是Winform。我设置了 20 个按钮以及向上和向下按钮,如下所示。 这些按钮的文本值为button1 = 1;按钮2=2; ... 开始时按钮20 =20。我已经做到了。

在此处输入图像描述

当我按下向下按钮时,20 个按钮的文本已更改为 21 到 40;和向上按钮,它返回 1 到 20。

btn_Down click event

button21.Text = "21";
button22.Text = "22";
...
..
button40.Text = "40";

and btn_Up Click event

btn_Down click event

button1.Text = "1";
button2.Text = "2";
...
..
button20.Text = "20";

有任何简单的方法可以对此进行编码吗?

更新 -

当我尝试 sll 代码时。

int index = 0;

foreach (var control in this.tableLayoutPanel1.Controls)
 {
     var button = control as Button;
     if (button != null)
     {
        button.Text = String.Format("{0}", index);
        index++;
     }
}

我得到了下面。上下也变了,数字顺序也颠倒了。

在此处输入图像描述

请更新答案。

I am using Winform. In that i have set 20 buttons and up and down buttons like below.
These buttons text value is button1 = 1; button 2 =2; ... button20 =20 at starting time. I have done this.

enter image description here

When I press Down button, the 20 buttons text has changed to 21 to 40; and Up buttons it returns 1 to 20.

btn_Down click event

button21.Text = "21";
button22.Text = "22";
...
..
button40.Text = "40";

and btn_Up Click event

btn_Down click event

button1.Text = "1";
button2.Text = "2";
...
..
button20.Text = "20";

Is any Simple way to Code this?

Updated -

When I try sll code,.

int index = 0;

foreach (var control in this.tableLayoutPanel1.Controls)
 {
     var button = control as Button;
     if (button != null)
     {
        button.Text = String.Format("{0}", index);
        index++;
     }
}

I got Below. The Up and Down is also Changed, And The Numbers Order is reverse.

enter image description here

Please Update Answers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

如果按钮按 1 到 20 的顺序排列,则不需要从按钮名称中提取数字,只需使用自动增量索引变量:

int index = 0;
// either form.Controls or this.Controls dedepnds on where you put this code
foreach (var control in form.Controls)
{
   var button = control as Button;
   if (button != null)
   {
      button.Text = String.Format("Button #{0}", index);     
      index++;
   }      
}

编辑:尝试一下,因为没有环境设置,所以没有检查

为了避免影响向上/向下按钮,请使用特殊标签值(例如 Tag="ControlButton")对其进行标记

using System.Linq;
var buttons = this.tableLayoutPanel1.Controls
                                    .OfType<Button>()
                                    .Where(b => b.Tag != "ControlButton");
int index = buttons.Count();
foreach (var button in buttons)
{
    button.Text = String.Format("Button #{0}", index);     
    index--;
}

If buttons are arranged in the order from 1 to 20 you do not need extracting number from a button name, just use autoincrement index variable:

int index = 0;
// either form.Controls or this.Controls dedepnds on where you put this code
foreach (var control in form.Controls)
{
   var button = control as Button;
   if (button != null)
   {
      button.Text = String.Format("Button #{0}", index);     
      index++;
   }      
}

EDIT: Try it out, have nopt checked since have no environment setup in hand

To avoid affecting Up/Down buttons mark it by special tag value like Tag="ControlButton"

using System.Linq;
var buttons = this.tableLayoutPanel1.Controls
                                    .OfType<Button>()
                                    .Where(b => b.Tag != "ControlButton");
int index = buttons.Count();
foreach (var button in buttons)
{
    button.Text = String.Format("Button #{0}", index);     
    index--;
}
东京女 2025-01-04 23:39:23

您是否正在尝试找到一种简单的方法来设置这 20 个按钮控件的 Text 属性,以避免必须逐个设置它们?

在这种情况下,您可以迭代表单的控件并检查它是否是按钮。

例如:

foreach(var control in base.Controls)
{
    if (control is Button)
    {
        var button = (Button) control;
        button.Text = ExtractNumber(button.Name);
    }
}

有关迭代控件的更多信息可以在此处找到:

http://www.dotnetperls.com/control

ExtractNumber(...) 方法从按钮的 Name 属性中提取数字。

private string ExtractNumber(string value)
{
    var number = Regex.Match(value, @"\d+").Value;
    return number;
}

Are you trying to find an easy way to set the Text property of those 20 button controls to avoid having to set them one by one?

In that case you could iterate over the form's controls and check if it is a button.

For example:

foreach(var control in base.Controls)
{
    if (control is Button)
    {
        var button = (Button) control;
        button.Text = ExtractNumber(button.Name);
    }
}

More information about iterating the controls can be found here:

http://www.dotnetperls.com/control

The ExtractNumber(...) method extract the number from the button's Name property.

private string ExtractNumber(string value)
{
    var number = Regex.Match(value, @"\d+").Value;
    return number;
}
独守阴晴ぅ圆缺 2025-01-04 23:39:23

也许这可以帮助您: C# 创建控件数组

您可以创建一个按钮数组并分配按钮文本,如下所示:

int i = start_value //e.g. 0 or 20 or 40...
foreach (button b in buttons)
{
    b.Text = i.ToString();
    i++;
}

Maybe this could help you: C# create an array of controls

You could create an array of buttons and assign the button text like:

int i = start_value //e.g. 0 or 20 or 40...
foreach (button b in buttons)
{
    b.Text = i.ToString();
    i++;
}
眼泪都笑了 2025-01-04 23:39:23

一种简单的方法是在表单中设置 Button 对象的数组。数组中的每个元素都是您的按钮之一。
然后您可以稍后使用简单的 for 循环遍历按钮。

像这样:

for(int i=0; i<buttons.Length; i++)
{
   buttons[i].Text = (i+1).ToString();
}

但是你想做什么?可能有更有效的解决方案可以解决您的问题。

A simple way would be to setup an array of Button objects in your form. Each element in the array would be one of your buttons.
Then you can later iterate through the buttons using a simple for-loop.

Something like this:

for(int i=0; i<buttons.Length; i++)
{
   buttons[i].Text = (i+1).ToString();
}

But what are you trying to do? There is probably a more efficient solution to your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文