Sign in to follow this  
Followers 0
myspacee

Move up/down element in ComboBox

2 posts in this topic

Hello to all,

going mad solving this.

I've a directory list inserted in ComboBox.

Want to do some operation, but before i want to learn how to move element in ComboBox.

Understand some basic thing but other are too hard for me.

In folowing example you have all c: disk list insert in combobox.

I want to select an element and usign up/down buttons move entry where i want.

Have some problem with delete/update logic and syntax.

Anyone can help me ?

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <String.au3>
#include <misc.au3>
#include <GUIConstants.au3>
#include <GuiListView.au3>
#include <array.au3>
#Include <Constants.au3>
#Include <GuiButton.au3>


#include <GUIComboBox.au3>
#include <GuiConstantsEx.au3>

local $file, $line, $Button_ok, $Button_cancel, $Button_UP, $Button_Down, $msg
local $avArray[1]
local $sText
local $hCombo, $retval
local $avFile
local $my_selection_index = 0, $my_selection = 0
local $my_element_number = 0



$sDir = "C:\"

; Shows sub-directoryes in the current directory.
$search = FileFindFirstFile($sDir & "*")  



;////////////////////////////////////////////////////////////////////////
;       GUI
;////////////////////////////////////////////////////////////////////////
#Region ### START Koda GUI section ### Form=
GUICreate("Current Apps folder on SD", 842, 505, 0, 0)
$hCombo = GUICtrlCreateCombo("", 2, 60, 200, 450, BitOR($CBS_SIMPLE,$CBS_AUTOHSCROLL,$CBS_DISABLENOSCROLL,$WS_VSCROLL))

$Button_ok = GUICtrlCreateButton("OK", 16, 20, 70, 25, $BS_DEFPUSHBUTTON)
$Button_cancel = GUICtrlCreateButton("Cancel", 100, 20, 70, 25)

$Button_UP = GUICtrlCreateButton("UP", 205, 82, 50, 200)
$Button_Down = GUICtrlCreateButton("DOWN", 205, 282, 50, 200, 0)

GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###





;////////////////////////////////////////////////////////////////////////
;       populate Combo
;////////////////////////////////////////////////////////////////////////
While 1 ;cicle
    $file = FileFindNextFile($search) 
    If @error Then ExitLoop
    
    $attribut = FileGetAttrib($sDir & $file) ;check if is a Directory
    $attribut_check = StringInStr($attribut, "D")

    if $attribut_check <> 0 Then ;If is a directory
        ;~  open combo
        _GUICtrlComboBox_BeginUpdate($hCombo)
        ;~  ; Add directory as new string
        _GUICtrlComboBox_AddString($hCombo, $file)
        ;~  ; update and end update
        _GUICtrlComboBox_EndUpdate($hCombo)
        
        ;count inserted element in combo
        $my_element_number = $my_element_number + 1
    EndIf
        
WEnd
            
;correct counter to array form (1 = 0)
$my_element_number = $my_element_number - 1









;////////////////////////////////////////////////////////////////////////
;       MAIN
;////////////////////////////////////////////////////////////////////////
;select first element
_GUICtrlComboBox_SetCurSel($hCombo, $my_selection_index)
; retrieve LB Text
_GUICtrlComboBox_GetLBText($hCombo, $my_selection, $sText)
    
While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $Button_ok

        Case $Button_cancel


        Case $Button_UP
            $my_selection = $my_selection - 1
            
            if $my_selection < 0 then $my_selection = 0
;~          ; Select Item
            _GUICtrlComboBox_SetCurSel($hCombo, $my_selection)

            ; retrieve LB Text
            _GUICtrlComboBox_GetLBText($hCombo, $my_selection, $sText)

            $my_selection_index = _GUICtrlComboBox_GetCurSel($hCombo)
            ;se un valore è stato selezionato procedo
            if $my_selection_index <> "-1" then
                ;___________________________________________________________ Menage_update_selection
                _GUICtrlComboBox_BeginUpdate($hCombo)

                ; select string
                _GUICtrlComboBox_GetLBText($hCombo, $my_selection_index , $my_selection)
                ;msgbox(0,"",$my_selection)
                ; delete selection
                _GUICtrlComboBox_DeleteString($hCombo, $my_selection_index)
                ; update
                _GUICtrlComboBox_EndUpdate($hCombo)
                
                ;___________________________________________________________ Add modified value
                _GUICtrlComboBox_BeginUpdate($hCombo)
                ; Insert string
                _GUICtrlComboBox_InsertString($hCombo, $sText, $my_selection )
                ; update
                _GUICtrlComboBox_EndUpdate($hCombo)
            EndIf
    
        Case $Button_Down
            $my_selection = $my_selection + 1
            
            if $my_selection >= $my_element_number then $my_selection = $my_element_number
;~          ; Select Item
            _GUICtrlComboBox_SetCurSel($hCombo, $my_selection)

            ; retrieve LB Text
            _GUICtrlComboBox_GetLBText($hCombo, $my_selection, $sText)

            $my_selection_index = _GUICtrlComboBox_GetCurSel($hCombo)
            ;se un valore è stato selezionato procedo
            if $my_selection_index <> "-1" then
                ;___________________________________________________________ Menage_update_selection
                _GUICtrlComboBox_BeginUpdate($hCombo)

                ; select string
                _GUICtrlComboBox_GetLBText($hCombo, $my_selection_index , $my_selection)
                ;msgbox(0,"",$my_selection)
                ; delete selection
                _GUICtrlComboBox_DeleteString($hCombo, $my_selection_index)
                ; update
                _GUICtrlComboBox_EndUpdate($hCombo)
                
                ;___________________________________________________________ Add modified value
                _GUICtrlComboBox_BeginUpdate($hCombo)
                ; Insert string
                _GUICtrlComboBox_InsertString($hCombo, $sText, $my_selection )
                ; update
                _GUICtrlComboBox_EndUpdate($hCombo)
            EndIf
    



    EndSwitch
    

    ToolTip("Selected item : " & _GUICtrlComboBox_GetCurSel($hCombo) & " ---> text : " & $sText  ,0,0)
    Sleep(10)
WEnd

GUIDelete()

Thank you all for reading,

m.

Share this post


Link to post
Share on other sites



Try it like this:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <File.au3>
#include <GUIComboBox.au3>

Global $iSel, $sText
Global $hGUI = GUICreate("Current Apps folder on SD", 842, 505)
Global $DirCombo = GUICtrlCreateCombo("", 2, 60, 200, 450, BitOR($CBS_SIMPLE, $CBS_AUTOHSCROLL, $CBS_DISABLENOSCROLL, $WS_VSCROLL))
Global $Button_UP = GUICtrlCreateButton("UP", 205, 82, 50, 200)
Global $Button_Down = GUICtrlCreateButton("DOWN", 205, 282, 50, 200, 0)

; populate Combo
Global $sDrive = StringLeft(@SystemDir, 3)
Global $avDirs = _FileListToArray($sDrive, "*", 2)
For $n = 1 To $avDirs[0]
    _GUICtrlComboBox_AddString($DirCombo, $avDirs[$n])
Next

GUISetState(@SW_SHOW)

; MAIN
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button_UP
            _ComboMoveOnce($DirCombo, 1)
        Case $Button_Down
            _ComboMoveOnce($DirCombo, 0)
    EndSwitch

    $iSel = _GUICtrlComboBox_GetCurSel($DirCombo)
    _GUICtrlComboBox_GetLBText($DirCombo, $iSel, $sText)
    ToolTip("Selected item: " & $iSel & " = " & $sText, 0, 0)
WEnd


; $hCombo = ComboBox control
; $iDir = 0 for down, 1 for up
Func _ComboMoveOnce($hCombo, $iDir)
    Local $sCurTxt, $sOtherTxt, $iOther
    Local $iCnt = _GUICtrlComboBox_GetCount($hCombo) ; 1-based count
    Local $iCurSel = _GUICtrlComboBox_GetCurSel($hCombo) ; 0-based index

    If $iDir Then ; Up
        If $iCurSel = 0 Then
            Return SetError(1, 1, 0)
        Else
            $iOther = $iCurSel - 1
        EndIf
    Else ; Down
        If $iCurSel = $iCnt - 1 Then
            Return SetError(1, 2, 0)
        Else
            $iOther = $iCurSel + 1
        EndIf
    EndIf

    _GUICtrlComboBox_GetLBText($DirCombo, $iCurSel, $sCurTxt)
    _GUICtrlComboBox_GetLBText($DirCombo, $iOther, $sOtherTxt)

    If $iDir Then ; Up
        _GUICtrlComboBox_DeleteString($hCombo, $iOther)
        _GUICtrlComboBox_InsertString($hCombo, $sOtherTxt, $iCurSel)
        _GUICtrlComboBox_SetCurSel($hCombo, $iOther)
    Else ; Down
        _GUICtrlComboBox_DeleteString($hCombo, $iCurSel)
        _GUICtrlComboBox_InsertString($hCombo, $sCurTxt, $iOther)
        _GUICtrlComboBox_SetCurSel($hCombo, $iOther)
    EndIf
EndFunc

:D


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0