0

我刚刚做了一个文件编辑器,它包含一个列表视图和一个文本框,当我从列表视图中选择和项目时,它出现在文本框中,文本是日语,当我选择日语文本或行时,它给出我一个错误:InvalidArgument:值'0'对'index'无效

你们能帮帮我吗?这是我的代码:

    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)
    {

    }
}

}

4

1 回答 1

1

在访问SelectedItems索引之前,您应该检查是否有任何项目被选中。

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

您可能还需要执行检查以确保在使用它之前SubItems有一个索引。1

于 2013-04-16T22:54:12.193 回答