首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

vs2005 中ImageList中InvalidArgument=“一”的值对于“index”无效

2012-07-25 
vs2005 中ImageList中InvalidArgument“1”的值对于“index”无效private void Form1_Load(object sender, Ev

vs2005 中ImageList中InvalidArgument=“1”的值对于“index”无效
private void Form1_Load(object sender, EventArgs e)
  {
  string path = Application.StartupPath.Substring(0,Application.StartupPath.Substring(0,Application.StartupPath.LastIndexOf("\\")).LastIndexOf("\\"));
  path += @"\01.jpg";
  string path2 = Application.StartupPath.Substring(0, Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("\\")).LastIndexOf("\\"));
  path2 += @"\02.jpg";
  Image ming = Image.FromFile(path,true);
  imageList1.Images.Add(ming);
  Image ming2 = Image.FromFile(path2, true);
  imageList1.Images.Add(ming2);
  imageList1.ImageSize = new Size(200,165);
  pictureBox1.Width = 200;
  pictureBox1.Height = 165;
   

  }

  private void button1_Click(object sender, EventArgs e)
  {
  int a = 0;
  if (a == (imageList1.Images.Count - 1))
  {
  a = 0;
  }
  else if(a<imageList1.Images.Count)
  {
  a = a + 1;
  }

  this.pictureBox1.Image=this.imageList1.Images[a];
  }

  private void button2_Click(object sender, EventArgs e)
  {
  pictureBox1.Image = imageList1.Images[1];
  }
  }



这是代码那里出错了啊,请大家帮忙

[解决办法]
ImageList没有2张图,所以不能用1,如果有1张图,用imageList1.Images[0];

[解决办法]

探讨
引用:
看这代码是看不出问题,你在 private void Form1_Load(object sender, EventArgs e) 的最后,private void button1_Click(object sender, EventArgs e) 最前,private void button2_Click(object sender, EventArgs e)
最前,……

[解决办法]
探讨
Application.StartupPath.Substring(0,Application.StartupPath.Substring(0,Application.StartupPath.LastIndexOf("\\")).LastIndexOf("\\"));
这句话是干嘛啊,好像是不是使用错误了

[解决办法]
C# code
我曾经也使用过ImageList,用下标取的时候,也是取不出来,也不知道什么原因;不过有解决办法:Image ming2 = Image.FromFile(path2, true);  imageList1.Images.Add(ming2);你添加的时候这样子添加: imageList1.Images.Add("ming2",ming2);给它添加一个key进去取的时候通过key来取例如imageList1["ming2"]这样子就可以取出来了。很奇怪,我怀疑是.net的bug 

热点排行