首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >按按钮上下移动列表视图

按按钮上下移动列表视图
EN

Stack Overflow用户
提问于 2012-01-14 06:24:48
回答 3查看 8K关注 0票数 0

我试图使用两个按钮(上下)在列表视图中上下移动,突出显示每个项目,具体取决于它在列表视图中的方向。

这是我目前的代码:

代码语言:javascript
运行
复制
Private Sub cmdDown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDown.Click
    If cmdDown.Tag <> totalRecordsReturned Then
        cmdDown.Tag += 1
        ListView1.Items(cmdDown.Tag).Selected = True
        ListView1.Focus()
    End If
End Sub

这确实有效,但,但每次我按下按钮时,它都会选择更多的项目。我能做些什么来纠正这个问题呢?

大卫

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-01-14 11:01:11

在设计视图中,选择Multiselect = False

或者在代码中添加这一行

代码语言:javascript
运行
复制
ListView1.MultiSelect = False

像这样

代码语言:javascript
运行
复制
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ListView1.MultiSelect = False
End Sub

Private Sub cmdDown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles cmdDown.Click
    If cmdDown.Tag <> totalRecordsReturned Then
        cmdDown.Tag += 1
        ListView1.Items(cmdDown.Tag).Selected = True
        ListView1.Focus()
    End If
End Sub
票数 1
EN

Stack Overflow用户

发布于 2013-04-10 00:33:41

我想你可以用这个解决方案。使用listview keyup/keydown:

代码语言:javascript
运行
复制
Private Sub lvSearch_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles lvSearch.KeyDown
        If e.KeyCode = Keys.Down Then

            If lvSearch.SelectedItems(0).Index < (lvSearch.Items.Count - 1) Then

                txtName.Text = lvSearch.Items(lvSearch.SelectedItems(0).Index + 1).SubItems(1).Text

            End If
        End If
    End Sub

    Private Sub lvSearch_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles lvSearch.KeyUp
        If e.KeyCode = Keys.Up Then
            If lvSearch.SelectedItems(0).Index >= 0 Then

                txtName.Text = lvSearch.Items(lvSearch.SelectedItems(0).Index).SubItems(1).Text

            End If
        End If
    End Sub
票数 0
EN

Stack Overflow用户

发布于 2016-09-25 16:13:01

这是非常有帮助的,我在我的项目中使用了它,但它有点不同。我有一个列表视图,其中第一列是一个固定的索引,这样用户就可以知道列表中有多少项,并且可以按编号引用它们。在上下移动项时,这是一个问题,因为第1列中的索引号需要保持不变,并且只更改第2列中的数据。我编写了下面的类,它还支持添加、删除和重命名第2列项。

代码语言:javascript
运行
复制
Public Class class_ListViewControl

Public ListViewControl_DebugMode As Boolean = True ' to show exceptions in debug mode 


Public Sub Init_ListView(ByRef SelectedListView As ListView, ByVal FullRowSelect As Boolean, ByVal ViewMode As View, ByVal Columns() As String)

    ' Pass the list to modify, two common settings and an array of columns by string name 
    Try

        SelectedListView.View = ViewMode
        SelectedListView.FullRowSelect = FullRowSelect

        SelectedListView.Columns.Clear()

        For i As Integer = 0 To Columns.Length - 1
            SelectedListView.Columns.Add(Columns(i), -2)
        Next

    Catch ex As Exception
        If ListViewControl_DebugMode = True Then MsgBox(ex.ToString)
    End Try


End Sub

Public Sub MoveListViewItem(ByRef SelectedlistView As ListView, ByVal move As ArrowDirection)

    ' Pass the listview to edit and the command UP/DOWN 

    Try
        ' Select between UP/DOWN arrows 
        If move = ArrowDirection.Up And SelectedlistView.SelectedItems.Count > 0 Then

            Dim selected As ListViewItem = SelectedlistView.SelectedItems(0)
            Dim indx As Integer = selected.Index
            Dim totl As Integer = SelectedlistView.Items.Count

            If indx > 0 Then ' Make sure it's not 0 

                Dim temp As String = SelectedlistView.Items(indx - 1).SubItems(1).Text ' save the string from the old one first so we can put it in the new location 

                ' move the text in column 2 to the column 2 index below it and place the old text in the next column
                SelectedlistView.Items(indx - 1).SubItems(1).Text = SelectedlistView.Items(indx).SubItems(1).Text
                SelectedlistView.Items(indx).SubItems(1).Text = temp

                ' This clears the selection then selects the new index before existing. This allows the user to hit UP again with out having to reselect the item 
                SelectedlistView.SelectedItems.Clear()
                SelectedlistView.Items(indx - 1).Selected = True

            End If


        ElseIf move = ArrowDirection.Down And SelectedlistView.SelectedItems.Count > 0 Then


            Dim selected As ListViewItem = SelectedlistView.SelectedItems(0)
            Dim indx As Integer = selected.Index
            Dim totl As Integer = SelectedlistView.Items.Count

            If indx < SelectedlistView.Items.Count - 1 Then ' if it's less than the max index you can just move the data to the next one 

                Dim temp As String = SelectedlistView.Items(indx + 1).SubItems(1).Text

                SelectedlistView.Items(indx + 1).SubItems(1).Text = SelectedlistView.Items(indx).SubItems(1).Text
                SelectedlistView.Items(indx).SubItems(1).Text = temp

                ' This clears the selection then selects the new index before existing. This allows the user to hit DOWN again with out having to reselect the item 
                SelectedlistView.SelectedItems.Clear()
                SelectedlistView.Items(indx + 1).Selected = True


            End If

            End If


    Catch ex As Exception
        If ListViewControl_DebugMode = True Then MsgBox(ex.ToString)
    End Try



End Sub

Public Sub EditListViewItem(ByRef SelectedListView As ListView, ByVal EditType As Char, ByVal NewText As String)

    ' Pass the listview to edit, EditType: ADD = 'A' DELETE = 'D', and Index of item 
    Try

        Dim selected As ListViewItem = Nothing
        Dim indx As Integer = 0

        If SelectedListView.SelectedIndices.Count > 0 Then ' Only load if something is selected 
            selected = SelectedListView.SelectedItems(0)
            indx = selected.Index
        Else
            ' This is here because you don't need an index selected to add a new item. 
            selected = Nothing
            indx = 0 ' set to 0
        End If



        Dim total As Integer = SelectedListView.Items.Count

        Select Case EditType

            Case "A" ' ADD NEW 

                Dim NewItem As New ListViewItem(CStr(total + 1))

                NewItem.SubItems.Add(NewText)
                SelectedListView.Items.Add(NewItem)

            Case "D" ' DELETE 
                If selected IsNot Nothing Then ' only execute if something is selected 

                    SelectedListView.Items.Remove(selected) ' Delete the selected item 

                    For i As Integer = 0 To SelectedListView.Items.Count - 1
                        SelectedListView.Items(i).SubItems(0).Text = CStr(i + 1)
                    Next


                End If

            Case "S" ' Save edits 

                SelectedListView.Items(indx).SubItems(1).Text = NewText

        End Select

    Catch ex As Exception
        If ListViewControl_DebugMode = True Then MsgBox(ex.ToString)
    End Try


End Sub

端级

若要使用该类,请在表单1声明中添加:

代码语言:javascript
运行
复制
 Private ListViewClass As New class_ListViewControl

然后在Form_Load子中添加您的初始化:

代码语言:javascript
运行
复制
  ListViewClass.Init_ListView(lstv_RFPInfo_Options, True, View.Details, {"#", "Option Title"})

添加按钮示例:

代码语言:javascript
运行
复制
ListViewClass.EditListViewItem(listview1, "A", textbox1.Text)

删除按钮示例:

代码语言:javascript
运行
复制
ListViewClass.EditListViewItem(listview1, "D", "")

表格1示例

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8860586

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档