C# 用空数据填充的列表
由于我还是C语言的初学者,我在代码方面遇到了一些问题。 用户在富文本框中填写一些问题:C# 用空数据填充的列表,c#,C#,由于我还是C语言的初学者,我在代码方面遇到了一些问题。 用户在富文本框中填写一些问题: List<RichTextBox> boxForQuestions = new List<RichTextBox>(); for (int i = 0; i < numberOfQuestions; i++) { Label labelForEnumeration = new Label(); labelForEnumeration.Text = (i
List<RichTextBox> boxForQuestions = new List<RichTextBox>();
for (int i = 0; i < numberOfQuestions; i++)
{
Label labelForEnumeration = new Label();
labelForEnumeration.Text = (i + 1).ToString();
labelForEnumeration.Text = labelForEnumeration.Text + ".";
flowLayoutPanel1.Controls.Add(labelForEnumeration);
RichTextBox tempBox = new RichTextBox();
tempBox.Size = new Size(650,60);
tempBox.Font = new System.Drawing.Font(FontFamily.GenericSansSerif,11.0F);
flowLayoutPanel1.Controls.Add(tempBox);
boxForQuestions.Add(tempBox);
}
List boxForQuestions=new List();
for(int i=0;i
我将这些问题添加到字符串列表中:
List<string> listOfQuestions = new List<string>();
for (int i = 0; i < numberOfQuestions; i++)
{
listOfQuestions.Add(boxForQuestions[i].Text);
}
问题列表=新列表();
for(int i=0;i
现在,我尝试在这个函数中将它们随机分组:
List<List<string>> questions = new List<List<string>>();
static Random rnd = new Random();
public void randomizingQuestions()
{
for (int i = 0; i < numberOfGroups; i++)
{
List<string> groupOfQuestions = new List<string>();
for (int j = 0; j < numberOfQuestionsPerGroup; j++)
{
int index = rnd.Next(listOfQuestions.Count - 1);
string oneQuestion = listOfQuestions[index];
foreach (string temp in groupOfQuestions)
{
if (temp != oneQuestion)
{
groupOfQuestions.Add(oneQuestion);
}
}
}
questions.Add(groupOfQuestions);
}
}
列出问题=新建列表();
静态随机rnd=新随机();
公共无效随机化问题()
{
对于(int i=0;i
但是,列表是空的,因为当我想将这些问题添加到PDF文件中时,没有在纸上显示:
Document document = new Document(iTextSharp.text.PageSize.LETTER, 20, 20, 42, 35);
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(pdfFile.FileName, FileMode.Create));
document.Open();
document.Add(new Paragraph("TEST"));
foreach (List<string> question in questions)
{
document.NewPage();
foreach (string field in question)
{
document.Add(new Paragraph(field));
}
}
document.Close();
Document Document=新文档(iTextSharp.text.PageSize.LETTER,20,20,42,35);
PdfWriter writer=PdfWriter.GetInstance(文档,新文件流(pdfFile.FileName,FileMode.Create));
document.Open();
文件。添加(新段落(“测试”);
foreach(在问题中列出问题)
{
document.NewPage();
foreach(有问题的字符串字段)
{
添加(新段落(字段));
}
}
document.Close();
你能告诉我我错了什么吗?问题是,groupOfQuestions在循环的开头是空的,因此其中没有要枚举的字符串,因此,每个循环的内部语句永远不会执行。您可以改为使用:
if(!groupOfQuestions.Contains(oneQuestion)
{
groupOfQuestions.Add(oneQuestion);
}
顺便说一下,如果执行了.Add命令,则会出现以下异常:
Collection was modified; enumeration operation may not execute.
循环时,您在
问题中收到了什么?尝试调试randomizingQuestions()
方法。我猜groupOfQuestions.Add(一个问题)代码>此行可能不会执行,因为每次为
循环输入第二个,因为您正在更新它,所以问题组中不会有项目。我现在明白我的错误了,但你能给我解释一下你的最后一句话吗。如果您开始对集合进行迭代(foreach Stetems实际调用GetEnumarator(),然后在枚举器iinstace和Current上调用Next(),以提供Instance(在这种情况下为temp)。如果您在使用该枚举器时向该集合添加项或从该集合中删除项,则会引发此异常,因为枚举器已过时(不再反映完整集合中的项目)您有更好的解决方案吗?您是否知道如何在列表中的每个字符串之前输入数字顺序?您可以使用SortedList,并通过SortedList.add将字符串添加到列表中(new Random().NextDouble(),myString).在本例中,您排序的列表已经随机分布。您认为我的代码好还是应该使用您的想法?每个字符串之前的数字顺序如何?