Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
{
    if (listView1.Items[12].Selected)
    {
        MessageBox.Show("Test");
    }
}


The above code works perfectly! But when I use my search box, it removes the ones that do not contain related text. So the indexes change, so item 12 is now, say, item 3. This is a problem. How can I fix this, thanks.
Posted

If you know the text that should be selected in the listview then you don't need to know the index at all. Just check the selected item's text.
C#
if( listview1.SelectedText == "YourStaticValue" )
{
   MessageBox.Show("Test");
}
 
Share this answer
 
Comments
ExcelledProducts 8-Nov-12 16:30pm    
Yes but the text is not always the same so you need to find the index.
fjdiewornncalwe 8-Nov-12 16:34pm    
I disagree. The OP is always looking for the same item that moves around and changes index. Therefore the text he's searching for is static.
ExcelledProducts 8-Nov-12 16:42pm    
That very well be true but with different users and different databases their are different text values so you must calculate the new index.
fjdiewornncalwe 8-Nov-12 16:43pm    
Agreed. That is why I called it YourStaticValue, not YourConstantValue.
ExcelledProducts 8-Nov-12 16:47pm    
But the static value may change between users.
I am not very familiar with C# but i think you could interpret this VB.NET Code. You need to do some math to figure out the difference between the two index's

VB
Public OldIndex As String
Public NewIndex As String
Public FinalIndex As String
Public ItemNumber As Integer

Know you only need to calculate the new index when you search it. So when you search it put this in your code.
VB
OldIndex=ListView1.Index
'Your search code
'After you search you get a new index so get the new one
NewIndex=ListView1.Index
'if the value is a negative number then the index is higher, if it is a positive number then the index is lower.
FinalIndex=NewIndex-OldIndex

If FinalIndex < 0 Then
FinalIndex=FinalIndex*-1
ItemNumber=12 + FinalIndex
ElseIf FinalIndex > 0 Then
FinalIndex= 12 - FinalIndex
End If

Their you go you should replace your code with this in the listView1_MouseDoubleClick
C#
private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (listView1.Items[ItemNumber].Selected)
            {
                MessageBox.Show("Test");
            }
        }

Hope this helps if it does not please don't complain, I tried my best :)
 
Share this answer
 
v2
I figured it out. This worked well for my purposes.

C#
private void listView1_DoubleClick(object sender, EventArgs e)
{
         ListViewItem people = listView1.FindItemWithText("Joe");
         ListViewItem cars = listView1.FindItemWithText("Toyota");
 
      if (people != null && listView1.FindItemWithText("Joe").Selected)
      {
      // Code here etc....
         Form1 form1 = new Form1();
         form1.Show();
         this.hide();
      }
      if (cars != null && listView1.FindItemWithText("Toyota").Selected)
      {
         // Code here etc....
         MessageBox.Show("Text");
      }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900