Jump to content

load text file to listbox


Recommended Posts

noob here;

I'm stuck and can't find any examples on what to do next. Let me explain what I'm trying to accomplish and post my code so far. I want to load a text file in a listbox (sort the list too) with username and ip address with the following format.

jdoe,192.168.0.3

bcole,192.168.0.5

sallen,192.168.0.7

I would like to be able to click on a user and be able to set the ip address to a variable. I have more planned after that, but I've already what to do to accomplish that. I just don't know how to load the file into the list box sorted and be able to split the record to assign the ip address only to a variable.

#include <GUIConstants.au3>
#Include <File.au3>
#Include <Array.au3>

GUICreate("Users", 300, 600)

$remote = GUICtrlCreateButton ("Remote",  90, 30, 100)
$refresh = GUICtrlCreateButton ( "Refresh List", 90,60,100)
$users = GUICtrlCreateList ( "User List", 10,100,280,300)

$file = FileOpen("c:\citrix\logon.txt", 0)

If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

FileClose($file)


GUISetState ()
While 1
    $msg = GUIGetMsg()
    Select

        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
                    
        Case $msg = $refresh
           ;some code here to refresh the list
            
        Case $msg = $remote
        ;some code here to extract the ip address and place it in a variable
            
EndSelect
Wend
Link to comment
Share on other sites

Opening a file in Read mode dosn't read the file for you.

For your question I would be looking at _FileReadToArray() and then load the list from the array.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Opening a file in Read mode dosn't read the file for you.

For your question I would be looking at _FileReadToArray() and then load the list from the array.

I think I'm closer. I have it to load, however I don't know how to use the stringsplit to extract the ip address only. Sorting the list from A to Z would be nice too.

#include <GUIConstantsEx.au3>
#include <GUIConstants.au3>
#Include <File.au3>
#Include <Array.au3>
#Include <GuiListView.au3>

GUICreate("Users", 300, 600)

; Read the file into an array
Global $aLines[1]
_FileReadToArray("c:\citrix\logon.txt", $aLines)
$remote = GUICtrlCreateButton ("Remote",  90, 30, 100)
$refresh = GUICtrlCreateButton ( "Refresh List", 90,60,100)
;$users = GUICtrlCreateList ( "", 10,100,280,300)
$hListView = GUICtrlCreateListView(" ", 10, 100, 280, 300)
_GUICtrlListView_SetColumnWidth($hListView, 0, $LVSCW_AUTOSIZE_USEHEADER)
$iIndex_Base = GUICtrlCreateDummy()
Fill_ListView()





GUISetState ()
While 1
    $msg = GUIGetMsg()
    Select

        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
                    
        Case $msg = $refresh
           ;some code here to refresh the list
            
        Case $msg = $remote
        ;some code here to extract the ip address and place it in a variable
            
EndSelect
Wend


Func Fill_ListView()
    For $i = 1 To $aLines[0]
        GUICtrlCreateListViewItem($aLines[$i], $hListView)
    Next
EndFunc
Link to comment
Share on other sites

  • Moderators

Tigerweld,

Based on the file format in your original post:

jdoe,192.168.0.3

bcole,192.168.0.5

sallen,192.168.0.7

the point to split the line would be "," - so use StringSplit thus:
$aSplitLine = StringSplit("the_line_to_split", ",")

Using "jdoe,192.168.0.3" as an example, this will create the $aSplitLine array with the following elements:

[0] = 2
[1] = jdoe
[2] = 192.168.0.3

So the variable you require is $aSplitLine[2]. ;-)

As to sorting, look at the _GUICtrlListView UDFs in the Help file. The easiest is _GUICtrlListView_SimpleSort - but "easiest" is, of course, a relative term!

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

ok I understand that, but does this get the complete string on the selected line? I tried using it and I get errors. Not really sure how to use the $aSplitLine[2]

Case $msg = $remote
            $aSplitLine = StringSplit("the_line_to_split", ",")
            MsgBox(0, "variable value", "variable value is " & $aSplitLine[2])
Link to comment
Share on other sites

Sort the array right after you create it

_FileReadToArray("c:\citrix\logon.txt", $aLines)
_ArraySort($aLines, 0, 1)

Edit 1:Actually you should also be able to use this to populate the ListView

For $i = 1 To Ubound($aLines) -1
    $aRegEx = StringRegExp($aLines[$i], "(?i)(\w*),(\d.*\d)", 1)
    If NOT @Error Then
        $Col1_data = $aRegEx[0]
        $Col2_data = $aRegEx[1]
        MsgBox(0, "Results", $Col1_Data & @CRLF & @CRLF & $Col2_Data)
    EndIf
Next

EDIT 2:I copied you examples into a text file named logon.txt and then ran this. It returned what I expected (and what you have asked for)

#include<file.au3>
#include<array.au3>
Global $aLines
_FileReadToArray(@ScriptDir & "\logon.txt", $aLines)
_ArraySort($aLines, 0, 1)

For $i = 1 To Ubound($aLines) -1
    $aRegEx = StringRegExp($aLines[$i], "(?i)(\w*),(\d.*\d)", 1)
    If NOT @Error Then
        $Col1_data = $aRegEx[0]
        $Col2_data = $aRegEx[1]
        MsgBox(0, "Results", $Col1_Data & @CRLF & @CRLF & $Col2_Data)
    EndIf
Next
Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

  • Moderators

Tigerweld,

Use this in your Case statement:

Case $msg = $remote
    $iIndex = GUICtrlRead($hListView) - $iIndex_Base
    $sString = $aLines[$iIndex]
    $aSplitLine = StringSplit($sString, ",")
    ConsoleWrite($aSplitLine[2] & @CRLF)

If your input file is of the same format as you posted, this gives you the IP address in the variable $aSplitLine[2] - which is what you wanted.

Not really sure how to use the $aSplitLine[2]

What do you want to do with it?

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Actually I think this reproduces what you want without StringSplits, _FileReadToArray() OR _ArraySort()

Again I just copied your example to the text file.

#include<ListviewConstants.au3>
Global $aLines
GUICreate("Test GUI")
$hList = GUICtrlCreateListView("Name|IP", 10, 10, 150, 100, _
BitOr($GUI_SS_DEFAULT_LISTVIEW, $LVS_SORTASCENDING, $LVS_SINGLESEL))
$aRegEx = StringRegExp(FileRead(@ScriptDir & "\logon.txt"), "(?i)(\w*),(\d.*\d)", 3)
$hStart = GUICtrlCreateDummy()
For $i = 0 To Ubound($aRegEx) -2 Step 2
 GUICtrlCreateListViewItem($aRegEx[$i] & "|" & $aRegEx[$i+1], $hList)
Next
$hEnd = GUICtrlCreateDummy()
GUISetState()

While 1
   $msg = GUIGetMsg()
   Switch $Msg
      Case -3
         Exit
      Case $hStart To $hEnd
         $iItem = ControlListView("","",$hList,"GetSelected")
         $sname = ControlListView("","",$hList,"GetText", $iItem, 0)
         $sIP = ControlListView("","",$hList,"GetText", $iItem, 1)
         MsgBox(0,"Results","Name:  " & $sName & @CRLF & @CRLF & "IP:  " & $sIP)
   EndSwitch
Wend

Edit: Added a couple of dummy controls for reference points and showed a method of returning the results if an item is clicked.

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

  • 2 weeks later...

GEOSoft, how can I add one more variable to the columns? I want to add variable sHostName to the text file. My new text file looks like

jdoe,192.168.0.3,MXL53109JK

bcole,192.168.0.5,MXL5309GB

sallen,192.168.0.7,DD80521

Thanks!

Actually I think this reproduces what you want without StringSplits, _FileReadToArray() OR _ArraySort()

Again I just copied your example to the text file.

#include<ListviewConstants.au3>
Global $aLines
GUICreate("Test GUI")
$hList = GUICtrlCreateListView("Name|IP", 10, 10, 150, 100, _
BitOr($GUI_SS_DEFAULT_LISTVIEW, $LVS_SORTASCENDING, $LVS_SINGLESEL))
$aRegEx = StringRegExp(FileRead(@ScriptDir & "\logon.txt"), "(?i)(\w*),(\d.*\d)", 3)
$hStart = GUICtrlCreateDummy()
For $i = 0 To Ubound($aRegEx) -2 Step 2
 GUICtrlCreateListViewItem($aRegEx[$i] & "|" & $aRegEx[$i+1], $hList)
Next
$hEnd = GUICtrlCreateDummy()
GUISetState()

While 1
   $msg = GUIGetMsg()
   Switch $Msg
      Case -3
         Exit
      Case $hStart To $hEnd
         $iItem = ControlListView("","",$hList,"GetSelected")
         $sname = ControlListView("","",$hList,"GetText", $iItem, 0)
         $sIP = ControlListView("","",$hList,"GetText", $iItem, 1)
         MsgBox(0,"Results","Name:  " & $sName & @CRLF & @CRLF & "IP:  " & $sIP)
   EndSwitch
Wend

Edit: Added a couple of dummy controls for reference points and showed a method of returning the results if an item is clicked.

Link to comment
Share on other sites

Dim $aLines[4] = [3, 'jdoe,192.168.0.3,MXL53109JK', 'bcole,192.168.0.5,MXL5309GB', 'sallen,192.168.0.7,DD80521']
Dim $hGUI
Dim $ListView
Dim $iOldDelimiter
$hGUI = GUICreate('Test', 300, 250)
$ListView = GUICtrlCreateListView('Name  |IP         |Host-name', 0, 0, 300, 200)

$iOldDelimiter = Opt('GUIDataSeparatorChar', ',')
For $i = 1 To $aLines[0]
    GUICtrlCreateListViewItem($aLines[$i], $ListView)
Next

Opt('GUIDataSeparatorChar', $iOldDelimiter)
GUISetState()

Do
Until GUIGetMsg() = -3

GUIDelete()
Exit

Link to comment
Share on other sites

Here is my code modified for the extra column. If you compare it with the last you will see the differences

;
#include<ListviewConstants.au3>
Global $aLines
GUICreate("Test GUI")
$hList = GUICtrlCreateListView("Name|IP|Host Name", 10, 10, 200, 100, _
BitOr($GUI_SS_DEFAULT_LISTVIEW, $LVS_SORTASCENDING, $LVS_SINGLESEL))
$aRegEx = StringRegExp(FileRead(@ScriptDir & "\logon.txt"), "(?i)(\w*),(\d.*\d),(\w*)", 3)
$hStart = GUICtrlCreateDummy()
For $i = 0 To Ubound($aRegEx) -3 Step 3
GUICtrlCreateListViewItem($aRegEx[$i] & "|" & $aRegEx[$i+1] & "|" & $aRegex[$i+2], $hList)
Next
$hEnd = GUICtrlCreateDummy()
GUISetState()

While 1
   $msg = GUIGetMsg()
   Switch $Msg
      Case -3
         Exit
      Case $hStart To $hEnd
         $iItem = ControlListView("","",$hList,"GetSelected")
         $sname = ControlListView("","",$hList,"GetText", $iItem, 0)
         $sIP = ControlListView("","",$hList,"GetText", $iItem, 1)
         $sHost = ControlListView("","",$hList,"GetText", $iItem, 2)
         MsgBox(0,"Results","Name:  " & $sName & @CRLF & @CRLF & "IP:  " & $sIP & _
         @CRLF & @CRLF & "Host:  " & $sHost)
   EndSwitch
Wend
;

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

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...