2014-12-06 75 views
-1

我需要调用一个数组来获得下面的程序工作,但我无法弄清楚。我不知道什么是或不工作,因为我无法获得列表框填充。以下是代码。无法获取列表框项目来填充C#

using System.IO; 

namespace Test_Your_Knowledge__Game 
{ 
public partial class Form1 : Form 
{ 

    public Form1() 
    { 
     InitializeComponent(); 
    } 
     string questionOne; 
     string questionTwo; 
     string questionThree;     
     string firstQuestion = ("Who is a Silk Worm?"); 
     string secondQuestion = ("What does Sapience mean?"); 
     string thirdQuestion = ("What is Tainou?"); 
     string firstAnswer = ("Onycho"); 
     string secondAnswer = ("Wisdom"); 
     string thirdAnswer = ("A Wolf"); 
     int count = 0; 


    private void listBox1_SelectedIndexChanged(object sender, EventArgs e) 
    { 

    } 


    private void playButton_Click(object sender, EventArgs e) 
    { 

     string name; 
     string email; 


     name = nameTextBox.Text; 
     email = emailTextBox.Text; 

     StreamWriter outputFile; 
     outputFile = File.CreateText("Submission_List.txt"); 

     outputFile.WriteLine(name); 
     outputFile.WriteLine(email); 
     outputFile.WriteLine(count); 

     outputFile.Close(); 

    } 

    private void myArray(string[] strArray, string value) 
    { 

     bool found = false; 
     int index = 0; 
     int position = -1; 

     while (!found && index < strArray.Length) 

      if (strArray[index] == value) 
      { 
       found = true; 
       position = index; 
       index++; 
      } 

     string[] questionOneArray = { "1) Beebo", "2) Bael", "3) Onycho", "4) Ilion" }; 
     string[] questionTwoArray = { "1) Beauty", "2) Cursed", "3) Properity", "4) Wisdom" }; 
     string[] questionThreeArray = { "1) Imp", "2) Wolf", "3) Trow", "4) Elf" }; 

     foreach (string length in strArray) 

     { 
      try 
      { 
       if (questionOneArray[index] != firstAnswer) 
       { 

        questionOne = (firstQuestion + "" + questionOneArray); 
        listBox1.Items.Add(questionOne.ToString()); 
        found = false; 
       } 
       else 
       { 
        found = true; 
        count++; 
       } 


       { 
        if (questionTwoArray[index] != secondAnswer) 
        { 
         questionTwo = (secondQuestion + "" + questionTwoArray); 
         listBox1.Items.Add(questionTwo.ToString()); 
         found = false; 

        } 
        else 
        { 
         found = true; 
         count++; 
        } 

        { 
         if (questionThreeArray[index] != thirdAnswer) 
         { 
          questionThree = (thirdQuestion + "" + questionThreeArray); 
          listBox1.Items.Add(questionThree.ToString()); 
          found = false; 

         } 
         else 
         { 
          found = true; 
          count++; 
         } 

        } 
       } 
      } 
      catch (Exception) 
{ } 
} 
} 
} 
+0

这是很难理解你正在尝试做的。将需要更多的信息,如你想要什么输出,你得到什么,你卡在哪里? – Codeek 2014-12-06 11:04:30

+0

对不起....我试图在列表框中写下问题和数组,以便玩家可以选择答案。如果它是正确的,它会在回答三个问题并写入文件结束时计算得分。该程序运行,但我不能得到任何东西写入列表框。 – ssm 2014-12-06 14:09:32

+0

您意识到第**步**是将问题和答案放入数组中。对? – ja72 2014-12-06 16:35:14

回答

0

我不知道你正在尝试做的, 我猜,你想知道答案的一个列表框,然后使用选定的索引INT为你的测试

因此装载盒

public Form1() 
{ 
    InitializeComponent(); 
    string[] questionOneArray = { "1) Beebo", "2) Bael", "3) Onycho", "4) Ilion" }; 
    ForEach (string s in questionOneArray) 
    { 
     ListBox1.Items.Add(s); 
    }  
} 
+0

仍然不写入列表框。我被告知在写入文件之前必须调用数组,所以我假设答案在那里。我如何调用myArray使其他数组写入列表框? – ssm 2014-12-06 14:15:13

+0

string [] questionOneArray将此移至顶部。然后myArray(questionOneArray,“答案”) – doctorbobapplications 2014-12-06 14:38:42

+0

仍然不写入列表框... – ssm 2014-12-06 16:15:31

1

所有你需要做的就是使用DataSource财产

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    protected override void OnLoad(EventArgs e) 
    { 
     base.OnLoad(e); 

     string[] questions=new string[] { 
      "Who is a Silk Worm?", 
      "What does Sapience mean?", 
      "What is Tainou?" 
     }; 

     listBox1.DataSource=questions; 
    } 
} 

SCR

另外,您可以读取一个文件

List<string> questions=new List<string>(); 
var fs=File.OpenText("<path to file with questions here>"); 
while (fs.EndOfStream) 
{ 
    questions.Add(fs.ReadLine()); 
} 
fs.Close(); 

listBox1.DataSource=questions; 
+0

我们还没有学习基类。我有一种感觉,我的程序可能需要高级代码才能工作。有没有办法使用基本代码来做到这一点? – ssm 2014-12-06 16:49:25

+0

至少我现在写点东西了。谢谢。我会操纵它并让它起作用。欣赏你的时间。 – ssm 2014-12-06 17:06:17

+0

除非你了解继承和基类,否则你不能做表单。在这种情况下,您需要知道的只是将代码填充到由系统自动调用的'OnLoad()'方法中。 – ja72 2014-12-06 17:12:29