Jump to content

Add .txt to GUICtrlCreateListView


PINTO1927
 Share

Recommended Posts

Hi Guys,

I'm working on this project:

$Import = GUICtrlCreateButton("Import", 15, 175, 90, 40, $WS_GROUP)
$List = GUICtrlCreateListView("Name|Address|E-mail", 15, 220, 400, 363, $LVS_SORTDESCENDING)
$Item1 = GUICtrlCreateListViewItem("test|test|test", $List)
GUISetState(@SW_SHOW, $GUI)

While 1
   $nMsg = GUIGetMsg()
   Switch $nMsg
   Case $GUI_EVENT_CLOSE, $Exit
      Exit
   Case $Import
      $Table_Import = FileOpenDialog("Import list '*.txt'", @DesktopDir & "\", "Text (*.txt)")
      If @error Then ContinueLoop
      GUICtrlSetData($Item1, $Table_Import)

In the text file the fields are delimited with "|" and the values are not in the same row but one below the other.
However in the txt file the sum of the values may change.

Can you help me?

Thank's

Link to comment
Share on other sites

  • Developers
1 minute ago, PINTO1927 said:

Can you help me?

Help with what? What isn't working yet?

How exactly does that input file look?

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

Ok, that makes sense now. 
So for starters, you are not reading the file at all at the moment. FileOpenDialog retuns the filename, not its content.
You could read the whole file into an Array with _FileReadToArray() and then loop though the records adding them a line at the time with GUICtrlCreateListViewItem()

Making sense? :)

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

No idea what you mean with the first sentence. Which part of what I described is unclear and do you have questions about? 
You should be able to code it yourself when the process is clear.

Jos 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

No...  as I described: just loop through the records and add them one at the time. What is the issue with that?

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

We did it by setting this command

$Import = GUICtrlCreateButton("Import", 15, 175, 90, 40, $WS_GROUP)
$List = GUICtrlCreateListView("Name|Address|E-mail", 15, 220, 400, 363, $LVS_SORTDESCENDING)
$Item1 = GUICtrlCreateListViewItem("test|test|test", $List)
GUISetState(@SW_SHOW, $GUI)

While 1
   $nMsg = GUIGetMsg()
   Switch $nMsg
   Case $GUI_EVENT_CLOSE, $Exit
      Exit
   Case $Import
      $Table_Import = FileOpenDialog("Import list '*.txt'", @DesktopDir & "\", "Text (*.txt)")
      If @error Then ContinueLoop
      Local $Array_Lista
      ;============================================================ ADD TO GUICtrlCreateListView
      _FileReadToArray($Tabella_Importa, $Array_Lista)
      $Limite_Array = UBound($Array_Lista) - 1
      For $i = 1 To $Limite_Array
          $All_Box = String($Array_Lista[$i])
          GUICtrlCreateListViewItem($All_Box, $Lista)
      Next

 

Link to comment
Share on other sites

  • Developers

Is that now working as you want or is there still a question as I don't understand the last question posted?

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

I managed to obtain import the text file, and I have the result we wanted (see Annex).
Now I would like, if a row is selected, and then pressed the button "Sele", the data "NAME & SURNAME | ADDRESS | EMAIL" is transferred into their inputbox you see on the right.

test.png

Link to comment
Share on other sites

45 minutes ago, PINTO1927 said:

I managed to obtain import the text file, and I have the result we wanted (see Annex).
Now I would like, if a row is selected, and then pressed the button "Sele", the data "NAME & SURNAME | ADDRESS | EMAIL" is transferred into their inputbox you see on the right.

test.png

 

Link to comment
Share on other sites

Pinto1927,

This is a demo listview showing how to create a listview and read row values based on the row clicked.  Hope you find it useful.

#include <ListViewConstants.au3>
#include <StructureConstants.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <Array.au3>
#include <GuiListView.au3>

Local $Clients = _
        [["1560", "Ben", "Age: 25", "Bla bla...", "Bla bla..."], _
        ["1025", "Somebody", "Age: 14", "Bla bla...", "Bla bla..."], _
        ["9684", "John", "Age: 36", "Bla bla...", "Bla bla..."], _
        ["1002", "Laura", "Age: 15", "Bla bla...", "Bla bla..."], _
        ["1234", "Another guy", "Age: 18", "Bla bla...", "Bla bla..."]]

;Create simple GUI
$Form1 = GUICreate("Select client:", 150, 200)

;Create list of clients
$List_Clients = GUICtrlCreateListView("ID|Name", 10, 10, 130, 200)

; dummy control actioned by notify routine when user clicks on control
$dummy_detail = GUICtrlCreateDummy()

GUISetState(@SW_SHOW)

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

; populate listview columns with ID and NAME
For $i = 0 To UBound($Clients) - 1
    _GUICtrlListView_AddItem($List_Clients, $Clients[$i][0])
    _GUICtrlListView_AddSubItem($List_Clients, $i, $Clients[$i][1], 1)
Next

; distribute column widths evenly to show all data
For $i = 0 To _GUICtrlListView_GetColumnCount($List_Clients)
    _GUICtrlListView_SetColumnWidth($List_Clients, $i, $LVSCW_AUTOSIZE_USEHEADER)
Next

While 1
    Switch GUIGetMsg()
        Case $gui_event_close
            Exit
            ; this control is actioned by the notification routine
        Case $dummy_detail
            _get_client_detail(_GUICtrlListView_GetItemTextString($List_Clients, -1))
    EndSwitch

WEnd

Func _get_client_detail($str)

    $str = StringRegExpReplace($str, '([^\|])\|.*', '$1')

    Local $out
    For $i = 0 To UBound($Clients) - 1
        If $Clients[$i][0] <> $str Then ContinueLoop
        For $j = 0 To UBound($Clients, 2) - 1
            $out &= $Clients[$i][$j] & ' '
        Next
    Next
    MsgBox(0, 'CLIENT DETAIL', $out)

EndFunc   ;==>_get_client_detail

; routine to run when user clicks on any entry in the listview
Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)

    Local $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)

    Switch $tNMHDR.IDFrom
        Case $List_Clients
            Switch $tNMHDR.Code
                Case $nm_click
                    GUICtrlSendToDummy($dummy_detail) ; action dummy control in the message loop
            EndSwitch
    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

kylomas

Edited by kylomas
clarification

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Hi kylomas, and thanks for the help.
hi kylomas, and thanks for the help.
The problem is that the data inside the GUICtrlCreateListView are imported through a text file, accordingly the time when I try to return this value in a row, brings me 0.
I have created a "Sele" button to simulate the data transfer.

Case $Sele
     $SeleRow = _GUICtrlListView_GetSelectedIndices($List, 0)
     $aItem = _GUICtrlListView_GetItemTextArray($List, 1)
     MsgBox(0, "", $aItem)

With this I try to import the text in the row, but the value is 0.

Something wrong?

Link to comment
Share on other sites

You're returning an array with the $aItem line, are you reading it as an array or as a simple variable?

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Pinto1927,

See if this makes sense...

#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <MsgBoxConstants.au3>
#include <StringConstants.au3>

GUICreate("E-Mail", 300, 200)
Local $idSelBtn = GUICtrlCreateButton('Sele', 10, 05, 40, 20)
Local $idListview = GUICtrlCreateListView("Name|Location|Email Address", 10, 30, 280, 180, $LVS_SORTDESCENDING)
_GUICtrlListView_SetColumnWidth($idListview, 2, $LVSCW_AUTOSIZE_USEHEADER)

; populate listview from text file...columns are "|" delimited

Local $aTextFile = StringSplit(FileRead(@ScriptDir & '\lv.test.txt'), @CRLF, $STR_ENTIRESPLIT)
For $i = 1 To $aTextFile[0]
    If $aTextFile[$i] = '' Then ContinueLoop
    GUICtrlCreateListViewItem($aTextFile[$i], $idListview)
Next

GUISetState(@SW_SHOW)

Local $aIndices, $out

While 1
    Switch GUIGetMsg()
        Case $idSelBtn
            ; get # of listview rows selected
            $aIndices = _GUICtrlListView_GetSelectedIndices($idListview, True)
            $out = ''
            ; loop thru selected indicies and format out
            For $i = 1 To $aIndices[0]
                $out &= _GUICtrlListView_GetItemTextString($idListview, $aIndices[$i]) & @CRLF
            Next
            MsgBox(0, 'Email Selection', $out)
        Case $GUI_EVENT_CLOSE
            GUIDelete()
            Exit
    EndSwitch
WEnd

Input file...lv.test.txt

As BrewmanNH said you are trying to display an array as a simple variable.  If you want to display an array use _arraydisplay($yourarray).

kylomas

edit: same code addressing your other question...

#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <MsgBoxConstants.au3>
#include <StringConstants.au3>

GUICreate("E-Mail", 300, 200)
Local $idSelBtn = GUICtrlCreateButton('Sele', 10, 05, 40, 20)
Local $idListview = GUICtrlCreateListView("Name|Location|Email Address", 10, 30, 280, 180, $LVS_SORTDESCENDING)
_GUICtrlListView_SetColumnWidth($idListview, 2, $LVSCW_AUTOSIZE_USEHEADER)

; populate listview from text file...columns are "|" delimited

Local $aTextFile = StringSplit(FileRead(@ScriptDir & '\lv.test.txt'), @CRLF, $STR_ENTIRESPLIT)
For $i = 1 To $aTextFile[0]
    If $aTextFile[$i] = '' Then ContinueLoop
    GUICtrlCreateListViewItem($aTextFile[$i], $idListview)
Next

GUISetState(@SW_SHOW)

Local $aIndices, $out

While 1
    Switch GUIGetMsg()
        Case $idSelBtn
            ; get # of listview rows selected
            $aIndices = _GUICtrlListView_GetSelectedIndices($idListview, True)
            $out = ''
            ; loop thru selected indicies and format out
            For $i = 1 To $aIndices[0]
                $out &= _GUICtrlListView_GetItemText($idListview, $aIndices[$i],0) & @TAB
                $out &= _GUICtrlListView_GetItemText($idListview, $aIndices[$i],2) & @CRLF
            Next
            MsgBox(0, 'Email Selection', $out)
        Case $GUI_EVENT_CLOSE
            GUIDelete()
            Exit
    EndSwitch
WEnd

 

Edited by kylomas
additional info

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

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

×
×
  • Create New...