2013-04-16 29 views
0

我只是做了一個文件編輯器,它包含一個列表視圖和一個文本框,我做了這個,當我從列表視圖中選擇和項目它出現在文本框中,文本是日語,並且當我選擇日文文本或行,它給了我一個錯誤:InvalidArgument:值'0'是無效'索引'c#索引無效參數,空值

你們可以幫我嗎?這是我的代碼:

private void Form1_Load(object sender, EventArgs e) 
    { 

    } 

    private void menuItem2_Click(object sender, EventArgs e) 
    { 
     listView1.Items.Clear(); 
     textBox1.Text = ""; 
     menuItem12.Text = "file type is: "; 
     OpenFileDialog ofd = new OpenFileDialog(); 
     ofd.Title = "Open File"; 
     ofd.Filter = "All Files (*.*)|*.*"; 
     if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
     { 
      path = ofd.FileName; 
      BinaryReader br = new BinaryReader(File.OpenRead(path), Encoding.GetEncoding("SHIFT-JIS")); 
      foreach (char mychar in br.ReadChars(4)) menuItem12.Text += mychar; 
      if (menuItem12.Text != "file type is: TXTD") 
      { 
       MessageBox.Show("This is not a TXTD file...", "Sorry", MessageBoxButtons.OK, MessageBoxIcon.Error); 
       return; 
      } 
      else 
      { 
        MessageBox.Show("File opened Succesfully!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information); 
        br.BaseStream.Position = 0x8; 
        int Pntrnum = br.ReadInt16(); 
        menuItem11.Visible = true; 
        menuItem11.Text = Pntrnum.ToString(); 
        List<int> offsets = new List<int>(); 
        br.BaseStream.Position = 0x10; 
        for (int i = 0; i < Pntrnum; i++) 
        { 
         offsets.Add(br.ReadInt32()); 
        } 
        Dictionary<int, string> values = new Dictionary<int, string>(); 
        for (int i = 0; i < offsets.Count; i++) 
        { 
         int currentOffset = offsets[i]; 

         int nextOffset = (i + 1) < offsets.Count ? offsets[i + 1] : (int)br.BaseStream.Length; 

         int stringLength = (nextOffset - currentOffset - 1)/2; 

         br.BaseStream.Position = currentOffset; 

         var chars = br.ReadChars(stringLength); 
         values.Add(currentOffset, new String(chars)); 
        } 

        foreach (int offset in offsets) 
        { 
         listView1.Items.Add(offset.ToString("X")).SubItems.Add(values[offset]); 
        } 

        br.Close(); 
        br = null; 
      } 
     } 
     ofd.Dispose(); 
     ofd = null; 
    } 

    private void menuItem4_Click(object sender, EventArgs e) 
    { 
     this.Close(); 
    } 

    private void menuItem6_Click(object sender, EventArgs e) 
    { 
     BinaryWriter bw = new BinaryWriter(File.OpenWrite(path)); 
     int number_pointers = Convert.ToInt32(menuItem11.Text); 
     Encoding enc = Encoding.GetEncoding("SHIFT-JIS"); 
     bw.BaseStream.Position = 0x10; 
     int curr_pointer = 4 + number_pointers * 4; 
     for (int i = 0; i < number_pointers; i++) 
     { 
      bw.Write(curr_pointer); 
      curr_pointer += enc.GetByteCount(listView1.Items[i].SubItems[1].Text) + 1; 
     } 
     for (int i = 0; i < number_pointers; i++) 
      bw.Write(enc.GetBytes(listView1.Items[i].SubItems[1].Text + '\0')); 

     bw.Flush(); 
     bw.Close(); 
     bw = null; 
    } 

    private void menuItem8_Click(object sender, EventArgs e) 
    { 
     SaveFileDialog sfd = new SaveFileDialog(); 
     sfd.InitialDirectory = Application.ExecutablePath; 
     sfd.Filter = "Text Files (*.txt)|*.txt"; 
     sfd.Title = "Save Text file"; 
     DialogResult result = sfd.ShowDialog(); 
     if (result == DialogResult.Cancel) 
      return; 
     StreamWriter wwrite = new StreamWriter(sfd.FileName, false, Encoding.GetEncoding("SHIFT-JIS")); 
     for (int i = 0; i < listView1.Items.Count; ++i) 
     { 
      string Ptrs = listView1.Items[i].SubItems[0].Text; 
      string Strs = listView1.Items[i].SubItems[1].Text; 
      wwrite.WriteLine(i.ToString() + " > " + Ptrs + " > " + Strs); 
     } 
     wwrite.Close(); 
    } 

    private void menuItem5_Click(object sender, EventArgs e) 
    { 
     MessageBox.Show("TXTD Editor by Omarrrio v0.1 Alpha\n2013 Copyrighted crap and whatever", "About...", MessageBoxButtons.OK, 
         MessageBoxIcon.Information); 
    } 

    private void listView1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     textBox1.Text = listView1.SelectedItems[0].SubItems[1].Text; 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     listView1.SelectedItems[0].SubItems[1].Text = textBox1.Text; 
    } 

    private void menuItem12_Click(object sender, EventArgs e) 
    { 

    } 
} 

}

+0

請問您只需發佈相關代碼? –

+0

或者至少告訴我們你得到異常的行 – Steve

+0

對不起,這是'private void listView1_SelectedIndexChanged'一個 – Omarrrio

回答

1

之前訪問SelectedItems指數,你應該檢查是否有選擇任何產品。

private void button1_Click(object sender, EventArgs e) 
{ 
    if(listView1.SelectedItems.Count > 0) 
     textBox1.Text = listView1.SelectedItems[0].SubItems[1].Text; 
} 

您可能還需要進行檢查,以確保SubItems1索引使用它之前。

+0

不客氣:) – keyboardP