[問題] 如何讓timer+imagelist隨機圖不重複?
看板C_Sharp (C#)作者windmax1 (I do my best)時間11年前 (2014/04/14 18:19)推噓1(1推 0噓 11→)留言12則, 3人參與討論串1/2 (看更多)
private void timer1_Tick(object sender, EventArgs e)
{
intSpinCount++;
if (intSpinCount <= 20) {
// 轉動中;
Random rnd = new Random();
int intIndex = rnd.Next(imageList1.Images.Count);
//0到imageList1.Images.Count隨機取一;
2~6省略
pictureBox1.Image = imageList1.Images[intIndex];
2~6省略
} else { // 停止; //隨機取值;
Random rnd = new Random();
int myIndex = rnd.Next(imageList1.Images.Count);
//0到imageList1.Images.Count隨機取一;
2~6省略
pictureBox1.Image = imageList1.Images[myIndex];
timer1.Enabled = false;
ifSpin = false;
btnAnimateImage.Enabled = true;
btnAnimateImage.Text = "動態切換圖片"; }
產生結果如圖http://imgur.com/2AakeZL

想要讓六個號碼彼此之間不重複,GOOGLE研究半天研究不出個所以
拜託高手指點一下方向!!
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 124.11.129.171
※ 文章網址: http://www.ptt.cc/bbs/C_Sharp/M.1397470749.A.5ED.html
※ 編輯: windmax1 (124.11.129.171), 04/14/2014 18:23:42
→
04/14 20:04, , 1F
04/14 20:04, 1F
→
04/14 20:04, , 2F
04/14 20:04, 2F
推
04/15 00:13, , 3F
04/15 00:13, 3F
→
04/15 00:14, , 4F
04/15 00:14, 4F
→
04/15 15:11, , 5F
04/15 15:11, 5F
→
04/15 15:50, , 6F
04/15 15:50, 6F
不好意思,小弟是新手,所以遇到問題比較多
請教Ha大 我照你的連結最上面的程式,試著修改後將它套進我的程式碼
放在timer.Tick底下
int n = imageList1.Images.Count;
while (n > 1)
{
n--; int k = rnd.Next(n + 1);
T value = imageList1.Images[k];
imageList1.Images[k] = imageList1.Images[n];
imageList1.Images[n] = value;
}
遇到一個問題,就是T value一定要在Shuffle<T>底下才會過
可是如果把上面這段放在Shuffle<T>底下imageList1.Images就無法過
我現在不知道怎麼做結合 冏
※ 編輯: windmax1 (124.11.129.171), 04/15/2014 18:33:31
→
04/16 17:19, , 7F
04/16 17:19, 7F
再請教H大
我修改後試著放入,可是
shuffledList[i]這段會顯示↓↓
錯誤 1 無法套用有 [] 的索引至類型
'System.Linq.IOrderedEnumerable<System.Windows.Forms.ImageList>' 的運算式
※ 編輯: windmax1 (124.11.129.171), 04/16/2014 18:01:45
→
04/16 23:23, , 8F
04/16 23:23, 8F
→
04/16 23:29, , 9F
04/16 23:29, 9F
→
04/17 09:20, , 10F
04/17 09:20, 10F
→
04/17 17:43, , 11F
04/17 17:43, 11F
→
04/17 17:44, , 12F
04/17 17:44, 12F
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 2 篇):
C_Sharp 近期熱門文章
PTT數位生活區 即時熱門文章