AutoIt GUICtrlCreateListViewItem Function

How to use the AutoIt GUICreateListViewItem to create items in ListView GUI controls.

1869 views

Edited: 2018-08-18 12:47

The AutoIt GUICtrlCreateListViewItem function is used to create items in a ListView control.

To create an item in a control, you will have to specify the text, of the item to be created, and the listviewID in the second parameter.

Parameters

textRequiredThe text value of the item – if multiple columns, separated by pipe "|" characters, or the character specified by Opt("GUIDataSeparatorChar")
listviewIDOptionalThe text of the window to activate.

Example

The below example shows both how to create a ListView control, and how to insert items into the list – you may also want to follow the AutoIt tutorial mentioned earlier.

 #include <GUIConstantsEx.au3>
 #include <ListviewConstants.au3>
 
 Opt("GUIOnEventMode", 1)

MainGUI()

 ; ----- GUIs
Func MainGUI()
  Global $listview
  $listGUI = GUICreate("AutoIt GUICtrlCreateListView Example", 400, 200, 100, 200, -1)
  GUISetOnEvent($GUI_EVENT_CLOSE, "OnClose")
  $listview = GUICtrlCreateListView("Username|Password", 10, 10, 200, 150, $LVS_NOSORTHEADER+$LVS_SINGLESEL,$LVS_EX_GRIDLINES)

  ; -   -    -    -    -    -    -    -    -    -   -
  ; In this section we insert list items
  GUICtrlCreateListViewItem("John|1234", $listview)
  GUICtrlCreateListViewItem("AutoItUser|153_4", $listview)
  GUICtrlCreateListViewItem("Jacob|792Tka", $listview)
  ; -   -    -    -    -    -    -    -    -    -   -

  $BtnSelect = GUICtrlCreateButton("Select", 100, 165, 80, 30)
  GUICtrlSetOnEvent(-1, "SelectItem")

  GUISetState()
  
  While 1
    Sleep(10)
  WEnd
EndFunc
 ; ///// Functions
Func SelectItem()
  $sItem = GUICtrlRead(GUICtrlRead($listview))
  $sItem = StringTrimRight($sItem, 1) ; Will remove the pipe "|" from the end of the string
  MsgBox(0, "Selected Item", $sItem)
EndFunc
  
Func OnClose()
   Exit
EndFunc

Tell us what you think:

  1. How to create a list of selectable items with AutoIt.
  2. Tutorial on how to make GUIs using the AutoIt scripting language.
  3. How to disable and enable AutoIt GUI elements using GUICtrlSetState.
  4. How to interrupt running functions and handle system events in AutoIt GUI scripting.
  5. Tutorial on how to add images to AutoIt GUIs while maintaining aspect ratio.

More in: AutoIt GUIs