简体   繁体   中英

VB.Net ListView scrollbar and ListView.ItemChecked issues

I have a listview with checkboxes.

I have a couple of issues I don't seem to be able to find the answers to:

  1. I don't seem to be able to find any properties to change the vertical scroll bar from the left to the right? All other scroll bars default to the right - but no?

  2. I want to update a label when the ItemChecked event fires to indicate the number of items in the list that have been checked. I am aware the ItemChecked event fires when the list is populating, but I have covered this elsewhere in my application by setting a 'DoEvents' variable as false, until the form is populated - and checking the state of that variable when events like this are fired - like thus:

     Private Sub SpecificUsers_ItemChecked(ByVal sender as System.Object, ByVal e as System.Windows.Forms.ItemCheckedEventArgs) Handles lstSpecificUsers.ItemChecked If DoEvents Then ' Set standard string Dim s as String = "You have currently selected " & lstSpecificUsers.CheckedItems.Count & " users" If lstSpecificUsers.CheckedItems.Count = 1 Then ' if only 1 selected, show user not users s = s.SubString(0, s.Length - 1) End If lblSpecificUsers.Text = s End If End Sub 

The problem is, the listview is in a tab; and it appears that the list doesn't actually physically populate until the tab is opened - and thus DoEvents is already true, meaning it takes an eternity to load because it's doing the above code a couple of thousand times.

Any reason why the listview doesn't populate until the tab is loaded?

Any help would be greatly appreciated!

First, with that many items and a database, a DataGridView might be a better choice. For the Scroll on the wrong side, you likely have RightToLeft set to True. If the Form Property is True I dont think it will let you change it on the child.

The issue with the checks, is that many controls paint/update only when needed. If no one can see the LV, it doesnt do all the painting which includes the ticking the check. To "fix" this, first force it to load the stuff in form load where you populate it:

Private Sub Form1_Load(sender etc etc etc
    Dim lvi As ListViewItem

    For n As Integer = 0 To something

        LV.Items.Add(lvi)
        If foo Then
           lvi.Checked = True        ' I assume...

    Next
    ' LV.RedrawItems doesnt work because there is no one looking
    ' force it to scroll
    LV.TopItem = LV.Items(LV.Items.Count - 1)

End Sub

Remove the Handles clause from the event handler:

Private lvDrawn As Boolean = False
Private Sub LV_ItemChecked(sender As Object, e As ItemCheckedEventArgs) 
               'Handles LV.ItemChecked   REMOVED

    If lvDrawn Then

        ' your code here

    End If
End Sub

Hook things up when the control is made visible, using the TabControl:

Private Sub TabCtrl_Selected(sender As Object, e As TabControlEventArgs) 
       Handles TabControl1.Selected

    If e.TabPage Is tabWithLV Then
        If lvDrawn = False Then
            ' first time thru, so Add the handler
            AddHandler LV.ItemChecked, AddressOf LV_ItemChecked
            ' we left it at the bottom, so reset
            LV.TopItem = LV.Items(0)

            ' set the just_do_this_once flag
            lvDrawn = True
        End If
    End If
End Sub

Setting TopItem to the bottom forces the list to load; Removing the Handles, prevents the Check event from firing. Then you are hooking the Handler back up only when it becomes visible. You might be able to use the LV.VisibleChanged event instead of the TabControl event - that was just the first thing that occurred to me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM