Visual Basic Programming

Page 21

Now we are going to FINALLY complete this text editor application.

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.

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.

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.

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.

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.

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.

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.

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.

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?

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

Well done for making it through this tutorial. Move on to Page 22 to learn some more complicated ways to manipulate strings.

Top of Page

Page 1 | Page 2 | Page 3 | Page 4 | Page 5 | Page 6 | Page 7 | Page 8 | Page 9 | Page 10 | Page 11

Page 12 | Page 13 | Page 14 | Page 15 | Page 16 | Page 17 | Page 18 | Page 19 | Page 20

Page 21 | Page 22 | Page 23 | Page 24 | Page 25 | Page 26 | Page 27 | Page 28 | Page 29 | Page 30