2012-04-05 10 views
2

ランダム質問を画像で表示するC#プログラムを作成しています。ランダム化テストの質問に画像を関連付ける

このテストには10​​の質問があります。私はまた、ImageListに10個の画像を追加しました。私の質問はランダムに選択され、私が解決する各クイズに表示されます。私はそれぞれの疑問をその絵にしたいと思っています。

私は、ファイルから読み込むの質問のためのコレクションを持っている:

Collection<question> questions = new Collection<question>(); 
StreamReader sr = new StreamReader("quiz.txt"); 
while (!sr.EndOfStream) 
{ 
    question i = new question(); 
    i.text = sr.ReadLine(); 
    questions.Add(i); 
} 
sr.Close(); 

Random r = new Random(); 
int x = r.Next(questions.Count); 

を私はツールボックスからImageListコントロールを追加しました。その後、イメージコレクションエディタを使用してイメージを追加しました。 ?私が使用したコードの場合:a > imageList1.Images.Count

がどのように私はcurrent_questionとイメージリストからその画像との相関関係を作ることができたときに

pictureBox1.Image = imageList1.Images[a]; 

これは停止しますか

UPDATE

public class question 
{ 
public bool displayed = false; 
public string text, answer1, answer2; 
} 

private void button1_Click_1(object sender, EventArgs e) 
{ 
     string line = questions[current_question].text; 
     int delimiter = line.IndexOf(':'); 
     int imageIndex = int.Parse(line.Substring(0, delimiter)); 
     string questionText=line.Substring(delimiter + 1); 
     pictureBox1.Image = imageList1.Images[imageIndex];//I still have problems with  
                  //images 
     if (nr > questions.Count) 
           { 
            button1.Enabled = false; 
            } 
     else 
     { 
      Random r = new Random(); 
      int x; 
      do { x = r.Next(questions.Count); } 
        while (questions[x].displayed == true); 
      textBox1.Text = questionText;// now it doesn't appear the index;thank you 
      radioButton1.Text = questions[x].answer1; // is not from the current 
                 // question 
      radioButton2.Text = questions[x].answer2;// is not from the current 
                // question 
      questions[x].displayed= true; 
      current_question = x; 
     } 
     } 
+0

どのようにあなたの 'questions'コレクションを充填され、そしてどのようにしているから始まるので、また、「オフ一つ」あなたの質問に画像インデックスを割り当てるエラーの用心あなたは 'ImageList'を満たしていますか? – Justin

+2

ImageListに画像がどのように含まれているかについて、テキストファイルの行に1対1の質問をしますか?同様に、最初の質問は最初の画像、2番目の質問は2番目の画像などです。そうでない場合は、どのように関連付けるのですか? –

答えて

2

あなたのquiz.txtファイル内の画像のインデックスを含む試みることができる:

3:バーにfooが何ですか?
10:どのようにウィジェットを追加できますか?
4:なぜfoo上のバーを選ぶのですか?

その後、あなたはランダムな質問は、あなたがイメージインデックスと質問のテキスト抽出ができ選んで、あなたのプログラム内のポイントで:それではquestionText

int x = r.Next(questions.Count); 
// intrebari is a variable of class 'question' 
string line = intrebari[x].text; 
// find the colon 
int delimiter = line.IndexOf(':'); 
// the image index is left of the colon 
int imageIndex = int.Parse(line.Substring(0, delimiter)); 
// and the question itself is right of the colon 
string questionText = line.Substring(delimiter + 1); 
// set the image in the picture box 
pictureBox1.Image = imageList1.Images[imageIndex]; 

を使用すると、表示文字列であり、 imageIndex正しい画像を選択するのに、imageList1.Images[imageIndex]を使用してPictureBoxに割り当てることができます。

まだ質問と画像インデックスが表示されている場合は、questionText変数を表示する必要があるときにline変数を表示していることを意味します。インデックスが0

編集

private void button1_Click_1(object sender, EventArgs e) 
{ 
    if (nr > questions.Count) 
    { 
     button1.Enabled = false; 
    } 
    else 
    { 
     Random r = new Random(); 
     int x; 
     do { x = r.Next(questions.Count); } 
       while (questions[x].displayed == true); 

     string line = questions[x].text; 
     int delimiter = line.IndexOf(':'); 
     int imageIndex = int.Parse(line.Substring(0, delimiter)); 
     string questionText=line.Substring(delimiter + 1); 
     pictureBox1.Image = imageList1.Images[imageIndex];//I still have problems with   

     textBox1.Text = questionText;// now it doesn't appear the index;thank you 
     radioButton1.Text = questions[x].answer1; // is not from the current 
                // question 
     radioButton2.Text = questions[x].answer2;// is not from the current 
               // question 
     questions[x].displayed= true; 
     current_question = x; 
    } 
    } 
+0

@Bodgan、私はあなたのコードを見ることができないときに何がうまくいかないのか分からないので、あなたの質問の下で '編集'ボタンをクリックし、関連するコードを含める必要があります。 –

+0

@Brad_Rem、私は '編集'ボタンを使用しました。ありがとうございました。 – Bogdan

+0

@ Bogdan、私の編集を参照してください。問題はあなたのcurrent_questionとdo-whileループでした。あなたは最後の質問だったcurrent_questionのためのイメージを持っていました、そしてあなたのdo-whileは新しい質問にループします。変数を見ているデバッガを使ってコードを1行ずつ進めると便利です!このすべてがあなたの元の質問に答えることを願っています。 –

関連する問題