In this section, we will consider the Edit menu's Cut, Copy, and Paste operations. Think about exactly what these function do.

Copy means to make a copy of the selected text (text is selected by either highlighting it with the mouse or with the cursor keys with the Shift key). The copy of the text is placed in a special object called the Clipboard. Thus to copy, we want to:

  1. clear the Clipboard (remove whatever was there)
  2. copy the text into the Clipboard

To accomplish this, we need to know some useful properties of text boxes and methods of the Clipboard.

Properties of Text Box

bullet SelLength - the length (in bytes) of the text
bullet SelStart - the position of the cursor from top of page.
bullet SelText - the text that is selected (highlighted)

Methods of the Clipboard object

bullet SetText - paste text from clipboard 

Clipboard.SetText txtWord.SelText

bullet GetText - copy text to clipboard 

txtWord.SelText = Clipboard.GetText

bullet Clear

Copying text

bullet clear Clipboard
bullet place the selected text from the text window into the Clipboard

Clipboard.Clear
Clipboard.SetText txtWord.SelText

Cutting text 

To cut text is similar, except that the selected is deleted from the text box after it has been copied. To do this, replace the selected text with "nothing."

Clipboard.Clear
Clipboard.SetText txtWord.SelText
txtWord.SelText = ""     <-- "nothing" is between the quotes!

Pasting text

To paste text, we retrieve it from the Clipboard and make it the "selected" text.

txtWord.SelText = Clipboard.GetText

Select All

bullet move the cursor to top of page (SelStart = 0)
bullet make the length of the select text equal to the length of the text in the text box (txtWord.SelLength = Len(txtWord.Text)
bullet set the focus to the text box 

Assignment: Add Cut, Copy, Paste, and Select All to the Word processor

The Word Processor Search Menu

String functions

  Format Returns Example
len len(<string>) The lengh of the string (integer) len("hello")

len(txtEdit.Text)

asc asc(<string>) ASCII code of the character. asc("A")
char char(<integer>) Character it represents in ASCII Char(65)

char(13)

val val(<string>) The numeric value of the string. val("A")
str str(<integer>) The string value of the number str(65)
left left(<string>,<integer>) The leftmost number of the indicated string left("Washington",4)
right right(<string>,<integer>) The rightmost number of the indicated string right("Washington",3)
mid mid(<string>,<integer1>,<integer2>) interger 1 = starting location

integer 2 = number of characters

mid("Washington",3,4)

The instr function

instr(<string to be searched>, <string being looked for>,[<search type] )

Search type is either

0 = binary [byte by byte]
1 = textual [not case sensitive] 

Write a program that asks the user three questions (make 'em up!). The user types an answer in a text box. The program then prints appropriate feedback in a label. Thus,

Question the answer produces Feedback

Who is the President of the U.S.?

Bill Clinton Right
Clinton Right
William Jefferson Clinton Right
I think its' Clinton Right
clinton Right ( ! )

The instr function can do this easily. If any part of the users answer matches the target, a number greater that 0 is returned. Thus

If Instr(txtAnwer.Text,"Clinton",1) > 0 Then

MsgBox "Right"

Else

MsgBox "Wrong"

End If

You can use this to implement the Find function of the Word Processor. 

SearchString = InputBox("String to Search For", 1)
If Instr(txtWord.Text, SearchString) > 0 Then

txtWord.SelStart = Instr(txtWord.Text, SearchString - 1
txtWord.SelLength = Len(SearchString)

Else

MsgBox "String not found"

End If

The Edit Menu

The Cut, Copy and Paste features are all found on the Edit menu. We will use this fact to control when the Cut, Copy and Paste features are available. When can you copy or cut text? (Or, even better, when can't you?) It should be obvious that you cannot cut OR copy unless you have first selected some text. Therefore, when the Edit menu is selected, we will tell it to check to make sure that some text has been selected. How can we do this? Recall the SelLength property. Obviously, if this property is > 0, some text has been selected. The availability of a menu item is controlled by the Enabled property.

Sub mnuEdit_Click()

If (txtWord.SelLength > 0) Then 'it is OK to cut and copy

mnuCopy.Enabled = True
mnuCut.Enabled = True

Else 'you cannot copy

mnuCopy.Enabled = False
mnuCut.Enabled = False

End If

End Sub

How about Paste? When can you not paste text? Well, you cannot paste text if the Clipboard doesn't have anything on it. Thus we can add the following code to mnuEdit.

If (Len(Clipboard.GetText) > 0) Then 'you can paste

mnuPaste.Enabled = True

Else ' you cannot paste

mnuPaste.Enabled = False

End If

Add the appropriate code to Edit, Cut, Copy, Paste, Select All and Find to your Word Processor.