Jump to content

Add string from txt file when choose it in Combobox


Recommended Posts

Hello.

I write a small script to help me at work. What i Want?
1. User choose Text from ComboBox
2. Load correct string from txt ()
3. Show messagebox with full string from txt

 

Here is my code.

1. Code to open txt file - working fine when 1,2,-1 is static
 

$file = FileOpen("txttest.txt", 0) ; open txt
$txt = FileReadLine($file, 1) ; read line 1
$txt1 = stringreplace($txt,'\n',@LF) ; replace \n to next line
MsgBox(0,'test',$txt1) ; msgbox for test output

And here is code to make GUI with combobox, Test, Testaaa and exit button.

Func Example()

    ; GUI
    Local $hGUI = GUICreate("Test App", 300, 200)

    ; COMBOBOX
    Local $idComboBox = GUICtrlCreateCombo("Choose", 10, 10, 185, 20)
    Local $idButton_Close = GUICtrlCreateButton("Close", 210, 170, 85, 25)


    ; Options
    GUICtrlSetData($idComboBox, "Test|Testaaa", "...")

    ; SHOW GUI
    GUISetState(@SW_SHOW, $hGUI)

    Local $sComboRead = ""

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE, $idButton_Close
                ExitLoop

            Case $idComboBox
                $sComboRead = GUICtrlRead($idComboBox)



                MsgBox($MB_SYSTEMMODAL, "", "Your answer is: " & $sComboRead, 0, $hGUI)
        EndSwitch
    WEnd
    GUIDelete($hGUI)
EndFunc


In txt i have strings:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \n aaaaaaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb \n bbbbbbbbbbbbbbbbbbbbbbb

 

What i want?
When user choose: Test or Testaaa, in messagebox i want to display correct string from txt file 
- When user choose Test i want display :
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \n aaaaaaaaaaaaaaaaaaaaaaaaaa

- When user choose Testaaa i want display:
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb \n bbbbbbbbbbbbbbbbbbbbbbb

In messagebox.

Ty for help :)

Edited by Lisuter
Link to comment
Share on other sites

You mean something like this:

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
Example()
Func Example()
    ; GUI
    Local $hGUI = GUICreate("Test App", 300, 200)
    ; COMBOBOX
    Local $idComboBox = GUICtrlCreateCombo("Choose", 10, 10, 185, 20)
    Local $idButton_Close = GUICtrlCreateButton("Close", 210, 170, 85, 25)
    ; Options
    GUICtrlSetData($idComboBox, "Test|Testaaa", "...")

    ; SHOW GUI
    GUISetState(@SW_SHOW, $hGUI)

    Local $sComboRead = ""

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE, $idButton_Close
                ExitLoop

            Case $idComboBox
                $sComboRead = GUICtrlRead($idComboBox)
                Switch $sComboRead
                    Case "Test"
                        MsgBox($MB_SYSTEMMODAL, "", "Your answer is: " & $sComboRead & @CRLF & StringReplace(FileReadLine(@ScriptDir & "\txttest.txt", 1), "\n", @CRLF))
                    Case "Testaaa"
                        MsgBox($MB_SYSTEMMODAL, "", "Your answer is: " & $sComboRead & @CRLF & StringReplace(FileReadLine(@ScriptDir & "\txttest.txt", 2), "\n", @CRLF))
                    Case Else
                        ContinueLoop
                EndSwitch
        EndSwitch
    WEnd
    GUIDelete($hGUI)
EndFunc

 

Link to comment
Share on other sites

Same with Subz but using "If...EndIf" function.

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <Array.au3>
#include <File.au3>

$file = FileOpen("txttest.txt", 0) ; open txt

Example()

Func Example()

    ; GUI
    Local $hGUI = GUICreate("Test App", 300, 200)

    ; COMBOBOX
    Local $idComboBox = GUICtrlCreateCombo("Choose", 10, 10, 185, 20)
    Local $idButton_Close = GUICtrlCreateButton("Close", 210, 170, 85, 25)


    ; Options
    GUICtrlSetData($idComboBox, "Test|Testaaa", "...")

    ; SHOW GUI
    GUISetState(@SW_SHOW, $hGUI)

    Local $sComboRead = ""

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE, $idButton_Close
                ExitLoop

            Case $idComboBox
               If GUICtrlRead($idComboBox) = "Test" Then
                $txt = FileReadLine($file, 1) ; read line 1
                $txt1 = stringreplace($txt,'\n',@LF) ; replace \n to next line
;~             MsgBox(0,'test',$txt1) ; msgbox for test output
                $sComboRead = $txt1
               ElseIf GUICtrlRead($idComboBox) = "Testaaa" Then
                $txt = FileReadLine($file, 2) ; read line 1
                $txt2 = stringreplace($txt,'\n',@LF) ; replace \n to next line
                $sComboRead = $txt2
               EndIf


                MsgBox($MB_SYSTEMMODAL, "", "Your answer is: " & $sComboRead, 0, $hGUI)
        EndSwitch
    WEnd
    GUIDelete($hGUI)
 EndFunc

 

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

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