遍历名称相似的控件 [英] Looping through controls with similar names

查看:73
本文介绍了遍历名称相似的控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有选项卡式控件的窗体(5个选项卡),在一个选项卡上,我有10个图片框,分别名为pic1,pic,pic3等.在VBA中,可以使用以下方式遍历控件:

I have a form with a tabbed control (5 tabs) and on one tab I have 10 picture boxes, named pic1, pic, pic3 and so on. In VBA it is possible to loop through the controls using something like this:

 For i = 1 To 10
    Me.Controls("Img" & i).Picture = Me.cboProperty.Column(i)
 Next

此刻我有

 this.pic1.DataBindings.Add("ImageLocation", this.mainTableBindingSource, "localPic1");
 this.pic2.DataBindings.Add("ImageLocation", this.mainTableBindingSource, "localPic2");
 this.pic3.DataBindings.Add("ImageLocation", this.mainTableBindingSource, "localPic3");

直到10,但是肯定有更好的方法吗?

until 10, but surely there is a better way?

推荐答案

就像那样,不.通常,您会加入如下一组控件:

Like that, no. Normally, you would acceess a group of controls like this:

foreach (var pictureBox in PictureTab.Controls.OfType(Of PictureBox)())
{
   pictureBox.Picture = //...
}

当然,这里的窍门是没有索引,无法知道要查看哪一列.但这很容易解决:

Of course, the trick here is that there is no index, to know which column to look at. But that's easy enough to get around:

int i = 0;
foreach (var pictureBox in PictureTab.Controls.OfType<PictureBox>())
{
   pictureBox.Picture = cboProperty.Column[i];
   i++;
}

根据您的修改,您可能需要这样做:

Based on your edit, you may want this:

int i = 0;
foreach(var pictureBox in PictureTab.Controls.OfType<PictureBox>())
{
    picutreBox.DataBindings.Add("ImageLocation", mainTableBindingSource, string.Format("loalPic{0}", i));
    i++;
}

这篇关于遍历名称相似的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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