Jump to content

Retain last selected item in listbox


archrival
 Share

Recommended Posts

Is it possible to retain the last selected position in a list box? I have a listbox that when an item is selected, an action is performed and the listbox is refreshed. When I do this, the last item in the listbox is always selected. Is it possible to force the selected item to be the previously selected item before the refresh?

Link to comment
Share on other sites

Here's one way. You might want to remove the line _GuiLB_Scroll($List_1)

And before you ask: The items are added to the list in sorted order, and "item10" comes before "item2" alphabetically.....

#include <GuiConstants.au3>
Global $COUNT = 0

If Not IsDeclared('WS_CLIPSIBLINGS') Then Global $WS_CLIPSIBLINGS = 0x04000000
$GUI = GuiCreate("MyGUI", 392, 323,(@DesktopWidth-392)/2, (@DesktopHeight-323)/2 , $WS_OVERLAPPEDWINDOW + $WS_VISIBLE + $WS_CLIPSIBLINGS)

$List_1 = GuiCtrlCreateList("item0", 20, 40, 120, 100)
$Button_2 = GuiCtrlCreateButton("Button2", 170, 50, 120, 40)

GuiSetState()
While 1
    $msg = GuiGetMsg()
    Select
    Case $msg = $GUI_EVENT_CLOSE
        ExitLoop
    Case $msg = $Button_2
      $COUNT = $COUNT + 1
      _GuiLB_AddString($List_1, "item" & $COUNT)
      ControlSend($GUI, "", $List_1, "{PgDn}");select last item
      _GuiLB_Scroll($List_1)
    EndSelect
WEnd
Exit

; Add item to list
Func _GuiLB_AddString($ref, $string)
    Return GuiCtrlSendMsg($ref, 0x0180, 0, $string);LB_ADDSTRING
 EndFunc
 
; scroll lis box so that you can see the last item added
Func _GuiLB_Scroll($ref)
   Local $count = GuiCtrlSendMsg($ref, 0x018B, 0, 0);LB_GETCOUNT
   Return GuiCtrlSendMsg($ref, 0x197, $count-1, 0);LB_SETTOPINDEX
EndFunc
Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!
Link to comment
Share on other sites

The example sort of worked for me, but didn't work with my script. Maybe I'm doing it wrong, but it didn't do anything for me. Does it make any difference that I totally refresh the entire listbox? I'm starting and stopping services on a machine and to get the updated list of services I run a vbscript to give me the list and it replaces what was in the listbox previously. Is it still possible? Here's what I've got:

_CheckServices()
   
   GUISetState()
   While 1
      $msgs = GUIGetMsg()
      Select
         Case $msgs = $GUI_EVENT_CLOSE
            ExitLoop
         Case Else
            If $msgs = $combos Then
               $linenumber = Int(_GuiLB_GetCurSel ($combos) + 1)
               $lastclicked = $servicename[$linenumber]
               GUICtrlSetData($labels, $servicestatus[$linenumber])
               GUICtrlSetData($svclabel, $servicename[$linenumber])
            EndIf
            If $msgs = $button1 Then
               If $lastclicked <> "" Then
                  If Not _ServiceRunning ($comp[$pcnumber], $lastclicked) Then
                     _StartService ($comp[$pcnumber], $lastclicked)
                     _CheckServices ()
                  EndIf
               EndIf
            EndIf
            If $msgs = $button2 Then
               If $lastclicked <> "" Then
                  If _ServiceRunning ($comp[$pcnumber], $lastclicked) Then
                     _StopService ($comp[$pcnumber], $lastclicked)
                     _CheckServices ()
                  EndIf
               EndIf
            EndIf
      EndSelect
   WEnd

_CheckServices():

RunWait(@ComSpec & " /c cscript //T:10 //nologo " & @TempDir & "\services.vbs " & $comp[$pcnumber] & " | sort > " & @TempDir & "\services.txt", "", @SW_HIDE)
   $services = @TempDir & "\services.txt"
   $lines = StringReplace(FileRead($services, FileGetSize($services)), @LF, "|")
   $lines = StringReplace($lines, Chr(9), "")
   $lines = StringReplace($lines, @CR, "")
   $lines = StringSplit($lines, "|")
   Global $servicerealname[$lines[0]]
   Global $servicename[$lines[0]]
   Global $servicestatus[$lines[0]]
   Global $service1occur[$lines[0]]
   Global $service2occur[$lines[0]]
   
   For $i = 1 to ($lines[0] - 1)
      $service1occur[$i] = StringInStr($lines[$i], " : ", "", 1)
      $service2occur[$i] = StringInStr($lines[$i], " : ", "", 2)
      $servicerealname[$i] = StringLeft($lines[$i], ($service1occur[$i] - 1))
      $servicename[$i] = StringMid($lines[$i], ($service1occur[$i] + 3) , ($service2occur[$i] - $service1occur[$i] - 3))
      $servicestatus[$i] = StringTrimLeft($lines[$i], $service2occur[$i] + 2)
      GUICtrlSetData($combos, $servicerealname[$i], "|")
   Next

I hope that's enough code to understand what I'm doing.

Link to comment
Share on other sites

The main line is ControlSend($GUI, "", $List_1, "{PgDn}")

where $GUI is the return value of GuiCreate and $List_1 is the return of GuiCtrlCreateList

Call that line after you make any change to the listbox

Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!
Link to comment
Share on other sites

The main line is ControlSend($GUI, "", $List_1, "{PgDn}")

where $GUI is the return value of GuiCreate and $List_1 is the return of GuiCtrlCreateList

Call that line after you make any change to the listbox

<{POST_SNAPBACK}>

That's what I gathered, I tried that but when I do it just displays the last item in the list still. Thanks for your help, it's greatly appreciated.
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...