Sign in to follow this  
Followers 0
Slash

edit textfile line

2 posts in this topic

Hi all!

I have a problem.

I want to create a combo box in each line of my text file is displayed.

If you choose the line they will be displayed in the editbox and you can edit it or you can delete it.

How am I doing this?

Heres my Code:

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

Global $Datenbank = @ScriptDir & "\data\data.txt"

If FileExists($Datenbank) Then
MsgBox(4096, "Überprüfen", "Datenbank ist Vorhanden")
Else
MsgBox(4096,"Überprüfen" , "Datenbank existiert nicht. Sie wird nun erstellt.")
_FileCreate($Datenbank)
FileWrite($Datenbank, "<normal>" & @CRLF)
FileWrite($Datenbank, "</normal>")
FileWrite($Datenbank, @CRLF & @CRLF)
FileWrite($Datenbank, "<hardcore>" & @CRLF)
FileWrite($Datenbank, "</hardcore>")
EndIf

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("ExoPlay", 554, 330, -1, -1)
$Edit1 = GUICtrlCreateEdit("", 16, 24, 521, 153)
$BtnNormal = GUICtrlCreateButton("Speichern Normal", 16, 184, 257, 33)
$BtnHardcore = GUICtrlCreateButton("Speichern Hardcore", 280, 184, 257, 33)
$Combo1 = GUICtrlCreateCombo("Wähle eine Aufgabe die gelöscht werden soll!", 16, 256, 521, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
$lines = _FileCountLines($Datenbank)
$Group1 = GUICtrlCreateGroup("Aufgaben erstellen", 8, 8, 537, 217)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group2 = GUICtrlCreateGroup("Aufgaben Editieren", 8, 232, 537, 89)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$BtnEdit = GUICtrlCreateButton("Aufgabe Editieren", 16, 280, 257, 33)
$BtnDelete = GUICtrlCreateButton("Aufgabe Löschen", 280, 280, 257, 33)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $BtnNormal
        _WriteBetweenTag($Datenbank, 'normal', GUICtrlRead($Edit1))
        MsgBox(4096,"Eingetragen", "Daten wurden in die Datenbank gespeichert")

        Case $BtnHardcore
        _WriteBetweenTag($Datenbank, 'hardcore', GUICtrlRead($Edit1))
        MsgBox(4096,"Eingetragen", "Daten wurden in die Datenbank gespeichert")
    EndSwitch
WEnd

Func _WriteBetweenTag($sFilePath, $sTag, $sText)
    Local $iLines = _FileCountLines($sFilePath)
    For $i = $iLines To 1 Step -1
        If FileReadLine($sFilePath, $i) = "</" & $sTag & ">" Then ExitLoop
    Next
    If $i = 1 Then Return SetError(1,0,0)
    Return _FileWriteToLine($sFilePath, $i, $sText)
EndFunc

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

It would take a while to write the whole thing. I looked at the help file and modified the combo example slightly. I'm guessing you need to read the control when it is clicked. I don't know if this is what you want.

#include <GUIConstantsEx.au3>

Opt('MustDeclareVars', 1)

Example()

Func Example()
    Local $msg
    GUICreate("My GUI combo")

    Local $h_Combo = GUICtrlCreateCombo("item1", 10, 10) ; create first item
    GUICtrlSetData(-1, "item2|item3", "item3") ; add other item snd set a new default

    GUISetState()

    ; Run the GUI until the dialog is closed
    While 1
        $msg = GUIGetMsg()
        Switch $msg
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $h_Combo ; If clicked read the data =>
                MsgBox(0, "", GUICtrlRead($h_Combo))
        EndSwitch
    WEnd
EndFunc   ;==>Example

Edit

I would use _FileReadToArray to get the data from the text file. See next example.

#include <GUIConstantsEx.au3>
#include <file.au3>

Opt('MustDeclareVars', 1)

Example()

Func Example()
    Local $msg
    GUICreate("My GUI combo")
    Local $a_Data, $h_File = "data.txt"
    _FileReadToArray($h_File, $a_Data)
    If @error Then ; You need to decide a course of action here.
        MsgBox(0, "The File contains no data", "The program will now explode.")
        Exit
    EndIf

    Local $h_Combo = GUICtrlCreateCombo($a_Data[1], 10, 10) ; create first item
    For $i = 2 To $a_Data[0]
        GUICtrlSetData(-1, $a_Data[$i]) ; Add the remaining lines of the text file.
    Next

    GUISetState()

    ; Run the GUI until the dialog is closed
    While 1
        $msg = GUIGetMsg()
        Switch $msg
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $h_Combo
                MsgBox(0, "", GUICtrlRead($h_Combo))
        EndSwitch
    WEnd
EndFunc   ;==>Example
Edited by czardas

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  
Followers 0