• Log in or sign up for an account here: https://www.sythe.org/login

[TUT] Starting a Word Processor

Blupig

BEEF TOILET

$5 USD Donor
Joined
Nov 23, 2006
Sythe Gold
1,609
Messages
7,145
Age
33
Location
Canada
Valentine's Singing Competition Winner
Sythe Awards 2015 Winner
Sythe Awards 2016 Winner
Sythe Awards 2020 Winner
Member of the Month Winner
MushyMuncher
Gohan has AIDS
Extreme Homosex
World War 3
I'm LAAAAAAAME
Discord Verified
Off Topic Participant
[TUT] Starting a Word Processor
I'm still a little iffy on this myself, so if any l337 coders what to correct me, feel free.



[TUT] Getting started on a word processor

This'll be a long one, so before you get into this make sure you've got at least 45mins ahead of you to spend on this project. Let's get crackin.

Things you need:
-Standard EXE
-Textbox (x1), name = txtNotebook
-CommonDialog (x1), name = cmOpenFile
-Load and save buttons, can be a menu, command button, whatever you want

1. We'll begin with the declarations, of course. In this project, lucky for us, we'll only need 1, and that's declaring a boolean I like to call blnIsDirty. Name it whatever you want, but that's what I'm using. This boolean will help us determine if a file is saved or not, so if the user tries to close your application before saving their text file, then a message box will come up asking if they want to save. If you didn't know how before, this is how we declare blnIsDirty:
Code:
Private blnIsDirty As Boolean

2. Okay, now we're going to add some code to our cmOpenFile common dialog control. Add this into your Form Load():
Code:
    With cmOpenFile
        .CancelError = True
        .Filter = "Text File (*.txt)|*.txt|All Files (*.*)|*.*"
        .FilterIndex = 1
        .DialogTitle = "Select a File"
    End With

^^ That really wasn't a difficult code at all. It's actually quite easy to understand too. In case you didn't already know, the "with" just saves time. Intead of retyping cmOpenFile.Cancelerror then cmOpenFile.Filer (etc), we use the with to only need to type out half of what would be needed without it. In this code, when the form loads, a couple setting in cmOpenFile will be changed. The first one you don't need to worry about - It just saves trouble when using the application (basically to not give an error when the user cancels). The second setting is the "filter" setting. This setting determines the file types the user will be able to open and save. Here, we have a choice between a text file (.txt) or any file in which case the user may define the extension. The "filterindex" setting chooses to have indexes in the filter range.

3. Okay, now comes a harder part. This is the code to open a file. Put it into your open button:
Code:
    On Error GoTo eropen
        Dim intFile As Integer
        Dim intMessageResult As Integer
        If blnIsDirty = True Then
          intMessageResult = MsgBox("Would you like to save?", vbQuestion + vbYesNo, "File Saved")
        If intMessageResult = vbYes Then
        Call mnuFileSave_Click
        cmOpenFile.DialogTitle = "Select a file"
        End If
        End If
        cmOpenFile.ShowOpen
        intFile = FreeFile
        Open cmOpenFile.FileName For Input As intFile
        txtNotebook.Text = Input(LOF(intFile), intFile)
        frmNoteBook.Caption = "Notebook " + cmOpenFile.FileName
        Close #intFile
        Exit Sub
eropen:

The first line is an error handler; so if anything goes wrong it "skips" the code and reffers to eropen to determine the plan of action for the error. In this case, nothing'll happen. The error will not be noticed. You might as well use "On Error Resume Next". It works the same way, unless you actually want some form of error handling. If you do, add it after the "eropen" at the bottom.

Now, we have some sub declarations. Here we've got 2 integers being declared; intFile and intMessageResult. The intFile is the input to go in our txtNotebook later on. The intMessageResult is the result of what the user selected if they didn't save their work and the message pops up to ask if they want to. That's basically what happens afterwards; now the code states that if blnIsDirty is active, so True, then intMessageResult would equal to the result chosen in the message box that follows asking if the user wants to save. After that, there's another If Statement. This one states that if the message box result was yes, then to call on the save button (mine was a menu item therefore mnuFileSave). It calls on it's click event, because that's what the save was under Feel free to change that to your liking. This is when the user can save their stuff. Then, we change the windows explorer dialog title to "Select a File". Then the Ifs are closed. Now we call upon our dialog control's open dialog. intFile become a free file, meaning we can open it. Now, we open up cmOpenFile's file name (file selected in windows explorer), with the input as intFile (file being opened). Now the application tells the input where to go, so txtNotebook. This next line is optional, but is useful for forgetful users. This just puts the file path that's opened into the application's title for easy refference. intFile's now closed (so it doesn't put an infinite amount of files in the textbox), and the sub's done.

4. Alright, now the save button. This isn't as complicated as the open button, not to worry. This can also work as a "New file" button, just change it around to erase the txtNotebook (txtNotebook.Text = "" or txtNotebook.Text = vbNullString).
Code:
On Error GoTo ersave
    Dim intFile As Integer
    cmOpenFile.DialogTitle = "Save your file"
    cmOpenFile.ShowSave
    intFile = FreeFile
    Open cmOpenFile.FileName For Output As intFile
    Print #intFile, txtNotebook.Text
    frmNoteBook.Caption = "Notebook " + cmOpenFile.FileName
    Close #intFile
    blnIsDirty = False
Exit Sub
ersave:
I won't go through the error handling again, so reffer to step 3 if you want to look at it again. Here we once again do a sub declaration for intFile. Once again it was explained in step 3. Now, since open dialogs and save dialogs have different functions and buttons (etc), we have to ask cmOpenFile to open it's save dialog (ShowSave). intFile becomes a free file (explained aaaaaaagain in step 3, lol), then cmOpenFile's filename is remembered as output (intFile, the current file). Then we have a nice little function called "Print". It really does all the work for us - It prints intFile into a file for us. The next 2 lines were also explained in step 3. blnIsDirty now changes to false since the user just saved their file, so the application won't be bugging them to save their work again.

5. This following code is for a close or exit button:
Code:
Dim intMessageResult As Integer
    If blnIsDirty = True Then
    intMessageResult = MsgBox("Would you like to save?", vbQuestion + vbYesNo, "File Saved")
    If intMessageResult = vbYes Then
    Call mnuFileSave_Click 'Save the File
    End If
End If
    txtNotebook.Text = ""
    frmNoteBook.Caption = "NoteBook"
    blnIsDirty = False

Once again we have a sub declaration for intMessageResult (explained in step 3). Now we ask the application that if the user didn't save their stuff and they want to exit, to ask if they want to save the file (using a message box). If the answer is yes, then to call up the save sub in step 4. The if statement ends, and the parts after that are somethings just to clear up the memory a bit. It's kind of useless, but add it if you really want to. The lines after the if ends are for making txtNotebook clear, making the application's caption default again (Notebook), and to change blnIsDirty to false. Once again, useless since the user's closing the program.

6. This is the last part in this lengthy tutorial. You better be grateful for it >:3
Add this code into your txtNotebook_Change() procedure.
Code:
    blnIsDirty = True
    mnuFileSave = True

So, when stuff is typed into txtNotebook, blnIsDirty becomes true so it'll bug people to save their stuff before opening a new file, making a new one or closing the application.


***********************
This tutorial took me 30 mins to type out, so please rep/thank if availble.
~Blupig
 
[TUT] Starting a Word Processor
not bad, keep it up. also, don't ask for rep/thanks. you will get it if you deserve it.
 
Runescape Gamblingrunescape gambling
Back
Top