Now we are going to FINALLY complete this text editor application.
Select the Project menu, then choose Add Form.
Click Open.
Set the form's BorderStyle to 1-Fixed Single.
Set the form's StartupPosition to 2-Center Screen.
Name the form frmTimeDate.
Set up the form below.
Name the top label lblFormat.
Name the left option button optFormat1 and the right option button optFormat2.
Set the Value property of optFormat1 to True to initially select that option button.
Name the label below the option buttons lblDisplay.
Name the right command button cmdOk and the left command button cmdCancel.
Set the Default property of cmdOk to True.
This form is going to allow us to insert two different formats of time and date on the text editor. To allow the editor to get data from this form, we need to use global variable using a code module.
Select the Project menu, then choose Add Module.
With the code module's code window open, type Public strTimeDate As String.
This declares the variable strTimeDate as a global string data type, which is going to be used to store the format of the date and time from the form frmTimeDate.
Open the code window for the command button cmdCancel, press tab, then type Unload frmTimeDate.
Select the optFormat1 object in the object box of the code window.
Press tab, then type the following lines:
lblDisplay.Left = 940
lblDisplay.Caption = "Display: " & Format(Time$, "h:mm:ss AMPM") & " " & Format(Date$, "m/d/yyyy")
strTimeDate = Format(Time$, "h:mm:ss AMPM") & " " & Format(Date$,
"m/d/yyyy")
Line 1: Set the left property of the label lblDisplay
to 940.
Line 2: Set the caption of the label lblDisplay to "Display:
" and format the current time (got from Time$) as h:mm:ss
(which looks like 0:00:00) in AMPM form (12-hour), then add a
space, then the current date, formatted as m/d/yyyy (which looks like 0/0/0000).
Line 3: Store the time and date format in the variable strTimeDate,
which was made global in the code module.
Select the optFormat2 object in the object box of the code window.
Press tab, then type the following lines:
lblDisplay.Left = 360
strTimeDate = Format(Time$, "h:mm:ss AMPM") & " " & Format(Date$, "dddd, mmmm d,
yyyy")
lblDisplay.Caption = "Display: " & strTimeDate
Line 1: Set the left property of the label lblDisplay
to 360.
Line 2: Set the variable strTimeDate to the current time (got from Time$)
formatted as h:mm:ss
(which looks like 0:00:00) in AMPM form (12-hour), then add a
space, then the current date, formatted as dddd, mmmm d, yyyy (which
looks like Wednesday, January 1, 2003).
Line 3: Set the caption of the label lblDisplay to "Display:
" and the text in the variable strTimeDate.
Select the Form object in the object box of the code window.
Press tab, then type the following lines:
frmEdit.Enabled = False
strTimeDate = Format(Time$, "h:mm:ss AMPM") & " " & Format(Date$, "m/d/yyyy")
lblDisplay.Caption = "Display: " & strTimeDate
Line 1: Disable the form frmEdit so that the user
may not access that form when the form frmTimeDate is loaded.
Line 2: Set the caption of the label lblDisplay to "Display:
" and format the current time (got from Time$) as h:mm:ss
(which looks like 0:00:00) in AMPM form (12-hour), then add a
space, then the current date, formatted as m/d/yyyy (which
looks like 1/1/02).
Line 3: Set the caption of the label lblDisplay to "Display:
" and the text in the variable strTimeDate.
Select the Unload event in the code window.
Press tab, then type frmEdit.Enabled = True.
Recall that when this form loads, it disables the form frmEdit, so it needs to be re-enabled in order to use it when this form unloads.
Select the cmdOk object in the object box.
Press tab, then type the following lines:
frmEdit.Enabled = True
If frmEdit.txtEdit.Visible = True Then
frmEdit.txtEdit.Text = frmEdit.txtEdit.Text & " " & strTimeDate
frmEdit.txtEdit.SelStart = Len(frmEdit.txtEdit.Text)
Else
frmEdit.txtEditWrap.Text = frmEdit.txtEditWrap.Text & " " & strTimeDate
frmEdit.txtEditWrap.SelStart =
Len(frmEdit.txtEditWrap.Text)
End If
Unload frmTimeDate
Line 1: Re-enable the form frmEdit.
Line 2: Test to see which text box is visible on the form frmEdit.
Line 3: When the text box txtEdit is visible, set the text of that
text box to itself, then a space, then the text in the variable strTimeDate.
Line 4: Set the SelStart of the text box txtEdit to the end
of it.
Line 6: Otherwise (the text box txtEditWrap is visible), set
the text of txtEditWrap to itself, then a space then the text in the
variable strTimeDate.
Line 7: Set the SelStart of the text box txtEditWrap to the
end of it.
Line 9: Unload the form frmTimeDate.
We have completed this form and now will finish the text editor. Be sure to save everything in a folder that you will remember. I would hate to have the computer crash on you and loose all this stuff. I recommend you save NOW!! Now let us continue.
Const conBtns As Integer = vbOKOnly + vbInformation
Dim intFoundPos As Integer
strSearchFor = InputBox("Find what?", "Find")
If txtEdit.Visible = True Then
intFoundPos = InStr(1, txtEdit.Text, strSearchFor)
Else
intFoundPos = InStr(1, txtEditWrap.Text, strSearchFor)
End If
If intFoundPos = 0 Then
MsgBox "The search string was not found.", conBtns,
"Find"
Else
If txtEdit.Visible = True Then
txtEdit.SelStart = intFoundPos - 1
txtEdit.SelLength = Len(strSearchFor)
Else
txtEditWrap.SelStart = intFoundPos - 1
txtEditWrap.SelLength = Len(strSearchFor)
End If
End If
Line 1: Declare constant conBtns as an Integer to store
the type of message box to be displayed.
Line 2: Declare variable intFoundPos as an Integer to store
the value of where the text is positioned in the InStr functions below.
Line 3: Display an input box to store the text entered into it in the
variable strSearchFor.
Line 4: Test to see if the text box txtEdit is visible.
Line 5: When the text box txtEdit is visible, set the variable intFoundPos
equal to position one (the first character) in the text box txtEdit, then
compare it with the first character of the variable strSearchFor.
Line 7: Same as Line 5, but with the text box txtEditWrap.
Lines 9 and 10: When nothing is found (InStr returns a
position in the variable intFoundPos, so when nothing is found, intFoundPos
equals 0), display the message The search string was not found.
Line 12: Determine which text box is visible
Line 13: When the text box txtEdit is visible, set the start of
the selection to one less than intFoundPos. If the start of the selection
was intFoundPos, the cursor actually starts at the character after the
first character in the search, so we need to go back one to select the entire
word.
Line 14: Set the length of the selection in txtEdit to the length
of strSearchFor (the text the user enters).
Line 15: When the text box txtEditWrap is visible, set the start
of txtEditWrap the same as we did for txtEdit.
The rest is self-explanatory.
Const conBtns As Integer = vbOKOnly + vbInformation
Dim intFoundPos As Integer, intBegSearch As Integer
If txtEdit.Visible = True Then
intBegSearch = txtEdit.SelStart + 1
Else: intBegSearch = txtEditWrap.SelStart + 1
End If
If txtEdit.Visible = True Then
intFoundPos = InStr(intBegSearch, txtEdit.Text, strSearchFor)
Else
intFoundPos = InStr(intBegSearch, txtEditWrap.Text, strSearchFor)
End If
If intFoundPos = 0 Then
MsgBox "The search has been completed.", conBtns, "Find
Next"
Else
If txtEdit.Visible = True Then
txtEdit.SelStart = intFoundPos
txtEdit.SelLength = Len(strSearchFor)
Else
txtEditWrap.SelStart = intFoundPos
txtEditWrap.SelLength = Len(strSearchFor)
End If
End If
Line 1: Declare constant conBtns as an Integer
to store the type of message box to be displayed.
Line 2: Declare variable intFoundPos as an Integer to store
the value of where the text is positioned in the InStr functions below.
Line 3: Test to see if the text box txtEdit is visible.
Line 4: Set the variable intBegSearch to the selection start (SelStart
-- Where the cursor is) plus one space ahead. This makes the cursor move
from the currently selected text, which is necessary so that it does not keep
finding the selected text.
Line 5: Do the same as line 4, but searching the text box txtEditWrap.
Line 6: End the If statement.
Line 7: Test to see if the text box txtEdit is visible.
Line 8: When the text box txtEdit is visible, set the variable intFoundPos
to whatever is in the string txtEdit, beginning at the position
set by the value of the intBegSearch variable, and compare the string in
the textbox with the string entered into the input box (which is stored in strSearchFor)
Line 10: Do the same as Line 8 with the txtEditWrap text
box.
Line 12: Test to see if the returned value of intFoundPos is 0.
Line 13: Display a message box telling the user that The search has
been completed.
Line 15: When intFoundPos in not 0 ...
Line 17: Set the selection start of the text box txtEdit to the
value of intFoundPos when it is visible
Line 18: Set the selection length of the text box txtEdit to the
length of the text entered into the input box (strSearchFor).
Line 20: Set the selection start of the text box txtEditWrap to
the value of intFoundPos when it is visible.
Line 21: Set the selection length of the text box txtEditWrap to
the length of the text entered into the input box (strSearchFor).
Well done! Test the application and PRAY that it works. Now that we have made quite a few applications, how about learning how to use them from Windows. This is simple. How about we compile this application?
Save each form and the code module for this project.
Now save the project.
Go to the File menu and select Make Editor.exe.
Specify your desired location, then click OK.
By doing this, Visual Basic translates the code that we entered into this application into machine code. Machine code is a simple form of code, consisting of 1's and 0's, also called binary code, because there are only two forms (one or zero).
Find the file in your computer and run it. It should work just fine. If you double-click the icon or press Enter with the icon selected while the application is already running, it will open another text editor, just like Notepad.
Well done for making it through this tutorial. Move on to Page 22 to learn some more complicated ways to manipulate strings.