Cant you just use item?
foreach (ListViewItem item in items)
{
richTextBox1.Text = File.ReadAllText(item.Text);
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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;
}
Cant you just use item?
foreach (ListViewItem item in items)
{
richTextBox1.Text = File.ReadAllText(item.Text);
See the following code sample.
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)