找回密码
 加入
搜索
查看: 10172|回复: 21

[GUI管理] 一次性更新Combo下拉框数据【已解决】

 火.. [复制链接]
发表于 2012-11-19 17:04:36 | 显示全部楼层 |阅读模式
本帖最后由 xiehuahere 于 2012-11-19 19:25 编辑

请教下,下面这样为何不能更新Combo下拉框中的数据?

#include <WinAPI.au3>
#include <GuiComboBox.au3>

$hGUI = GUICreate("test", 300, 250, -1, -1)
$hCombo = _GUICtrlComboBox_Create($hGUI, "", 40, 60, 120, 26)
GUISetState()
GUICtrlSetData(_WinAPI_GetDlgCtrlID($hCombo), "123456|ok", "ok")

Do
Until GUIGetMsg() = -3


使用_GUICtrlComboBox_Create创建(这个别改);而且实际运用中,我需要做去重等处理后再更新下拉框,用于保存历史查询记录,最新的在最上面。
貌似 _GUICtrlComboBox_ 开头的函数没有更新所有下拉框数据的,只能在创建时一次性添加。
发表于 2012-11-19 18:15:27 | 显示全部楼层
应该没有该函数,_GUICtrlComboBox_Create 创建时也是循环添加的,自己写个。
#include <WinAPI.au3>
#include <GuiComboBox.au3>
 
$hGUI = GUICreate("test", 300, 250, -1, -1)
$hCombo = _GUICtrlComboBox_Create($hGUI, "ss|111", 40, 60, 120, 26)
GUISetState()
_GUICtrlComboBox_SetData($hCombo, "123456|ok|ok1", "ok")

Do
Until GUIGetMsg() = -3

Func _GUICtrlComboBox_SetData($hWnd, $sText, $default = '')
        If Not IsHWnd($hWnd) Then Return SetError(1, 0, 0)
        Local $aText, $sDelimiter = Opt('GUIDataSeparatorChar')
        If $sText = '' Or StringLeft($sText, 1) == $sDelimiter Then
                _SendMessage($hWnd, 0x14B) ;$CB_RESETCONTENT = 0x14B
                $sText = StringTrimLeft($sText, 1)
                If $sText = '' Then Return
        EndIf
        $aText = StringSplit($sText, $sDelimiter)
        For $x = 1 To $aText[0]
                _SendMessage($hWnd, 0x143, 0, $aText[$x], 0, 'wparam', 'wstr') ;$CB_ADDSTRING = 0x143
        Next
        If $default = '' Then Return
        $iIndex = _SendMessage($hWnd, 0x158, -1, $default, 0, 'wparam', 'wstr') ;$CB_FINDSTRINGEXACT = 0x158
        _SendMessage($hWnd, $CB_SETCURSEL, $iIndex) ;$CB_SETCURSEL = 0x14E
EndFunc   ;==>_GUICtrlComboBox_SetData

评分

参与人数 1金钱 +30 收起 理由
xiehuahere + 30 很好,学习了~

查看全部评分

发表于 2012-11-19 18:45:59 | 显示全部楼层
分析:
根据 GuiComBox.au3
334-339行源代码

        If StringLen($sText) Then
                $aText = StringSplit($sText, $sDelimiter)
                For $x = 1 To $aText[0]
                        _GUICtrlComboBox_AddString($hCombo, $aText[$x])
                Next
        EndIf


可知
_GUICtrlComboBox_Create 函数的第二个参数 可以用 含有 | 分隔的 一组字符串
来实现下拉菜单的各个条目


#include <WinAPI.au3>
#include <GuiComboBox.au3>
$hGUI = GUICreate("test", 300, 250, -1, -1)
$hCombo = _GUICtrlComboBox_Create($hGUI, "baidu|google|autoit", 40, 60, 120, 26)
GUISetState()
Do
Until GUIGetMsg() = -3
发表于 2012-11-19 18:49:39 | 显示全部楼层
貌似 无法用 guictrlsetdata 或 _GUICtrlComboBox_AddString 一次搞定对个选项
发表于 2012-11-19 18:54:52 | 显示全部楼层
再次从  GuiComBox.au3

找到 Func _GUICtrlComboBox_AddString($hWnd, $sText)
        If $Debug_CB Then __UDF_ValidateClassName($hWnd, $__COMBOBOXCONSTANT_ClassName)
        If Not IsHWnd($hWnd) Then $hWnd = GUICtrlGetHandle($hWnd)

        Return _SendMessage($hWnd, $CB_ADDSTRING, 0, $sText, 0, "wparam", "wstr")
EndFunc   ;==>_GUICtrlComboBox_AddString

故 推测
可以自己写个函数 封装一下  和 StringSplit 和 _SendMessage
发表于 2012-11-19 19:09:07 | 显示全部楼层
本帖最后由 veket_linux 于 2012-11-19 19:10 编辑

成功了


#include <WinAPI.au3>
#include <GuiComboBox.au3>
 
$hGUI = GUICreate("test", 300, 250, -1, -1)
$hCombo = _GUICtrlComboBox_Create($hGUI, "", 40, 60, 120, 26)
GUISetState()
MyComboBoxSetAllItem($hCombo, "百度|google|AutoIt中文论坛")

Do
Until GUIGetMsg() = -3

Func MyComboBoxSetAllItem($hWnd, $items_string)
        If $items_string <> "" Then
                Local $items = StringSplit($items_string, "|")
                Local $i
                For $i = 1 To $items[0]
                        _SendMessage($hWnd, $CB_ADDSTRING, 0, $items[$i], 0, "wparam", "wstr")
                Next
        EndIf
EndFunc



没有枪... 没有炮 ... 我们自己来造.....

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×

评分

参与人数 1金钱 +30 收起 理由
xiehuahere + 30 很有钻研精神,多谢帮忙~

查看全部评分

 楼主| 发表于 2012-11-19 19:24:55 | 显示全部楼层
嗯,了解了。
多谢大家热心帮忙!
_SendMessage用得太少。学习了~~
发表于 2012-11-19 19:48:46 | 显示全部楼层
本帖最后由 netegg 于 2012-11-19 19:51 编辑

_GUICtrlComboBox_ResetContent
更新的重点不久在所有数据重置吗?现成的,数据另外加
发表于 2012-11-19 20:36:31 | 显示全部楼层
回复 8# netegg


    实测 此函数是 清空了所有选项 .........
 楼主| 发表于 2012-11-19 20:41:37 | 显示全部楼层
我不想影响到edit control中的内容
发表于 2012-11-19 21:19:31 | 显示全部楼层
本帖最后由 netegg 于 2012-11-20 11:36 编辑

#Include <GuiListBox.au3>
#include<Guicombobox.au3>
if _GUICtrlComboBox_GetComboBoxInfo($hCombo, $tInfo) then
     $hList = DllStructGetData($tInfo, "hList")
     _GUICtrlListBox_ResetContent($hList)
     _GUICtrlListBox_addstring($hList, _GUICtrlComboBox_getedittext($hCombo))
endif
发表于 2012-11-19 21:21:25 | 显示全部楼层
_GUICtrlComboBox_ResetContent
更新的重点不久在所有数据重置吗?现成的,数据另外加
netegg 发表于 2012-11-19 19:48



    我在2#写的代码中包括了此功能。也因此所有功能和 GuiCtrlSetData() 针对Combobox 控件的数据设置功能一致。
_GUICtrlComboBox_SetData( 控件句柄, 数据 [, 默认值] )
如果“数据”对应于一个已存在的条目,则将它设置为默认.
如果 "数据"以 GUIDataSeparatorChar(设置的分隔符) 开始或空串 "" ,则前一条列表被销毁.
发表于 2012-11-19 21:27:20 | 显示全部楼层
回复 9# veket_linux
'更新所有下拉框数据'
楼主这个要求不清空怎么更新,项目数都不一定相同
发表于 2012-11-20 00:58:04 | 显示全部楼层
路过学习一下!
 楼主| 发表于 2012-11-20 10:10:00 | 显示全部楼层
回复 13# netegg

是的,you're right.
要先清再更新的。
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-4-27 19:22 , Processed in 0.080562 second(s), 25 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表