由於我仍然是C#的初學者,我在代碼方面存在一些問題。 用戶正在填寫富文本框的一些問題:填充空數據的列表
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<string> listOfQuestions = new List<string>();
for (int i = 0; i < numberOfQuestions; i++)
{
listOfQuestions.Add(boxForQuestions[i].Text);
}
現在,我想他們隨機在一些在這個功能組:
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);
}
}
但是,該列表是空的,因爲當我要添加這些問題的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();
你能告訴我我錯了什麼嗎?
你'questions'收到什麼而遍歷?嘗試調試'randomizingQuestions()'方法。我想'groupOfQuestions.Add(oneQuestion);'這行可能沒有執行,因爲每次你輸入第二個'for'循環時,'groupOfQuestions'中就不會有項目,因爲你正在創建它。從你的結尾檢查是否發生了這種情況。 –