如何在WinForms运行时设置多个按钮文本?
我用的是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.
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.
Please Update Answers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果按钮按 1 到 20 的顺序排列,则不需要从按钮名称中提取数字,只需使用自动增量索引变量:
编辑:尝试一下,因为没有环境设置,所以没有检查
为了避免影响向上/向下按钮,请使用特殊标签值(例如 Tag="ControlButton")对其进行标记
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:
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"
您是否正在尝试找到一种简单的方法来设置这 20 个按钮控件的 Text 属性,以避免必须逐个设置它们?
在这种情况下,您可以迭代表单的控件并检查它是否是按钮。
例如:
有关迭代控件的更多信息可以在此处找到:
http://www.dotnetperls.com/control
ExtractNumber(...) 方法从按钮的 Name 属性中提取数字。
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:
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.
也许这可以帮助您: C# 创建控件数组
您可以创建一个按钮数组并分配按钮文本,如下所示:
Maybe this could help you: C# create an array of controls
You could create an array of buttons and assign the button text like:
一种简单的方法是在表单中设置 Button 对象的数组。数组中的每个元素都是您的按钮之一。
然后您可以稍后使用简单的 for 循环遍历按钮。
像这样:
但是你想做什么?可能有更有效的解决方案可以解决您的问题。
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:
But what are you trying to do? There is probably a more efficient solution to your problem.