我正在尝试将所选项目从Type保存到文本文件(checklistbox)。
我还将主要演员(treeview)保存到文本文件
这部分有用,而且它们正在保存
但是,当我尝试打开文本文件以将数据重新填充回表单时,我收到错误:
索引超出了数组的范围。
以下是我的保存和打开功能的代码:
private void openDatabaseToolStripMenuItem_Click(object sender, EventArgs e)
{
// Open the file and upload the information
openFileDialog1.ShowDialog();
Stream s1 = openFileDialog1.OpenFile();
StreamReader reader = new StreamReader(s1);
saveDatabaseToolStripMenuItem.Enabled = true;
string infomovie = reader.ReadLine();
string[] select = infomovie.Split('#');
int nline = select.Length;
txtTitle.Text = select[0];
txtYear.Text = select[1];
txtDir.Text = select[2];
txtDur.Text = select[3];
int nactors = Convert.ToInt32(select[4]);
// put the number of actors in the file
for (int i = 0; i < nactors; i++)
{
tvActor.Nodes.Add(select[4 + i]);
}//end of for loop
int genre = Convert.ToInt32(select[5]);
// put the number of actors in the file
for (int i = 0; i < genre; i++)
{
cBoxType.Items.Add(select[5 + i]);
}//end of for loop
reader.Close();
}
private void saveDatabaseToolStripMenuItem_Click(object sender, EventArgs e)
{
//save data enter into form to text file
saveFileDialog1.ShowDialog();
Stream textOut = saveFileDialog1.OpenFile();
StreamWriter writer = new StreamWriter(textOut);
//creates string called infomovie
String infomovie = "";
//prints parameters
infomovie += txtTitle.Text + "#";
infomovie += txtYear.Text + "#";
infomovie += txtDir.Text + "#";
infomovie += txtDur.Text + "#";
//gets values from check list box
int genre = cBoxType.Items.Count;
infomovie += genre.ToString() + "#";
//gets values for tree view
int nactors = tvActor.Nodes.Count;
infomovie += nactors.ToString() + "#";
for (int i = 0; i < nactors; i++)
{
//convert treeview to string
infomovie += tvActor.Nodes[i].Text + ",";
}
//sep
infomovie += "#";
for (int i = 0; i < genre; i++)
{
if (cBoxType.GetItemCheckState(i) == CheckState.Checked)
{
//if statement to check which boxes are selected then converts to string
infomovie += cBoxType.Items[i].ToString() + ",";
}
}
//sep
infomovie += "#";
writer.WriteLine(infomovie);
writer.Close();
}
答案 0 :(得分:0)
您的actor count stored at index 5
以及genre count is at 4
和Actorlist at 6
以,
分隔,因此您必须在索引6处获取元素,然后将其拆分为{{1}然后循环并将其添加到,
tvActor.Nodes
同样适用于GenreList ..