如果双击列表视图项,请寻求帮助. [英] If a listview item is doubled clicked, help.

查看:91
本文介绍了如果双击列表视图项,请寻求帮助.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
{
    if (listView1.Items[12].Selected)
    {
        MessageBox.Show("Test");
    }
}



上面的代码可以完美地工作!但是,当我使用搜索框时,它会删除不包含相关文本的搜索框.因此索引改变了,因此第12项现在是第3项.这是一个问题.



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.

推荐答案

如果您知道应在列表视图中选择的文本,则无需知道完全没有索引.只需检查所选项目的文本即可.
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.
if( listview1.SelectedText == "YourStaticValue" )
{
   MessageBox.Show("Test");
}


我对C#不太熟悉,但我认为您可以解释此VB.NET代码.您需要做一些数学运算才能弄清楚两个索引之间的区别

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

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.

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


他们去了,您应该在listView1_MouseDoubleClick
中用此代码替换您的代码


Their you go you should replace your code with this in the listView1_MouseDoubleClick

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


我知道了.就我的目的而言,这很好.

I figured it out. This worked well for my purposes.

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");
      }
}


这篇关于如果双击列表视图项,请寻求帮助.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆