2012-01-14 15 views
0

リストビューで上下に動かすために2つのボタン(上下)を使用して、リストビューにどの方向が表示されているかに応じて各アイテムをハイライト表示しようとしています。ボタンでリストビューを上下に移動する

これは私の現在のコードです:

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

、私はあなたが使用できると思い複数選択= Falseの

を選択するか、あなたのコード内でこの

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

理由はコードを 'cmdDown_Click'関数が実行されるたびに実行します。フォームのコンストラクタで設定します。 –

+0

上記の記事をCodyの提案に従って編集しました。 –

0

よう

ListView1.MultiSelect = False 

をこの1行を追加します。この解決策。リストビューのキーアップ/キーダウンを使用して:

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

これは私のプロジェクトに使用しましたが、私は少し違っています。私は最初の列が固定インデックスであるリストビューを持っているので、ユーザーはリスト内のアイテムの数を知り、番号で参照することができます。これは、列1のインデックス番号が同じで、列2のデータのみを変更する必要があるため、項目を上下に移動するときの問題です。列2の項目の追加、削除、および名前変更もサポートするクラスを作成しました。

Private ListViewClass As New class_ListViewControl 

その後のForm_Loadにサブあなたの初期化を追加します:1つの宣言は追加のフォームに、このクラスを使用するには

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 

エンドクラス

ListViewClass.Init_ListView(lstv_RFPInfo_Options, True, View.Details, {"#", "Option Title"}) 

は、ボタンを追加例:

ListViewClass.EditListViewItem(listview1, "A", textbox1.Text) 

削除ボタン例:

ListViewClass.EditListViewItem(listview1, "D", "") 

Form 1 example

関連する問題