将数据加载到组合框中很慢 [英] Loading data in to a combo box is slow

查看:37
本文介绍了将数据加载到组合框中很慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有搜索屏幕的VB6应用程序.在搜索中,我有9个组合框.一些组合框只有几个项目,但有些则有数百个项目.填充数据需要很长时间(几秒钟).

I have a VB6 application with a search screen. On the search, I have 9 combo boxes. Some of the combo boxes only have a couple items, but some have a couple hundred items. It takes a long time (couple of seconds) to populate the data.

每个组合框的配置都相同:排序= False,样式= 2-下拉列表

Each combo box is configured the same: Sorted = False, Style = 2 - Dropdown List

3个组合框的项目少于20个.1件商品有130件.4个有大约250个项目1个有将近700个项目.

3 of the combo boxes have less that 20 items. 1 has 130 items. 4 have approximately 250 items 1 has almost 700 items.

我用相似的代码填充所有九个组合框.

I fill all nine combo boxes with similar code.

While Not RS.EOF

    cmbX.List(i) = RS("Description")
    cmbX.ItemData(i) = RS("Id")

    i = i + 1

    RS.MoveNext
Wend

我尝试设置Visible = False,但对性能没有影响.

I tried setting Visible = False but it had no effect on performance.

是否有另一种方法来填充组合框,该组合框的性能将比现有方法更好?

推荐答案

您可以尝试以下方法.根据此帖子,您可以节省60%的开销通过使用Windows API函数而不是通常的AddItem方法来填充组合框:

Here is something you can try. According to this post you can shave about 60% off your overhead by using a Windows API function to populate the combo box, instead of the usual AddItem method:

Private Const CB_ERR As Long = -1
Private Const CB_ADDSTRING As Long = &H143
Private Const CB_RESETCONTENT As Long = &H14B
Private Const CB_SETITEMDATA As Long = &H151

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal _
hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Public Sub AddItem(cmb As ComboBox, Text As Variant, Optional ItemData As Long)

   Dim l As Long
   Dim s As String

   If VarType(Text) = vbString Then
      s = Text
   Else
      s = Trim$(Str$(Text))
   End If

   l = SendMessage(cmb.hwnd, CB_ADDSTRING, 0&, ByVal s)
   If l <> CB_ERR Then
      SendMessage cmb.hwnd, CB_SETITEMDATA, l, ByVal ItemData
   End If

End Sub

Public Sub Clear(cmb As ComboBox)
   SendMessage cmb.hwnd, CB_RESETCONTENT, 0, 0&
End Sub

通过省略函数调用,而直接调用API函数,您也许可以节省更多的钱.

You might be able to shave a little more off by omitting the function call, and just calling the API function directly.

这篇关于将数据加载到组合框中很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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