2014-12-12 3 views
0

Я пытаюсь сохранить выбранные элементы из типа в текстовый файл (контрольный список).
Я также сохраняю главных действующих лиц (treeview) в текстовый файл
Эта часть работает, и они сохраняются.
Однако, когда я пытаюсь открыть текстовый файл, чтобы заселить данные обратно в форму,
я получаю сообщение об ошибке:Сохраните значения из контрольного списка в текстовый файл, затем откройте текстовый файл и снова запишите сохраненные значения в форму

Index was outside the bounds of the array.

Ниже мой код для сохранения и открытых функций:

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

Пожалуйста, расскажите нам, какие строки вызывает ошибку! Вы пробовали отладчик? (При ошибке просто выберите «break» и проверьте значения ..! – TaW

+2

В какой строке вы принимаете ошибку? – Mehrad

+0

tvActor.Nodes.Add (выберите [4 + i]); << Вот где ошибка – user3659907

ответ

0

ваш actor count stored at index 5 и genre count is at 4 и Actorlist at 6 и которая отделена от , так вы, чтобы получить элемент с индексом 6, а затем разделить их , и сделать петлю над ним и добавить его в tvActor.Nodes

string[] actorlist= select[6].Split(','); 
// Then Do loop here 

То же самое касается GenreList ..