Question

i just made a file editor, it contains a listview and a textbox, i made that when i select and item from the list view it appears in the textbox, the text is japanese, and when i select a japanese text or line, it gives me an error: InvalidArgument: Value '0' is not valid for 'index'

can you guys help me ? this is my code:

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

    }
}

}

Was it helpful?

Solution

Before accessing the SelectedItems index, you should check if there are any items selected.

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

You may also want to perform a check to ensure that SubItems has an index of 1 before using it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top