|
|
| SelLength - the length (in bytes) of the text | |
| SelStart - the position of the cursor from top of page. | |
| SelText - the text that is selected (highlighted) |
SetText - paste text from clipboard Clipboard.SetText txtWord.SelText
GetText - copy text to clipboard txtWord.SelText = Clipboard.GetText
Clear
| clear Clipboard | |
| place the selected text from the text window into the Clipboard |
Clipboard.Clear
Clipboard.SetText txtWord.SelText
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!
txtWord.SelText = Clipboard.GetText
| move the cursor to top of page (SelStart = 0) | |
| make the length of the select text equal to the length of the text in the text box (txtWord.SelLength = Len(txtWord.Text) | |
| set the focus to the text box |
Assignment: Add Cut, Copy, Paste, and Select All to the Word processor
![]()
![]()
| 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) |
![]()
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 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 = TrueElse 'you cannot copy
mnuCopy.Enabled = False
mnuCut.Enabled = FalseEnd 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.
![]()