同时选择多个列表视图控件的同一行 [英] Selecting same row of multiple listview controls at the same time

查看:149
本文介绍了同时选择多个列表视图控件的同一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我有一个包含5个不同的ListView控件的窗体。我想在这里实现两件事情,我已经变得有点憋屈......

In my application I have a form that contains 5 different listview controls. I am trying to achieve two things here which I have become a little stuck on...

1 有没有一种方法,我可以使它所以当我点击ListView1的排3(例如),它选择行3对所有时,ListView控件?我在我的ListView1的的_SelectedIndexChanged事件,正常工作下code组,但是,我想使它所以如果用户点击任何的ListView ROW3,它改变了所有的ListView控件排3。

1. Is there a way I can make it so when I click on row 3 (for example) of Listview1, it selects row 3 for all listview controls? I have the following code set in my _SelectedIndexChanged event of Listview1 which works correctly, however, I want to make it so if the user clicks on row3 of any listview, it changes all listview controls to row 3.

Private Sub lsvRegisters_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lsvRegisters.SelectedIndexChanged
    Try
        If Not lsvRegisters.SelectedItems.Count = 0 Then
            Dim index As Integer = lsvRegisters.SelectedIndices(0)

            If lsvRegister_Hardware.Items.Count > 0 Then lsvRegister_Hardware.Items(index).Selected = True
            If lsvRegister_Software.Items.Count > 0 Then lsvRegister_Software.Items(index).Selected = True
            If lsvRegister_Processes.Items.Count > 0 Then lsvRegister_Processes.Items(index).Selected = True
            If lsvRegister_System.Items.Count > 0 Then lsvRegister_System.Items(index).Selected = True
            If lsvRegister_Misc.Items.Count > 0 Then lsvRegister_Misc.Items(index).Selected = True
        End If

    Catch ex As Exception
        CreateLog("Module: lsvRegisters_SelectedIndexChanged()" & vbNewLine & "Exception Error: " & ex.Message)
        MsgBox("Exception Error: " & ex.Message, MsgBoxStyle.Critical, "Module: lsvRegisters_SelectedIndexChanged()")
    End Try
End Sub

其次:

2 有没有一种方法我可以使它因此所有的ListView控件具有高亮条为活动颜色?目前,它只能显示一个蓝色亮条和其他人在灰暗的焦点ListView控件。我想(如果可能)拥有所有的ListView控件显示为蓝色不管它焦点。

2. Is there a way I can make it so all listview controls have the highlight bar as active colour? Currently it only shows the focused listview control with a blue highlight bar and the others in a dull grey. I want to (if possible) have all listview controls show in blue regardless if it has focus or not.

任何帮助AP preciated。
谢谢

Any help appreciated. Thanks

更新
列表视图控件的多重选择现在工作的要求,然而,一旦一个ListView失去焦点,只保留第一个单元格高亮显示,如下图所示:

Update The multiple selection of listview controls is now working as requested, however, once a listview loses focus, it only keeps the first cell highlighted as shown below:

我的code设立在ListView控件来自数据集。我已经证明所述第一列表视图,但所有这些都相同。

My code for setting up the listview controls comes from a dataset. I have demonstrated the first listview, but all of them are the same.

        Try
            QueryString = "SELECT * FROM Registers WHERE StoreID = '" & _StoreCode & "'"
            Dim ExQry As New MySqlCommand(QueryString, MySQLConn)

            Dim da As New MySqlDataAdapter(ExQry)
            da.Fill(dsStoreDetail, "StoreDetail")
            Dim tempDT As DataTable = dsStoreDetail.Tables("StoreDetail")

            If dsStoreDetail.Tables.Count > 0 And dsStoreDetail.Tables(0).Rows.Count > 0 Then
                For x = 0 To (dsStoreDetail.Tables(0).Rows.Count - 1)
                    Dim lvi_RegistersItem As ListViewItem = lsvRegisters.Items.Add(tempDT.Rows(x)("Online").ToString)
                    lvi_RegistersItem.SubItems.Add(tempDT.Rows(x)("Lane").ToString)
                    lvi_RegistersItem.SubItems.Add(tempDT.Rows(x)("Host_Name").ToString)
                    lvi_RegistersItem.SubItems.Add(tempDT.Rows(x)("Reg_Type").ToString)
                    lvi_RegistersItem.SubItems.Add(tempDT.Rows(x)("Operator").ToString)
                    lvi_RegistersItem.SubItems.Add(tempDT.Rows(x)("Register_Locked").ToString)

                Next
            Else
                lblEmptyString.Visible = True
            End If
            dsStoreDetail.Clear()
            tempDT = Nothing

        Catch ex As Exception
            CreateLog("Module: LoadStoreData()" & vbNewLine & "Exception Error: " & ex.Message)
            MsgBox("Exception Error: " & ex.Message, MsgBoxStyle.Critical, "Module: LoadStoreData()")
            lblEmptyString.Visible = True
        End Try

我已经将所有的ListView控件FullRowSelect = true,但仅在设计时 - 不知道如果该事项

I have set all listview controls to FullRowSelect = True but only at design time - not sure if that matters.

推荐答案

对,这是我对这个结论,它会用的东西和你不同的名称进行,我害怕。

Right, this is my conclusion for this and it will be made with things named differently from yours, I'm afraid.

要开始了,所有的设置列表视图有theese设置:

To Start off, setup all ListViews to have theese settings:

HideSelection = True
MultiSelect = false

这是我摆在所有列表视图中选择指数的变化。

This is what I have placed in all ListViews selected Index changed.

Private Sub ListView2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView2.SelectedIndexChanged
    If Not Working And ListView2.SelectedIndices.Count <> 0 Then
       UpdateAllListViewes(ListView2.SelectedIndices)
    End If
End Sub

工作是用于避免对UpdateAllListViews函数不必要调用私有布尔。 UpdateAllListViews发送所有列表框与指数的UpdateSelectedIndex功能点亮。正如你可以看到虽然这执行工作布尔被更新为True。这可以被跳过,但如果出现问题将调试变成一场噩梦。

Working is a Private Boolean that is used to avoid unecessary calls to the UpdateAllListViews function. UpdateAllListViews sends all listboxes to the UpdateSelectedIndex function with the Index to light up. As you can see the Working Boolean is updated to True while this executes. This can be skipped, but debugging will turn into a nightmare if something goes wrong.

Private Sub UpdateAllListViewes(ByVal Indexes As ListView.SelectedIndexCollection)
        Working = True
        UpdateSelectedIndex(ListView2, Indexes(0))
        UpdateSelectedIndex(ListView3, Indexes(0))
        UpdateSelectedIndex(ListView4, Indexes(0))
        UpdateSelectedIndex(ListView5, Indexes(0))
        Working = False
    End Sub

最后如何更新选定的指标:

And finally how to update the selected indexes:

Private Sub UpdateSelectedIndex(ByVal lv As ListView, ByVal Index As Integer)
    For i As Integer = 0 To lv.Items.Count - 1
        If i = Index Then
            lv.Items(i).Selected = True
            lv.Items(i).BackColor = Color.DodgerBlue
            lv.Items(i).ForeColor = Color.White
        Else
            lv.Items(i).Selected = False
            lv.Items(i).BackColor = Color.White
            lv.Items(i).ForeColor = Color.Black
        End If
    Next
End Sub

宝蓝​​是一个ListView的SelectedIndex的颜色。这应该解决这两个问题,1和2

DodgerBlue is the SelectedIndex color for a ListView. This should solve both question 1 and 2.

这篇关于同时选择多个列表视图控件的同一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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