Jump to content

Read content of text file with combo box


Recommended Posts

Hello autoit forums,

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <MsgBoxConstants.au3>
#include <File.au3>

Global $TXT_LIST_FILENAME = "List.txt"
Global $TXT_LIST_FILENAME_PATH = @ScriptDir & "\" & $TXT_LIST_FILENAME

Opt("GUIOnEventMode", 1)
#Region ### START Koda GUI section ###
;========================== G U I ==================================================================>
$Form1 = GUICreate("Shopping List", 615, 301, 192, 124)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form1_Exit")
;========================== C O M B O ==================================================================>
$Combo1 = GUICtrlCreateCombo("<Select Item>", 16, 48, 145, 25, BitOR($CBS_DROPDOWNLIST + $WS_VSCROLL, $CBS_AUTOHSCROLL))
GUICtrlSetOnEvent(-1, "Combo1Change")
;========================== B U T T O N ==================================================================>
$Button1 = GUICtrlCreateButton("Save", 192, 48, 75, 25)
GUICtrlSetOnEvent(-1, "Button1Click_Save")
$Button2 = GUICtrlCreateButton("Delete", 280, 48, 75, 25)
GUICtrlSetOnEvent(-1, "Button2Click_Delete")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    Sleep(100)
WEnd

Func Button1Click_Save()
    $sComboRead = GUICtrlRead($Combo1)
    FileWrite(@ScriptDir & "\NewList.txt", $sComboRead)
EndFunc
Func Button2Click_Delete()
    $old_file = FileDelete(@ScriptDir & "\NewList.txt")
    MsgBox($MB_ICONINFORMATION, "Delete", "Delete file: " & $old_file)
EndFunc
Func Combo1Change()
    Local $DB_Read = FileOpen($TXT_LIST_FILENAME_PATH, 0)
    For $i = 1 to _FileCountLines($DB_Read)
        $line = FileReadLine($DB_Read, $i)
        GUICtrlSetData($Combo1,StringStripCR($line)); add other items
    Next
    $sComboRead = GUICtrlRead($Combo1)
    GUICtrlSetData($Combo1, $sComboRead); add other items
    MsgBox($MB_ICONINFORMATION, "Read", "Selected item: " & $sComboRead)
    FileClose($DB_Read)
EndFunc
Func Form1_Exit()
    Exit -1
EndFunc

When running that code, there's nothing showing up in combobox but when click on the first item in the combobox, the contents of the text file are there but I cannot select any the highlighted item is always the last line of the text file

I don't want to use the other code where $nMsg = GUIGetMsg() for some reason, this method doesn't save data using many buttons, sorry I am new here

I want to use this to write data into a basic database I'm using sqlite, thank you in advance

Link to post
Share on other sites

You need to fill the combo before you start looping.  Also I don't understand the way your 3 buttons are supposed to act.   Could you explain ?

#include <Array.au3>
#include <File.au3>
#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <MsgBoxConstants.au3>

Global $TXT_LIST_FILENAME = "Test.txt"
Global $TXT_LIST_FILENAME_PATH = @ScriptDir & "\" & $TXT_LIST_FILENAME

Local $sList = _ArrayToString(FileReadToArray("Test.txt"))

Opt("GUIOnEventMode", 1)
#Region ### START Koda GUI section ###
;========================== G U I ==================================================================>
$Form1 = GUICreate("Shopping List", 615, 301, 192, 124)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form1_Exit")
;========================== C O M B O ==================================================================>
$Combo1 = GUICtrlCreateCombo("<Select Item>", 16, 48, 145, 25, BitOR($CBS_DROPDOWNLIST + $WS_VSCROLL, $CBS_AUTOHSCROLL))
GUICtrlSetOnEvent(-1, "Combo1Change")
GUICtrlSetData(-1, $sList)
;========================== B U T T O N ==================================================================>
$Button1 = GUICtrlCreateButton("Save", 192, 48, 75, 25)
GUICtrlSetOnEvent(-1, "Button1Click_Save")
$Button2 = GUICtrlCreateButton("Delete", 280, 48, 75, 25)
GUICtrlSetOnEvent(-1, "Button2Click_Delete")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    Sleep(100)
WEnd

Func Button1Click_Save()
;    $sComboRead = GUICtrlRead($Combo1)
;    FileWrite(@ScriptDir & "\NewList.txt", $sComboRead)
EndFunc
Func Button2Click_Delete()
;    $old_file = FileDelete(@ScriptDir & "\NewList.txt")
;    MsgBox($MB_ICONINFORMATION, "Delete", "Delete file: " & $old_file)
EndFunc
Func Combo1Change()
    $sComboRead = GUICtrlRead($Combo1)
;    GUICtrlSetData($Combo1, $sComboRead); add other items
    MsgBox($MB_ICONINFORMATION, "Read", "Selected item: " & $sComboRead)
EndFunc
Func Form1_Exit()
    Exit -1
EndFunc

 

Edited by Nine
Link to post
Share on other sites

Why this one works :

#include <MsgBoxConstants.au3>
#include <File.au3>
#include <GUIConstantsEx.au3>
#include <ComboConstants.au3>
#include <WindowsConstants.au3>

$TXT_LIST_FILENAME = "List.txt"
$TXT_LIST_FILENAME_PATH = @ScriptDir & "\" & $TXT_LIST_FILENAME

Opt("GUIOnEventMode", 1)
#Region ### START Koda GUI section ###
;========================== G U I ==================================================================>
$Form1 = GUICreate("Shopping List", 615, 301, 192, 124)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form1_Exit")
;========================== C O M B O ==================================================================>
$Combo1 = GUICtrlCreateCombo("<Select Item>", 16, 48, 145, 25, BitOR($CBS_DROPDOWNLIST + $WS_VSCROLL, $CBS_AUTOHSCROLL))
GUICtrlSetOnEvent(-1, "Combo1Change")
;========================== B U T T O N ==================================================================>
$Button1 = GUICtrlCreateButton("Save", 192, 48, 75, 25)
GUICtrlSetOnEvent(-1, "Button1Click_Save")
$Button2 = GUICtrlCreateButton("Delete", 280, 48, 75, 25)
GUICtrlSetOnEvent(-1, "Button2Click_Delete")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

Example()
Func Example()
    Local $TXT_Read = FileOpen($TXT_LIST_FILENAME_PATH, 0)
    Local $lines = _FileCountLines($TXT_Read)
    For $i = 1 To $lines
        $line = FileReadLine($TXT_Read, $i)
        GUICtrlSetData($Combo1, $line, "")
    Next
    FileClose($TXT_Read)
EndFunc

Func Button1Click_Save()
;    $sComboRead = GUICtrlRead($Combo1)
;    FileWrite(@ScriptDir & "\NewList.txt", $sComboRead)
; Write the highlighted text inside the combobox into a filename "NewList.txt"
EndFunc
Func Button2Click_Delete()
;    $old_file = FileDelete(@ScriptDir & "\NewList.txt")
;    MsgBox($MB_ICONINFORMATION, "Delete", "Delete file: " & $old_file)
; I want to delete the NewList because days have passed and I don't need it, I need to write new ingredients inside a default textfile "List.txt"
EndFunc
Func Combo1Change()
    $sComboRead = GUICtrlRead($Combo1)
;    GUICtrlSetData($Combo1, $sComboRead); add other items
    MsgBox($MB_ICONINFORMATION, "Read", "Selected item: " & $sComboRead)
    ; Here, I want to confirm if the selected item is correct, this is being read by the save button
EndFunc

Func Form1_Exit()
    Exit -1
EndFunc

While 1
    Sleep(250)
WEnd

Sorry new into scripting been started in a scratch from this community

Link to post
Share on other sites

Your example to fill combo box should be working.  But it is not the most elegant solution and would be slow as hell if you had a long list to fill.

The save button should add a new line to the file right ?  Cause right now it overwrites the file totally.

The delete button is working but FileDelete is not returning the name of the old_file but 0 or 1 depending of failure or success (see help file for more details)

ps.  tested with handle on _FileCountLines and it is working.  Help file should be reviewed.  Looking at this code, it has been changed to allow handle...

Edited by Nine
Link to post
Share on other sites
On 4/7/2021 at 11:55 PM, Nine said:

Your example to fill combo box should be working.  But it is not the most elegant solution and would be slow as hell if you had a long list to fill.

The save button should add a new line to the file right ?  Cause right now it overwrites the file totally.

The delete button is working but FileDelete is not returning the name of the old_file but 0 or 1 depending of failure or success (see help file for more details)

ps.  tested with handle on _FileCountLines and it is working.  Help file should be reviewed.  Looking at this code, it has been changed to allow handle...

My bad hehe

You're right, I shouldn't be using a file handle 😂

I've fixed save text file add new line

Tysm 🙏

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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...