2012-11-21 38 views
1

當我說我是一個初學者時,我真的很新鮮,因爲您可以在前面道歉!在每個按鈕上點擊圖片循環的兩個圖片框點擊

我想要做的是在窗體中有兩個圖片框和一個按鈕。當我點擊該按鈕,它從我的圖片文件夾中選取2倍新的圖像,它會做這個一共有5次

任何想法我如何處理這一點 - 這是我到目前爲止的按鈕,點擊

private void button1_Click(object sender, EventArgs e) 
    { 
     pictureBox1.Load(@"C:\Resources\PICT01.JPG"); 
     pictureBox2.Load(@"C:\Resources\PICT02.JPG"); 
} 

任何答案真的需要基本的,因爲我只是在學習!在此先感謝

+0

根據你正在做的或者有..do要加載2個圖像什麼的5倍磨片用戶點擊一次按鈕..?如果是這樣,那麼這種方法是不正確的..如果我正確理解你的問題,你將需要5個圖片框..或者你需要創建動態5圖片盒.. – MethodMan

+0

感謝DJ Kraze - 我需要2個圖片框加載每次單擊按鈕時最多可以點擊5次不同的圖像..有意義嗎? –

+0

你是否需要這些圖像是隨機或只是一個硬編碼進展 –

回答

1

您需要創建一個全局int來跟蹤您切換圖片的頻率,並在button1_click中處理該數字。

我不是專家我自己,但這是我將如何做到這一點。一個開關是理想的,因爲你需要檢查5種不同的可能性。

//global int 
int count = 0; 

private void button1_Click(object sender, EventArgs e) 
{ 
    count++; 
    switch(count) 
    { 
    case 1: 
     //load image 1 and 2 
     break; 
    case 2: 
     //load image 3 and 4 
     break; 
    case 3: 
     //load image 5 and 6 
     break; 
    case 4: 
     //load image 7 and 8 
     break; 
    case 5: 
     //load image 9 and 10 
     break; 
    default: 
     break; 
    } 
} 
+0

蒂姆 - 非常感謝你 - 這正是我所需要的 - 正如前面提到的,我被卡住了一段時間,所以這真的讓我出了一口漏洞! –

+0

不客氣! –

0

此示例程序將創建MyPictures文件夾中的所有JPG格式文件的列表,從列表中隨機挑選的圖片,當列表爲空,將引發異常。

public Form1() 
{ 
    // Reading pictures from My Pictures: 
    string path = Environment.GetFolderPath(Environment.SpecialFolders.MyPictures); 
    DictionaryInfo myPictures = new DictionaryInfo(path); 
    FPictureFiles = myPictures.GetFiles("*.jpg", SearchOption.AllDirectories).ToList(); 
} 
private List<FileInfo> FPictureFiles; 
private Random FRandom = new Random(); 

private void button1_Click(object sender) 
{ 
    pictureBox1.Load(PickFile()); 
    pictureBox2.Load(PickFile()); 
} 

private string PickFile() 
{ 
    if (FPictureFiles.Count == 0) throw new Exception("No more picture files"); 

    int index = FRandom.Next(FPictureFiles.Count); 
    string filename = FPictureFiles[index].FullName; 
    FPictureFiles.RemoveAt(index); 
    return filename; 
} 

我希望這會幫助你在你的追求。

+0

非常感謝您花時間做到這一點 - 再次強調這段代碼非常有趣,我會100%嘗試實現它 - 它幫助我學習! –

0

您可以嘗試創建一個新的List<string>,其中列出了具有所選內容的特定擴展名的所有文件。然後,初始化一個新的Random類,該類在特定範圍內獲得一個隨機數,考慮到該範圍不會超過我們的List<string>.Count值。

假設CurrentClicks確定一個新的整數和MaximumClicks是我們的最大價值

public Form1() 
{ 
    InitializeComponent(); 
    button1.Click += new EventHandler(button1_Click); //Link the Click event of button1 to button1_Click 
} 

const int MaximumClicks = 5; //Initialize a new constant int of value 5 named MaximumClicks 
int CurrentClicks = 0; //Initialize a new variable of name CurrentClicks as an int of value 0 

以下情況可適用

private void button1_Click(object sender, EventArgs e) 
{ 
    if (CurrentClicks < MaximumClicks) //Continue if CurrentClicks is less than 5 
    { 
     string FilesPath = @"D:\Resources\International"; //Replace this with the targeted folder 
     string FileExtensions = "*.png"; //Applies only to .png file extensions. Replace this with your desired file extension (jpg/bmp/gif/etc) 
     List<string> ItemsInFolder = new List<string>(); //Initialize a new Generic Collection of name ItemsInFolder as a new List<string> 
     foreach (string ImageLocation in Directory.GetFiles(FilesPath, FileExtensions)) //Get all files in FilesPath creating a new string of name ImageLocation considering that FilesPath returns a directory matching FileExtensions considering that FileExtensions returns a valid file extension within the targeted folder 
     { 
      ItemsInFolder.Add(ImageLocation); //Add ImageLocation to ItemsInFolder 
     } 
     Random Rnd = new Random(); //Initialize a new Random class of name Rnd 
     int ImageIndex = Rnd.Next(0, ItemsInFolder.Count); //Initialize a new variable of name ImageIndex as a random number from 0 to the items retrieved count 
     pictureBox1.Load(ItemsInFolder[ImageIndex]); //Load the random image based on ImageIndex as ImageIndex belongs to a random index in ItemsInFolder 
     CurrentClicks++; //Increment CurrentClicks by 1 
    } 
    else 
    { 
     //Do Something (MaximumClicks reached) 
    } 
} 

這將讓內的文件位置提供與指定擴展名匹配的文件夾然後,從收集的文件位置中加載一個隨機文件,名稱爲pictureBox1PictureBox

但是,這將不會避免以前添加到PictureBox的文件。如果你想避免重複,你可以創建一個新的int陣列或List<int>,並添加每ImageIndex已應用到PictureBox但建議去與List<int>,因爲它似乎更容易管理

List<int> AlreadyAdded = new List<int>(); 
redo: 
    Random Rnd = new Random(); 
    int ImageIndex = Rnd.Next(0, ItemsInFolder.Count); 
    if (AlreadyAdded.Contains(ImageIndex)) //Continue within this block if the item already exists in AlreadyAdded 
    { 
     goto redo; 
    }     
    AlreadyAdded.Add(ImageIndex); //Add ImageIndex to AlreadyAdded 

謝謝,
我希望對您有所幫助:)

+0

哇 - 這真的很好 - 我會嘗試下週的實施。正如我所說我是一個初學者,正在學習所有的術語和代碼去哪裏等你在代碼中的意見是非常有用的指向我在正確的方向 –

+0

@ jimbob - arooney我很高興你有你的問題已經解決。祝你今天愉快 :) –