2012-04-05 2 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과의 ImageList에서의 사진 사이의 상관 관계를 만들 수

때 정지

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

에서 시작하기 때문에 오류 "하나 오프"를 조심하고, 어떻게하다 당신은 당신의'ImageList'을 채우고 있습니까? – Justin

+2

ImageList에 이미지가 포함 된 방법에 대해 텍스트 파일의 라인에 1 대 1로 질문합니까? 첫 번째 질문은 첫 번째 이미지, 두 번째 질문은 두 번째 이미지 등입니다. 그렇지 않다면, 어떻게 그들을 연관 시키나요? –

답변

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 변수를 표시하고 있음을 의미합니다. 또한, 질문에 대한 이미지 인덱스를 할당 할 때 당신이 당신의`questions` 수집을 작성하는 방법 색인은 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 루프가있었습니다. 변수를보고있는 디버거와 함께 코드를 단계별로 실행하는 것이 좋습니다!이 모든 것이 귀하의 원래 질문에 응답되기를 바랍니다. –