c# winform 动态获取picturebox

hezhifang1998 2012-06-11 05:43:00
窗体中有0-20个picturebox控件 名称分别是
ptb_0 ptb_1 ptb_2 ptb_3 ...... ptb_20

如何动态循环分别设置picturebox 1 到 10 分别显示其它的图片
ptb_1.image="ddd.gif";
ptb_2.image="ddd.gif";
ptb_3.image="ddd.gif";
..................
ptb_10.image="ddd.gif";


求大家帮助呀

...全文
249 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
hezhifang1998 2012-06-12
  • 打赏
  • 举报
回复
明白了,有一个地方改一下
if (int.TryParse(str, out index) && (index >= 1 && index <= 10))

就可以了,谢谢大家
卧_槽 2012-06-11
  • 打赏
  • 举报
回复
控件多了会卡死的。
xtddjx 2012-06-11
  • 打赏
  • 举报
回复
同意3楼的
__天涯寻梦 2012-06-11
  • 打赏
  • 举报
回复
foreach (Control item in this.Controls)
{
PictureBox pbx = item as PictureBox;
if (pbx != null && pbx.Name.StartsWith("ptb_"))
{
string str = pbx.Name.Substring(4);
int index;
if (int.TryParse(str, out index) && (index >= 1 || index <= 10))
{
pbx.Image = Image.FromFile("ddd.gif");
}
}
}
hezhifang1998 2012-06-11
  • 打赏
  • 举报
回复
如何只设置 picturebox1 到10 不是所有的都设置
cnfixit 2012-06-11
  • 打赏
  • 举报
回复

foreach(Control con in this.Controls)
{
if(con is PictureBox)
{
(con as PictureBox).Image = Image.FromFile("xxx.xx");
}
}

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧