请问如何将imagelist里面的8张图片随机放进8个pictureBox里面,不能重复

beardymanG 2011-11-17 12:32:21
RT,我的代码如下,
ImageList imglst = new ImageList();
List<PictureBox> pictureboxes = new List<PictureBox>();
List<int> indexes = new List<int>();
Random r = new Random(DateTime.Now.Millisecond);
for (int i = 0; i < 9; i++)
{
indexes.Insert(r.Next(0, indexes.Count - 1), i);
//Thread.Sleep(10);
}
foreach (var item in indexes)
{
pictureboxes[item].Image = imglst.Images[item];
}
报错是这一句:indexes.Insert(r.Next(0, indexes.Count - 1), i);
报错内容:“minValue”不能大于 maxValue。”

麻烦各位大哥大姐帮忙看看,我是新手,不怎么懂,如果各位有更好地实现方法,也可以教教我啊,谢谢了
...全文
293 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
beardymanG 2011-11-17
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 bdmh 的回复:]
一个列表,选出一个删掉一个,肯定重复不了
[/Quote]2L的大哥,我的想法就是这个,你可以帮我看看我的代码哪里出问题了吗?万分感激啊
csdn_aspnet 2011-11-17
  • 打赏
  • 举报
回复
//装载19个图片的ImageList
ImageList imglst = new ImageList();


//准备装载19个图片的PictureBox,建议使用List<T>
List<PictureBox> pictureboxes = new List<PictureBox>();



//产生随机索引顺序
List<int> indexes = new List<int>();
Random r = new Random(DateTime.Now.Millisecond);

for (int i = 0; i < 19; i++)
{
indexes.Insert(r.Next(0, indexes.Count - 1), i);
Thread.Sleep(10);
}



//装载图片
foreach (var item in indexes)
{
pictureboxes[item].Image = imglst.Images[item];
}
bdmh 2011-11-17
  • 打赏
  • 举报
回复
一个列表,选出一个删掉一个,肯定重复不了
beardymanG 2011-11-17
  • 打赏
  • 举报
回复
补充一下,这是老师叫我们做一个拼图游戏...3*3
Bule 2011-11-17
  • 打赏
  • 举报
回复
random.next(minValue,maxValue)得到minValue和maxValue之间的随机数,楼主的
r.Next(0, indexes.Count - 1),其中indexes.Count-1大于0吗?
阿非 2011-11-17
  • 打赏
  • 举报
回复
List<int> numList = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8 };

for (int i = 0; i < 9; i++)
{
int num = r.Next(0, numList.Count);
indexes.Add(numList[num]);
numList.Remove(numList[num]);
}


这样
阿非 2011-11-17
  • 打赏
  • 举报
回复
for (int i = 0; i < 9; i++)
{
indexes.Insert(r.Next(0, indexes.Count - 1), i);
//Thread.Sleep(10);
}
=>

List<int> numList = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8 };

for (int i = 0; i < 9; i++)
{
int num = r.Next(0, numList.Count);
while (indexes.Contains(numList[num]))
{
num = r.Next(0, numList.Count);
}
indexes.Add(numList[num]);
numList.Remove(numList[num]);
}

110,544

社区成员

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

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

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