Sign in to follow this  
Followers 0
BananaFredSoft

Simple Text Editor

8 posts in this topic

#1 ·  Posted (edited)

This is an example text editor I made to help new people with learning AutoIt. Suggestions, criticism etc. appreciated.

;includes
#include <GUIConstants.au3>
#include <File.au3>

;declaring variables
$OpenedFilePath = "no file"
$OpenedFile = "no file"

$TextEditor = GUICreate("Simple Text Editor", 600, 600) ;Creates (Hidden) GUI which will be 600 by 600
$FileMenu = GUICtrlCreateMenu("File") ;Creates the File menu
$OpenFile = GUICtrlCreateMenuItem("Open...", $FileMenu) ;Creates Open File menu item
$SaveFile = GUICtrlCreateMenuItem("Save", $FileMenu) ;Creates Save File menu item
$PrintFile = GUICtrlCreateMenuItem("Print", $FileMenu) ;Creates Print File menu item
$EditControl = GUICtrlCreateEdit("", 0, 0, 600, 600) ;Creates Edit Control
GUISetState(@SW_SHOW) ;Shows the GUI

While 1 ;Initiates While loop
    $msg = GUIGetMsg() ;Checks if any GUI events have occured
    Switch $msg ;Initiates the Switch
        Case $GUI_EVENT_CLOSE ;Checks if $msg = $GUI_EVENT CLOSE: Checks if the user pressed the Close button
            If $OpenedFilePath <> "no file" Then ;Checks if $OpenedFilePath doesn't equal the string "no file"
                If GUICtrlRead($EditControl) <> FileRead($OpenedFile) Then ;Checks if the text in the Edit Control doesn't equal the text in the opened file
                    $SaveChangesBox = MsgBox(3, "Save File?", "Would you like to keep unsaved changes to the file you were editing?") ;Creates a dialog with Yes, No and Cancel buttons
                    If $SaveChangesBox = 6 Then ;Checks if the user pressed Yes in the Unsaved Changes box
                        FileDelete($OpenedFilePath) ;Deletes the Opened File
                        $OpenedFile = FileOpen($OpenedFilePath, 0 + 2) ;Opens and clears the file for reading/writing
                        FileWrite($OpenedFile, GUICtrlRead($EditControl)) ;Writes the text in the Edit Control to the opened text file
                        Exit ;Exits the script
                    ElseIf $SaveChangesBox = 7 Then;Checks if the user pressed No in the Unsaved Changes box
                        Exit ;Exits the script
                    EndIf ;Ends the If statement
                Else ;Executes the following code if the If/Elseif statement isn't true
                    Exit ;Exits the script
                EndIf ;Ends the If statement
            Else ;Executes the following code if the If/Elseif statement isn't true
                Exit ;Exits the script
            EndIf ;Ends the If statement
        Case $PrintFile ;Checks if $msg = $PrintFile: Checks if the user selected the Print File GUI Control
            $PrintFileFunction = _FilePrint($OpenedFilePath) ;Prints the text file $OpenedFilePath
            If $PrintFileFunction Then ;Executes the following code if $PrintFileFunction = 1
                MsgBox(0, "Print", "The file was printed.") ;Creates a message box with an OK button stating that $OpenedFilePath was printed
            Else ;Executes the following code if the If/Elseif function was not true
                MsgBox(0, "Print Error", "The file was not printed.") ;Creates an error message box with an OK button stating that $OpenedFilePath was printed
            EndIf ;Ends the If statement
        Case $OpenFile ;Checks if $msg = $OpenFile: Checks if the user selected the "Open File" GUI Control
            $OpenFileDialog = FileOpenDialog("Load Text File", "", "Text Files (*.txt)") ;Creates a File Open Dialog in which the title is "Load Text File", the Initial Directory is the Desktop, and the Filter is *.txt files
            If FileExists($OpenFileDialog) Then ;Executes the following code if the file path $OpenFileDialog exists
                $OpenedFilePath = $OpenFileDialog ;makes $OpenedFilePath = $OpenFileDialog
                $PreviousFileRead = FileRead($OpenedFilePath) ;Reads the text from $OpenedFilePath
                FileDelete($OpenedFilePath) ;Deletes the Opened File
                GUICtrlSetData($EditControl, $PreviousFileRead) ;Sets the Edit Control to have the same text as the previous read of the file
                $OpenedFile = FileOpen($OpenedFilePath, 0 + 2) ;Opens and clears the text file for reading/writing
                FileWrite($OpenedFile, $PreviousFileRead) ;Writes the value of $PreviousFileRead to the text file $OpenedFile
            EndIf ;Ends the If statement
        Case $SaveFile ;Checks if $msg = $OpenFile: Checks if the user selected the "Save File" GUI Control
            If $OpenedFilePath = "no file" Then ;Executes the following code if $OpenedFilePath = the string "no file"
                $NewDialog = FileSaveDialog("New File", "", "Text Files (*.txt)") ;Creates a File Save dialog in which the title is "New File", the initial directory is the Desktop, and the file filter is *.txt
                If $NewDialog <> "" Then ;Executes the following code if $NewDialog doesn't equal the string ""
                    $OpenedFilePath = $NewDialog & ".txt" ;makes $OpenedFilePath equal $NewDialog with ".txt" appended to the end
                    $OpenedFile = FileOpen($OpenedFilePath, 0 + 2) ;Opens and clears the text file for reading/writing
                    FileWrite($OpenedFile, GUICtrlRead($EditControl)) ;Writes the text in the Edit Control to $OpenedFile
                EndIf ;Ends the If statement
            Else ;Executes the following code if the If/Elseif statement isn't true
                FileDelete($OpenedFilePath) ;Deletes the Opened File
                $OpenedFile = FileOpen($OpenedFilePath, 0 + 2) ;Opens and clears the text files for reading/writing
                FileWrite($OpenedFilePath, GUICtrlRead($EditControl)) ;Writes the text in the Edit Control to $OpenedFilePath
            EndIf ;Ends the If statement
    EndSwitch ;Ends the Switch statement
WEnd ;Ends the While loop
Edited by BananaFredSoft

Share this post


Link to post
Share on other sites

Just as you said, VERY simple! Good job, I like the comments. :)

Share this post


Link to post
Share on other sites

I think you should put a print function in the file menu, _Fileprint

Share this post


Link to post
Share on other sites

Very Nice and simple.. Will certainly help some new folks I am sure...

Share this post


Link to post
Share on other sites

good job,

My Opinion

Make the texteditor resizable

if possible, add simple formatting toolbar like wordpad

Share this post


Link to post
Share on other sites

Looks awesome, going to try it out :) Thanks also for some coding examples for a newbie :)

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