英文:
Adding the value (integer) of a variable to a control name (PictureBox) in 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:
答案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;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论