C# 获取要在C中填充的列表框项时遇到问题#
我需要调用一个数组来让下面的程序工作,但我想不出来。我不确定什么是有效的还是无效的,因为我无法填充列表框。下面是代码C# 获取要在C中填充的列表框项时遇到问题#,c#,C#,我需要调用一个数组来让下面的程序工作,但我想不出来。我不确定什么是有效的还是无效的,因为我无法填充列表框。下面是代码 using System.IO; namespace Test_Your_Knowledge__Game { public partial class Form1 : Form { public Form1() { InitializeComponent(); } string questionOne;
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)
{ }
}
}
}
使用System.IO;
命名空间测试你的知识游戏
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
字符串问题1;
字符串问题2;
字符串问题三;
字符串firstQuestion=(“谁是丝虫?”);
字符串secondQuestion=(“智慧是什么意思?”);
字符串thirdQuestion=(“什么是泰诺?”);
字符串firstAnswer=(“Onycho”);
字符串secondAnswer=(“智慧”);
字符串thirdAnswer=(“狼”);
整数计数=0;
私有无效列表框1\u SelectedIndexChanged(对象发送方,事件参数e)
{
}
私有void播放按钮\u单击(对象发送者,事件参数e)
{
字符串名;
字符串电子邮件;
name=nameTextBox.Text;
email=emailTextBox.Text;
StreamWriter输出文件;
outputFile=File.CreateText(“Submission_List.txt”);
outputFile.WriteLine(名称);
outputFile.WriteLine(电子邮件);
outputFile.WriteLine(计数);
outputFile.Close();
}
私有void myArray(字符串[]字符串,字符串值)
{
bool-found=false;
int指数=0;
int位置=-1;
而(!found&&index
我不确定你想做什么,
我想,你需要一个答案列表框,然后使用选定的索引int进行测试
因此,我们需要装载箱子
public Form1()
{
InitializeComponent();
string[] questionOneArray = { "1) Beebo", "2) Bael", "3) Onycho", "4) Ilion" };
ForEach (string s in questionOneArray)
{
ListBox1.Items.Add(s);
}
}
您所要做的就是使用
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;
}
}
或者,您可以读取一个文件
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;
列出问题=新建列表();
var fs=File.OpenText(“”);
while(fs.EndOfStream)
{
添加(fs.ReadLine());
}
fs.Close();
listBox1.DataSource=问题;
很难理解你想做什么。你需要更多信息,比如你想要什么输出,你得到了什么,你被困在哪里?很抱歉……我正在尝试将问题和数组写在一个列表框中,以便玩家可以选择答案。如果答案正确,它将在最后计算的分数中添加一个回答完三个问题并写入一个文件。程序运行,但我无法将任何内容写入列表框。您知道第一步是先将问题和答案放入数组中。对吗?这不是我对if语句所做的吗?仍然没有写入列表框。有人告诉我,必须在写入之前调用数组它要归档,所以我假设答案在那里的某个地方。我如何调用myArray以便其他数组写入列表框?string[]questionOneArray将其移到顶部。然后myArray(questionOneArray,“答案”)我们还没有学习基类。我有一种感觉,我的程序可能需要高级代码才能工作。有没有一种方法可以使用基类代码来完成它?至少我现在有一些东西要写。谢谢。我会处理它并让它工作。感谢您的时间。除非您了解继承和基类,否则您不能使用表单。在这种情况下,您所需要知道的就是将填充值的代码放在系统自动调用的OnLoad()
方法中。