无法在C#中填充列表框项目

时间:2014-12-06 06:47:35

标签: 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) 
{ }
}
}
}

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分:0)

我不确定你要做什么, 我想,你想要一个答案列表框,然后使用选定的索引int进行测试

因此加载框

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