如何检测列表视图何时加载了项目? [英] How do I detect when a listview has items loaded in it?
问题描述
我的程序中有2个表单,其中包含带有复选框的列表视图。当用户单击打开时,系统会提示他们加载哪些自定义数据库文件(随机访问)。我可以正常加载文件,但它们可能包含多达6500个单词,需要几秒钟。我不希望用户点击按钮来操作列表视图中的数据,直到完成绘图(显示所有项目)。
什么我试过了:
这是加载listview的代码。我无法找到一个解决方案来检测控件何时显示和完全加载。
I have 2 forms in my program that have listviews in them with checkboxes. When the user clicks "OPEN", they are prompted which of my custom database files to load (Random access). I am able to load the files fine, but they may contain up to 6500 words which takes a few seconds. I don't want the user to click on a button to manipulate the data in the listview until it is completed drawing (all items shown).
What I have tried:
This is the code that loads the listview. I haven't been able to come up with a solution for detecting when the control is shown and fully loaded.
<pre>Private Sub OPENToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OPENToolStripMenuItem.Click
' ***** This is the FILE/OPEN menu item *****
' Step 1: Establish the variables I will use in this subroutine.
Dim tmpInt1 As Integer = 0
Dim tmpInt2 As Integer = 0
Dim tmpString1 As String = Nothing
Dim tmpString2 As String = Nothing
' Step 2: Set the variable ListLoaded to false to prevent unwanted code from running.
ListLoaded = False
' Step 3: Set up the OpenFile dialog box.
OpenFileDialog1.Reset()
OpenFileDialog1.DefaultExt = "dat"
OpenFileDialog1.CheckFileExists = True
OpenFileDialog1.CheckPathExists = True
OpenFileDialog1.Multiselect = False
OpenFileDialog1.ValidateNames = True
OpenFileDialog1.Title = "Select the word dictionary that you wish to export from:"
OpenFileDialog1.Filter = "ERea Chances Are Word Dictionary Files (*.dat)|*.dat"
OpenFileDialog1.InitialDirectory = DictPath
' Step 4: Open the dialog box. If the user clicks cancel, exit the sub.
If OpenFileDialog1.ShowDialog = DialogResult.Cancel Then Exit Sub
' Step 5: Set the back color for label6 to white, and put the selected file path in the label.
Label6.BackColor = System.Drawing.Color.White
Label6.Text = OpenFileDialog1.FileName
' Step 6: Refresh the entire form.
Me.Refresh()
' Step 7: Hide Label1 & enable GroupBox2
Label1.Hide()
GroupBox2.Enabled = True
' Step 8: Get a free file number and open the selected dictionary file.
FileNum = FreeFile()
FileOpen(FileNum, Trim(Label6.Text), OpenMode.Random, OpenAccess.Read, OpenShare.LockRead)
' Step 9: Get the first entry in this file and set the progressbar maximum to it.
FileGet(FileNum, tmpInt1, 1)
If tmpInt1 > 6500 Then
MsgBox("There are more than 6500 words in the selected dictionary file." & Chr(10) & "Only the first 6500 words will be loaded.")
tmpInt1 = 6500
End If
' Step 11: Set the back color for Label7 to WHITE and populate the field with the word count.
Label7.BackColor = System.Drawing.Color.White
Label7.Text = tmpInt1
' Step 12: Loop thru the dictionary file and place the words and their lengths into the listview control.
For x As Integer = 3 To (tmpInt1 + 2)
FileGet(FileNum, tmpString1, x)
If tmpString1 <> String.Empty Then
Dim wrditem As New ListViewItem
tmpString2 = Trim(Mid(tmpString1, 1, 15))
tmpInt2 = Len(Trim(tmpString2))
wrditem.Text = tmpString2
wrditem.SubItems.Add(Str(tmpInt2))
ListView1.Items.Add(wrditem)
End If
Next
' Step 13: Close the dictionary file.
FileClose(FileNum)
' Step 14: Reset the variable ListLoaded to its default value.
ListLoaded = True
' Step 15: Set the sort order for the listview control and sort the list ascending (alphabetically).
ListView1.Sorting = SortOrder.Ascending
ListView1.Sort()
' Step 16: Make the back color of Label8 white & place the checked items count in the label.
Label8.BackColor = System.Drawing.Color.White
Label8.Text = ListView1.CheckedItems.Count.ToString
' Step 17:Enable GroupBoxes 3 & 4.
If ListView1.Items.Count > 0 Then
GroupBox3.Enabled = True
Button1.Enabled = True
Button2.Enabled = False
GroupBox4.Enabled = False
End If
' Step 18: Return the menu items to their proper status.
EXPORTToolStripMenuItem.Enabled = False
SAVEASToolStripMenuItem.Enabled = True
STARTToolStripMenuItem.Enabled = False
CLOSEToolStripMenuItem.Enabled = True
OPENToolStripMenuItem.Enabled = False
EXITToolStripMenuItem.Enabled = False
End Sub
推荐答案
笨拙,但你可以添加一个计时器,并在listview1.items.count = tmpInt1的计时器事件检查中,当这是真的时打开所有内容。
Clumsy, but you could add a timer and in the timer event check for listview1.items.count = tmpInt1, turn everything on when this is true.
这篇关于如何检测列表视图何时加载了项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!