将一个变量的值(整数)添加到 Windows Forms 中的控件名称(PictureBox)?

huangapple go评论76阅读模式
英文:

Adding the value (integer) of a variable to a control name (PictureBox) in Windows Forms?

问题

我的所有图片框控件的名称都一样,只有末尾的数字不同。因为我是这样制作的,是否可以将整数值与PictureBox名称的末尾匹配,并避免复制粘贴一些代码?

我的代码:

将一个变量的值(整数)添加到 Windows Forms 中的控件名称(PictureBox)?

英文:

All my picture box controls are named the same beside the number at the end. Since I made it this way, is it possible to match an int value to the end of the PictureBox name and save copy/pasting some code?

My code:

将一个变量的值(整数)添加到 Windows Forms 中的控件名称(PictureBox)?

答案1

得分: 1

一种方法是创建一个数组,然后索引到数组中:

var controls = new Control[] { soldOutPB1, soldOutPB2, soldOutPB3 };

controls[index].Visible = true;

另一种方法是使用控件集合,并将控件名称指定为字符串:

var control = this.Controls[$"soldOutPB1{index}"].Visible = true;
英文:

Two ways to do this:

One way is to create an array and index into that instead

var controls = new Control[] { soldOutPB1, soldOutPB2, soldOutPB3 };

controls[index].Visible = true;

Another way is to use the controls collection and specify the control name as a string:

var control = this.Controls[$"soldOutPB1{index}"].Visible = true;

huangapple
  • 本文由 发表于 2023年2月27日 08:29:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75575894.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定