How can I get the selected index of listview in selectedindexchanged event ?

Chocolade 536 Reputation points
2021-12-30T21:16:12.95+00:00

The selectedindex is not exist.

Now I'm using SelectedIndices[0] but that will select each time the first item in the listview and not the item I clicked on in the listview.

My whole highlighting and splitting words is still not working 100% but first I need to solve this problem.
The main goal is to select item in the listview read this item(file) content to richTextBox1 then to highlight the words in the richTextBox1 in yellow.

void lvnf_SelectedIndexChanged(object sender, EventArgs e)
        {
            results = new List<int>();
            richTextBox1.Text = File.ReadAllText(listViewCostumControl1.lvnf.Items[listViewCostumControl1.lvnf.SelectedIndices[0]].Text);
            FileInfo fi = new FileInfo(listViewCostumControl1.lvnf.Items[listViewCostumControl1.lvnf.SelectedIndices[0]].Text);
            lblfilesizeselected.Text = ExtensionMethods.ToFileSize(fi.Length);
            lblfilesizeselected.Visible = true;
            filePath = Path.GetDirectoryName(fi.FullName);
            string words = textBox1.Text;
            string[] splittedwords = words.Split(new string[]
            { ",," }, StringSplitOptions.None);
            foreach (string myword in splittedwords)
            {
                HighlightPhrase(richTextBox1, myword, Color.Yellow);
                lblviewerselectedfile.Text = results.Count.ToString();
                lblviewerselectedfile.Visible = true;
                if (results.Count > 0)
                {
                    numericUpDown1.Maximum = results.Count;
                    numericUpDown1.Enabled = true;
                    richTextBox1.SelectionStart = results[(int)numericUpDown1.Value];
                    richTextBox1.ScrollToCaret();
                }
            }
        }

The highlighting method :

void HighlightPhrase(RichTextBox box, string phrase, Color color)
        {
            int pos = box.SelectionStart;
            string s = box.Text;
            for (int ix = 0; ;)
            {
                int jx = s.IndexOf(phrase, ix, StringComparison.CurrentCultureIgnoreCase);
                if (jx < 0)
                {
                    break;
                }
                else
                {
                    box.SelectionStart = jx;
                    box.SelectionLength = phrase.Length;
                    box.SelectionColor = color;
                    ix = jx + 1;
                    results.Add(jx);
                }
            }
            box.SelectionStart = pos;
            box.SelectionLength = 0;
        }
Developer technologies | Windows Forms
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Ken Tucker 5,861 Reputation points
    2021-12-30T22:28:49.18+00:00

    Cant you just use item?

                 foreach (ListViewItem item in items)
                 {
                     richTextBox1.Text = File.ReadAllText(item.Text);
    
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2021-12-30T22:40:22.223+00:00

    See the following code sample.

    161513-listview.png

    1 person found this answer helpful.
    0 comments No comments

  2. Castorix31 90,686 Reputation points
    2021-12-30T21:32:16.797+00:00

    You get ListView.SelectedListViewItemCollection , like in the ListView1_SelectedIndexChanged_UsingItems sample
    (it will loop on the selected item only if the ListView is not MultiSelect)


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.