support23
24-04-2013, 08:34
Доброго дня!
Можно ли хранить историю введенных имен в поле GUICtrlCreateCombo, и при запуске утилиты загружать из истории данные (например имена ПК) ?
на скриншоте пример как это выглядит.
support23
24-04-2013, 11:42
отлично!)
Как это сделать, подскажи?
#include (http://autoit-script.ru/autoit3_docs/keywords.htm##include) <GUIConstantsEx.au3>
#include (http://autoit-script.ru/autoit3_docs/keywords.htm##include) <GUIComboBox.au3>
$sConfig = @ScriptDir (http://autoit-script.ru/autoit3_docs/macros.htm#@desktopdir) & '\Config.ini'
$sHistory = IniRead (http://autoit-script.ru/autoit3_docs/functions/IniRead.htm)($sConfig, 'Main', 'History', '')
$hGUI = GUICreate (http://autoit-script.ru/autoit3_docs/functions/GUICreate.htm)("Test Script", 300, 200)
$iCombo = GUICtrlCreateCombo (http://autoit-script.ru/autoit3_docs/functions/GUICtrlCreateCombo.htm)("", 20, 40, 260, 60)
GUICtrlSetData (http://autoit-script.ru/autoit3_docs/functions/GUICtrlSetData.htm)($iCombo, $sHistory)
$iAddItem_Bttn = GUICtrlCreateButton (http://autoit-script.ru/autoit3_docs/functions/GUICtrlCreateButton.htm)('Add', 20, 70, 70, 20)
GUISetState (http://autoit-script.ru/autoit3_docs/functions/GUISetState.htm)(@SW_SHOW (http://autoit-script.ru/autoit3_docs/macros.htm#@sw_show), $hGUI)
While (http://www.autoitscript.com/autoit3/docs/keywords.htm#While) 1
Switch (http://www.autoitscript.com/autoit3/docs/keywords.htm#Switch) GUIGetMsg (http://autoit-script.ru/autoit3_docs/functions/GUIGetMsg.htm)()
Case (http://www.autoitscript.com/autoit3/docs/keywords.htm#Case) $GUI_EVENT_CLOSE
$sHistory = _GUICtrlComboBox_GetList (http://autoit-script.ru/autoit3_docs/libfunctions/_guictrlcombobox_getlist.htm)($iCombo)
IniWrite (http://autoit-script.ru/autoit3_docs/functions/IniWrite.htm)($sConfig, 'Main', 'History', $sHistory)
Exit (http://www.autoitscript.com/autoit3/docs/keywords.htm#Exit)
Case (http://www.autoitscript.com/autoit3/docs/keywords.htm#Case) $iAddItem_Bttn
$sItem = GUICtrlRead (http://autoit-script.ru/autoit3_docs/functions/GUICtrlRead.htm)($iCombo)
If (http://www.autoitscript.com/autoit3/docs/keywords.htm#If) $sItem <> '' Then (http://www.autoitscript.com/autoit3/docs/keywords.htm#Then)
_GUICtrlComboBox_AddString (http://autoit-script.ru/autoit3_docs/libfunctions/_guictrlcombobox_addstring.htm)($iCombo, $sItem)
_GUICtrlComboBox_SetEditText (http://autoit-script.ru/autoit3_docs/libfunctions/_guictrlcombobox_setedittext.htm)($iCombo, '')
GUICtrlSetState (http://autoit-script.ru/autoit3_docs/functions/GUICtrlSetState.htm)($iCombo, $GUI_FOCUS)
EndIf (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndIf)
EndSwitch (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndSwitch)
WEnd (http://www.autoitscript.com/autoit3/docs/keywords.htm#WEnd)
support23
24-04-2013, 13:08
Мой вопрос решен, большое спасибо!
support23, могу только предложить выковыривать из программ JumpReg (http://u.to/abSc) и TextReplace (http://u.to/wZZN), особенность - посде добавления очередного пункта, если он уже есть в списке, то перемещается к верху. Выбор из списка перемещает пункт к верху. Превышение определённого количества пунктов удаляет последний в списке. Алгоритм прост, после нажатия Enter или любого события подтверждающего, что ввод окончен происходит очистка ComboBox, а список пунктов хранящийся в переменной подвергается обработке по условию указанному выше и результат применяется к ComboBox.
Вот
#include <GUIConstantsEx.au3>
#include <GuiComboBox.au3>
$sConfig = @ScriptDir & '\Config.ini'
$sHistory = IniRead($sConfig, 'Main', 'History', '')
$iLimit = 5
$hGUI = GUICreate("Пример, История в Combo", 300, 200)
$iCombo = GUICtrlCreateCombo('', 20, 40, 260, -1)
If $sHistory Then GUICtrlSetData($iCombo, $sHistory, StringRegExpReplace($sHistory, '(.*?)\|.*|(.*?)', '\1'))
$iAddItem_Btn = GUICtrlCreateButton('Добавить', 20, 70, 70, 20)
GUISetState()
While 1
Switch GUIGetMsg()
Case $iAddItem_Btn
_Combo_AddToHistory($iCombo, GUICtrlRead($iCombo), $sHistory, $iLimit)
Case $GUI_EVENT_CLOSE
$sHistory = _GUICtrlComboBox_GetList($iCombo)
IniWrite($sConfig, 'Main', 'History', $sHistory)
Exit
EndSwitch
WEnd
; Добавляет пункт в историю
Func _Combo_AddToHistory($iCombo, $sAdd, ByRef $sHistory, $iLimit)
If Not $sAdd Then Return
If Not $sHistory Then
$sHistory = $sAdd
GUICtrlSetData($iCombo, $sHistory, $sHistory)
Return
EndIf
; Удаляем элемент из списка, если он существует в нём
$tmp = StringReplace('|' & $sHistory & '|', '|' & $sAdd & '|', '|')
If @extended Then $sHistory = StringTrimLeft(StringTrimRight($tmp, 1), 1)
; Добавляем элемент
$sHistory = $sAdd & '|' & $sHistory
; Если число элементов превышает определённый лимит, то отрезаем крайние справа
StringReplace($sHistory, '|', '')
If @extended > $iLimit - 1 Then
; Получаем позицию обрезки и обрезаем
$sHistory = StringLeft($sHistory, StringInStr($sHistory, '|', 0, $iLimit) - 1)
EndIf
GUICtrlSendMsg($iCombo, $CB_RESETCONTENT, 0, 0) ; удаляем контент в Combo
GUICtrlSetData($iCombo, $sHistory, StringRegExpReplace($sHistory, '(.*?)\|.*', '\1')) ; Вставляем контент
; GUICtrlSetState($iCombo, $GUI_FOCUS) ; ставим фокус в Combo, если требуется
EndFunc ;==>_AddedHistory
вариант 2
#include <GUIConstantsEx.au3>
#include <GuiComboBox.au3>
$sConfig = @ScriptDir & '\Config.ini'
$sHistory = IniRead($sConfig, 'Main', 'History', '')
$iLimit = 5
$hGUI = GUICreate("Пример, История в Combo", 300, 200)
$iCombo = GUICtrlCreateCombo('', 20, 40, 260, -1)
If $sHistory Then GUICtrlSetData($iCombo, $sHistory, StringRegExpReplace($sHistory, '(.*?)\|.*|(.*?)', '\1'))
$iAddItem_Btn = GUICtrlCreateButton('Добавить', 20, 70, 70, 20)
GUISetState()
While 1
Switch GUIGetMsg()
Case $iAddItem_Btn
_Combo_AddToHistory($iCombo, GUICtrlRead($iCombo), $sHistory, $iLimit)
Case $GUI_EVENT_CLOSE
$sHistory = _GUICtrlComboBox_GetList($iCombo)
IniWrite($sConfig, 'Main', 'History', $sHistory)
Exit
EndSwitch
WEnd
; Добавляет пункт в историю
Func _Combo_AddToHistory($iCombo, $sAdd, ByRef $sHistory, $iLimit)
If Not $sAdd Then Return
If Not $sHistory Then
$sHistory = $sAdd
GUICtrlSetData($iCombo, $sHistory, $sHistory)
Return
EndIf
$iIndex = _GUICtrlComboBox_FindStringExact($iCombo, $sAdd) ; поиск строки, если такая уже существует в истории
If $iIndex <> -1 Then _GUICtrlComboBox_DeleteString($iCombo, $iIndex) ; удалить если существует
_GUICtrlComboBox_InsertString($iCombo, $sAdd, 0) ; добавляет строку к начало
$iCount = _GUICtrlComboBox_GetCount($iCombo) ; Количество пунктов
If $iCount > $iLimit Then ; если больше лимита, то удаляем
For $i = $iLimit To $iCount + 1
_GUICtrlComboBox_DeleteString($iCombo, $i)
Next
EndIf
; GUICtrlSetState($iCombo, $GUI_FOCUS) ; ставим фокус в Combo, если требуется
EndFunc ;==>_AddedHistory
vBulletin v3.6.4, Copyright ©2000-2025, Jelsoft Enterprises Ltd.