2017-03-24 4 views
0

アプリケーションを作成すると、テキストファイルからテキストファイルのパスが取り出され、リストボックスにロードされます。リストボックスでクリックすると、テキストファイルのコンテンツがスクリプトエディタウィジェットはtextEditorControl1と呼ばれます。C#範囲外:listBoxesにtextFileの内容を書き出す

物がございます。テキストファイル名をホストするリストボックスから項目を削除した後、リストボックス内の別の項目をクリックします。それは私にエラーを与える:

An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll

Additional information: Index was out of range. Must be non-negative and less than the size of the collection.

string fullFileName2 = selectedScripts[listBox3.SelectedIndex];

List<String> fullFileName; 
    List<String> fullFileName2; 
List<string> selectedScripts = new List<string>(); 

     public void listBox3_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      if (listBox3.SelectedIndex >= 0) 
      { 
       string fullFileName2 = selectedScripts[listBox3.SelectedIndex]; 
       textBox3.Text = fullFileName2; 
       string File1 = fullFileName2; 
       string text = System.IO.File.ReadAllText(File1); 
       textEditorControl1.Text = text; 
       textEditorControl1.Refresh(); 
      } 
      else 
      { 

      } 

     private void materialFlatButton10_Click(object sender, EventArgs e) 
     { 
      OpenFileDialog OpenFileDialog1 = new OpenFileDialog(); 
      OpenFileDialog1.Multiselect = true; 
      OpenFileDialog1.Filter = "Text Files|*.txt|All Files|*.*|Lua Files|*.lua"; 
      OpenFileDialog1.Title = "Select a Text/Lua File"; 
      if (OpenFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
      { 
       fullFileName2 = new List<String>(OpenFileDialog1.FileNames); 


       foreach (string s in OpenFileDialog1.FileNames) 
       { 
        listBox3.Items.Add(Path.GetFileName(s)); 
        selectedScripts.Add(s); 
       } 


      } 
     } 



private void deleteFromListToolStripMenuItem_Click(object sender, EventArgs e) 
    { 
     label4.Text = " "; 
     textBox3.Text = ""; 
     IDocument document = textEditorControl1.Document; 
     document.Remove(0, document.TextLength); 
     textEditorControl1.Refresh(); 
     selectedScripts.Clear(); 

     for (int i = listBox3.SelectedIndices.Count - 1; i >= 0; i--) 
     { 
      listBox3.Items.RemoveAt(listBox3.SelectedIndices[i]); 
     } 
    } 

答えて

1

にあなたはSelectedScriptsをクリアしていて、あなたが何かをクリックしたときに、あなたはインデックスlistBox3でSelectedScriptsのアイテムにアクセスしようとしています。 SelectedIndexが、この時点でSelectedScriptsは空です。

私はあなたのdeleteメソッドは、このされるべきだと思う:あなたがリストに追加する1つのクラスオブジェクトにすべてのデータをカプセル化することができるようにUI ListBoxコントロールは、クラスを取ることができること

private void deleteFromListToolStripMenuItem_Click(object sender, EventArgs e) 
{ 
    label4.Text = " "; 
    textBox3.Text = ""; 
    IDocument document = textEditorControl1.Document; 
    document.Remove(0, document.TextLength); 
    textEditorControl1.Refresh(); 

    for (int i = listBox3.SelectedIndices.Count - 1; i >= 0; i--) 
    { 
     selectedScripts.RemoveAt(listBox3.SelectedIndices[i]); 
     listBox3.Items.RemoveAt(listBox3.SelectedIndices[i]); 
    } 
} 

注意。

+0

ああ、そのオプションから新しいテキストファイルの内容をスクリプトエディタに作成します。 –

+0

@RohanPas私はあなたの状況をうまく解決するはずの回答を追加しました。 クラスのオブジェクトをリストで使用する方法については、次のビデオを参照してください。https://www.youtube.com/watch?v=_leuGAsWDis – john

+0

.OrderByDescendingはSelectedIndicesの特徴ではありません。ビデオをありがとう。 –

関連する問題