Jump to content
Sign in to follow this  
Nova

GUICtrlCreateListViewItem

Recommended Posts

Nova

Hi again,

Rite ive never used the GUICtrlCreateListView functions before and im having a small bit of trouble !

The below code creates a gui with a listview which has one item !

I cant figure out how to select an item from the listview !

I would like a msg box to appear when an item from the list is double clicked !

Or a context menu to apear when an item is right clicked !

Ive been messing with ideas but cant get any to work !

#include <GUIConstants.au3>

GUICreate("Nova Server",800,400, 100,200)
$listview = GUICtrlCreateListView ("Computer Name   |Os Version  |Network Address |Ip Address    ",10,10,780,380, -1 , 0x1 + 0x20)
GUISetState()

$item = GUICtrlCreateListViewItem ( "Comp 1", $listview )

While 1
  sleep (10)
WEnd

Little help ?

Edit : Added below code an example of my attemp to add a context menu to an item in a list !

#include <GUIConstants.au3>

GUICreate("My GUI Context Menu",300,200)

$listview = GUICtrlCreateListView ("col1  |col2|col3  ",10,10,200,150);,$LVS_SORTDESCENDING)
$item1=GUICtrlCreateListViewItem("item2|col22|col23",$listview)

$context = GUICtrlCreateContextMenu($item1)
$item = GUICtrlCreateMenuitem("About button",$context)

GUISetState ()

; Run the GUI until the dialog is closed
While 1
    $msg = GUIGetMsg()
    
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
Wend

Edit : Also added below an example of my attemp to add a GuiOnEventMode idea

#include <GUIConstants.au3>
Opt("GuiOnEventMode", 1) 

GUICreate("My GUI Context Menu",300,200)

$listview = GUICtrlCreateListView ("col1  |col2|col3  ",10,10,200,150,$LVS_SINGLESEL)
$item1=GUICtrlCreateListViewItem("item2|col22|col23",$listview)

GUISetState ()

GUICtrlSetOnEvent($item1, "Item1")

; Run the GUI until the dialog is closed
While 1
    $msg = GUIGetMsg()
    
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
Wend

Func Item1 ()
 MsgBox(0, "Test", "Hello") 
EndFunc
Edited by Nova

Share this post


Link to post
Share on other sites
Nova

Edit : Double post sry !!!

Edited by Nova

Share this post


Link to post
Share on other sites
GaryFrost

I realize this seems a little lengthy but I'm using the latest release version, believe

it might be easier in the next version

but here's an example

#include <GUIConstants.au3>

GUICreate("My GUI Context Menu",300,200)

$listview = GUICtrlCreateListView ("col1  |col2|col3  ",10,10,200,150);,$LVS_SORTDESCENDING)
$item1=GUICtrlCreateListViewItem("item2|col22|col23",$listview)

$context = GUICtrlCreateContextMenu($item1)
$item = GUICtrlCreateMenuitem("About button",$context)

GUISetState ()

; Run the GUI until the dialog is closed
While 1
    $msg = GUIGetMsg()
     select
     Case $msg = $GUI_EVENT_SECONDARYDOWN
         If (ControlListView("My GUI Context Menu", "", $listview , "GetSelectedCount")) Then
             $COUNT = ControlListView("My GUI Context Menu", "", $listview, "GetSubItemCount")
             For $X = 0 To $COUNT - 1 
                MsgBox(0,"",ControlListView("My GUI Context Menu", "", $listview, "GetText", ControlListView("My GUI Context Menu", "", $listview, "GetSelected"), $X))
            Next
        EndIf
    EndSelect
    
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
Wend

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites
GaryFrost

here's a second example:

#include <GUIConstants.au3>

GUICreate("My GUI Context Menu",300,200)

$listview = GUICtrlCreateListView ("col1  |col2|col3  ",10,10,200,150);,$LVS_SORTDESCENDING)
$item1=GUICtrlCreateListViewItem("item2|col22|col23",$listview)

$context = GUICtrlCreateContextMenu($item1)
$item = GUICtrlCreateMenuitem("About button",$context)

GUISetState ()

; Run the GUI until the dialog is closed
While 1
    $msg = GUIGetMsg()
     select
     Case $msg = $GUI_EVENT_SECONDARYDOWN
         If (ControlListView("My GUI Context Menu", "", $listview , "GetSelectedCount")) Then
             $COUNT = ControlListView("My GUI Context Menu", "", $listview, "GetSubItemCount")
             For $X = 0 To $COUNT - 1 
                MsgBox(0,"",_GUICtrlLVGetItemText($listview, "My GUI Context Menu", "", -1, $X))
            Next
            MsgBox(0,"",_GUICtrlLVGetItemText($listview, "My GUI Context Menu"))
        EndIf
    EndSelect
    
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
Wend

;===============================================================================
; Function:      _GUICtrlLVGetItemText
; Author:        Gary Frost
;
; Description:       Get the text if the item (all or 1 subitem)
; Parameters:        $lv - The ListView Control to get the text from
;                         $WindowTitle - The Title to the window the control resides on
;                         [$WindowText] - The Text from the window the control resides on
;                        [$ITEM] - The row (item) to get from
;                         [$SubItem] - The SubItem to get the text from
; Requirements:  None
; Return Values: Text from item or error
;
; Note:          None
;===============================================================================
Func _GUICtrlLVGetItemText($LV, $WINDOWTITLE, $WINDOWTEXT = "", $ITEM = -1, $SUBITEM = -1)
   Local $X, $COUNT, $STR
   $COUNT = ControlListView($WINDOWTITLE, $WINDOWTEXT, $LV, "GetSubItemCount")
   if (ControlListView($WINDOWTITLE, $WINDOWTEXT, $LV, "GetSelectedCount")) Then
      If ($SUBITEM == -1) Then; return all the subitems in the item selected
         For $X = 0 To $COUNT - 1 Step 1
            If ($STR) Then
               If ($ITEM == -1) Then
                  $STR = $STR & "|" & ControlListView($WINDOWTITLE, $WINDOWTEXT, $LV, "GetText", _ 
                                                    ControlListView($WINDOWTITLE, $WINDOWTEXT, $LV, "GetSelected"), $X)
               Else
                  $STR = $STR & "|" & ControlListView($WINDOWTITLE, $WINDOWTEXT, $LV, "GetText", $ITEM, $X)
               EndIf
            Else
               If ($ITEM == -1) Then
                  $STR = ControlListView($WINDOWTITLE, $WINDOWTEXT, $LV, "GetText", _ 
                                    ControlListView($WINDOWTITLE, $WINDOWTEXT, $LV, "GetSelected"), $X)
               Else
                  $STR = ControlListView($WINDOWTITLE, $WINDOWTEXT, $LV, "GetText", $ITEM, $X)
               EndIf
            EndIf
         Next
         Return $STR
      ElseIf ($SUBITEM < $COUNT) Then; return the subitem in the item selected
         If ($ITEM == -1) Then
            Return ControlListView($WINDOWTITLE, $WINDOWTEXT, $LV, "GetText", _ 
                            ControlListView($WINDOWTITLE, $WINDOWTEXT, $LV, "GetSelected"), $SUBITEM)
         Else
            Return ControlListView($WINDOWTITLE, $WINDOWTEXT, $LV, "GetText", $ITEM, $SUBITEM)
         EndIf
      Else
        ; set an error, subitem is out of range
         SetError(-1)
         Return @error
      EndIf
   Else
    ; set an error, no item selected
      SetError(-2)
      Return @error
   EndIf
EndFunc  ;==>_GUICtrlLVGetItemText

Edit: the above function currently only works for single selection haven't taken the time to add multiple row selections to it yet.

Example of creating the context menu on the fly:

Dim $listcontext,$subitems[1]
; Run the GUI until the dialog is closed
While 1
    $msg = GUIGetMsg()
     select
     Case $msg = $GUI_EVENT_PRIMARYDOWN
         If (ControlListView("My GUI Context Menu", "", $listview , "GetSelectedCount")) Then
            $vars = StringSplit(_GUICtrlLVGetItemText($listview, "My GUI Context Menu"),"|")
            ReDim $subitems[$vars[0]]
            If($listcontext) Then GUICtrlDelete($listcontext)
            $listcontext = GUICtrlCreateContextMenu($listview)
            For $X = 1 To $vars[0]
                $subitems[$X - 1] = GUICtrlCreateMenuitem($vars[$X],$listcontext)
                If($X < $vars[0]) Then GUICtrlCreateMenuitem("",$listcontext)
            Next
        EndIf
    EndSelect
    
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
Wend
Edited by gafrost

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites
Nova

Tnx for the reply gafrost.

The following is close to one idea im trying to impliment !

Altho I only want the msgbox to appear when the item is doubled clicked in quick sucession !

#include <GUIConstants.au3>

GUICreate("My GUI Context Menu",300,200)

$listview = GUICtrlCreateListView ("col1  |col2|col3  ",10,10,200,150);,$LVS_SORTDESCENDING)
$item1=GUICtrlCreateListViewItem("item2|col22|col23",$listview)

$context = GUICtrlCreateContextMenu($item1)
$item = GUICtrlCreateMenuitem("About button",$context)

GUISetState ()

; Run the GUI until the dialog is closed
While 1
    $msg = GUIGetMsg()
    select
        Case $msg = ($GUI_EVENT_PRIMARYDOWN)
            If (ControlListView("My GUI Context Menu", "", $listview , "GetSelectedCount")) Then
                MsgBox(0,"Test","Hello")
            EndIf
    EndSelect
    
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
Wend

As for your contex menu code ! Your code creates a contextmenu for the whole list ! I want individual context menus for each item !

The following code is an example of what Im trying to do !

I think the problem is maby you cant create a contextmenu for a list item

#include <GUIConstants.au3>

GUICreate("My GUI Context Menu",300,200)

$listview = GUICtrlCreateListView ("col1  |col2|col3  ",10,10,200,150);,$LVS_SORTDESCENDING)
$item1=GUICtrlCreateListViewItem("item2|col22|col23",$listview)

$context = GUICtrlCreateContextMenu($item1)
$item = GUICtrlCreateMenuitem("About button",$context)

GUISetState ()

; Run the GUI until the dialog is closed
While 1
    $msg = GUIGetMsg()
    select
        Case $msg = ($GUI_EVENT_PRIMARYDOWN)
            If (ControlListView("My GUI Context Menu", "", $listview , "GetSelectedCount")) Then
                $listcontext = GUICtrlCreateContextMenu($item1)
                GUICtrlCreateMenuitem("About",$listcontext)
            EndIf
    EndSelect
    
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
Wend

Anyidea ?

Edited by Nova

Share this post


Link to post
Share on other sites
GaryFrost

here's an example of a double click

#include <GUIConstants.au3>

GUICreate("My GUI Context Menu",300,200)

$listview = GUICtrlCreateListView ("col1  |col2|col3  ",10,10,200,150);,$LVS_SORTDESCENDING)
$item1=GUICtrlCreateListViewItem("item2|col22|col23",$listview)

$context = GUICtrlCreateContextMenu($item1)
$item = GUICtrlCreateMenuitem("About button",$context)

GUISetState ()
Dim $DIFF, $START,$DOUBLECLICKED,$MOUSESPEED
; Run the GUI until the dialog is closed
While 1
    $msg = GUIGetMsg()
    select
        Case $msg = ($GUI_EVENT_PRIMARYDOWN)
            Local $FOCUS = ControlGetFocus("My GUI Context Menu")
            If ($FOCUS == "SysListView321") Then
               $DIFF = TimerDiff($START)
               If ($MOUSESPEED = "") Then
                  $MOUSESPEED = 500
               EndIf
               If ($DIFF < $MOUSESPEED And $DOUBLECLICKED == 0) Then
                  $ITEMCLICKED = GUICtrlRead($listview)
                  If ($ITEMCLICKED > 0) Then
                            MsgBox(0,"Test","Hello")
                        EndIf
                  $DOUBLECLICKED = 1
               Else
                  $DOUBLECLICKED = 0
               EndIf
               $START = TimerInit()
            EndIf
    EndSelect
    
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
Wend

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites
Nova

@gafrost : Tnx a million for your help so far man.

The above code for the double click works perfectly ! but the context menu dosent work for me at all ! Is it working for u ?

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  

×