11 04 MDI3EZMAVJMY6UBWLZYORVTAWKED7VYTDZMZ53A




Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!:Working With Text
function GetCookie (name) { var arg = name + "="; var alen = arg.length; var clen = document.cookie.length; var i = 0; while (i < clen) { var j = i + alen; if (document.cookie.substring(i, j) == arg) { var end = document.cookie.indexOf (";", j); if (end == -1) end = document.cookie.length; return unescape(document.cookie.substring(j, end)); } i = document.cookie.indexOf(" ", i) + 1; if (i == 0) break; } return null; } var m1=''; var gifstr=GetCookie("UsrType"); if((gifstr!=0 ) && (gifstr!=null)) { m2=gifstr; } document.write(m1+m2+m3);            Keyword Title Author ISBN Publisher Imprint Brief Full  Advanced      Search  Search Tips Please Select ----------- Components Content Mgt Certification Databases Enterprise Mgt Fun/Games Groupware Hardware IBM Redbooks Intranet Dev Middleware Multimedia Networks OS Prod Apps Programming Security UI Web Services Webmaster Y2K ----------- New Titles ----------- Free Archive To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 function isIE4() { return( navigator.appName.indexOf("Microsoft") != -1 && (navigator.appVersion.charAt(0)=='4') ); } function bookMarkit() { var url="http://www.itknowledge.com/PSUser/EWBookMarks.html?url="+window.location+"&isbn=0"; parent.location.href=url; //var win = window.open(url,"myitk"); //if(!isIE4()) // win.focus(); } Search this book:  














Previous
Table of Contents
Next




As for the Copy operation, we’ll use the Windows Clipboard, of course, which is represented in Visual Basic by the Clipboard object. This object is automatically available to all Visual Basic programs; explicitly adding it to the project is not necessary. The Clipboard object has several methods, two of which we will use:


•  Clipboard.SetText places a specified block of text on the Clipboard, replacing any text that might already be there.
•  Clipboard.GetText returns the text from the Clipboard.

With these tools, implementing the Copy command is easy. The code is placed in the Edit|Copy menu command’s Click event procedure. You can display this procedure either by selecting it from the lists at the top of the Code Editing window or by selecting the command from the menus when the form is displayed. Listing 11.4 shows the code for this procedure. It verifies only that text is selected in the Text Box, which it then copies to the Clipboard.
Listing 11.4 The Edit|Copy command event procedure.


Private Sub mnuEditCopy_Click()

‘ If any text is selected copy it to the Clipboard.

If Text1.SelLength = 0 Then
Beep
Exit Sub
End If

Clipboard.SetText Text1.SelText

End Sub


Implementing the Cut command is similar. In fact, the only difference is that the selected text is deleted from the Text Box after being copied to the Clipboard. This is accomplished by setting the Text Box’s SelText property to an empty string. The code for the Edit|Cut command’s Click event procedure is shown in Listing 11.5.
Listing 11.5 The Edit|Cut command event procedure.


Private Sub mnuEditCut_Click()

‘ If any text is selected, copy it to the Clipboard,
‘ then delete it.

If Text1.SelLength = 0 Then
Beep
Exit Sub
End If

Clipboard.SetText Text1.SelText
Text1.SelText = “”

End Sub


Implementing the Edit|Paste command is the simplest of all, requiring only a single line of code. Set the Text Box’s SelText property equal to the text returned by the Clipboard’s GetText method—that’s it. Note that if text is selected in the Text Box when the Paste command is executed, the selected text is replaced by the pasted text, which is the standard Windows way. If no text is selected, then the pasted text is inserted at the insertion point. The code for this event procedure is shown in Listing 11.6.
Listing 11.6 The Edit|Paste command event procedure.


Private Sub mnuEditPaste_Click()

‘ Paste Clipboard text.

Text1.SelText = Clipboard.GetText()

End Sub


You can run the Baby Editor now; you’ll be able to enter text and then cut and copy it to other locations (either within the editor’s own text or into another Windows application). True, we still have plenty to add, such as file support and font selection, but I think you’ll agree that we have a good deal of functionality with very little coding involved.

Enabling And Disabling Menu Items
The editor, as it stands, has one fairly minor problem. If you open the Edit menu, you’ll see that both the Cut and Copy commands are enabled, even when no text is selected in the editor. Clearly, these commands are applicable only when text is selected. True, code in both the Edit|Cut and Edit|Copy command event procedures prevents anything from happening should either command be issued with no text selected, but our Baby Editor will appear more professional if the commands are enabled only when appropriate. Likewise, the Paste command should be enabled only when the Clipboard contains text to be pasted. How can we accomplish this?

The enabling/disabling of the menu commands is easy, requiring only that we set the command’s Enabled property to True or False. Determining whether to enable or disable these commands is equally as easy. We can look at the value of the SelLength property to determine if text is selected in the Text Box, and we can use the Clipboard object’s GetText method to see if the Clipboard holds any text.
Where will we carry out these actions? In other words, what event can we use? Obviously, we don’t care whether these menu items are enabled or disabled when the menu is not displayed. What always happens before the menu is displayed? You click on the top-level menu item (in this case, Edit). Therefore, the Click event procedure for the top-level menu item is the right place for this code. Listing 11.7 shows the code for the mnuEdit_Click event procedure.
Listing 11.7 The mnuEdit_Click event procedure.


Private Sub mnuEdit_Click()

‘ Enable Paste command only if there is text in
‘ the Clipboard.

Dim x As String

x = Clipboard.GetText()

If x = “” Then
mnuEditPaste.Enabled = False
Else
mnuEditPaste.Enabled = True
End If

‘ Enable Cut and Copy commands only if
‘ there is text selected.

If Text1.SelLength <> 0 Then
mnuEditCopy.Enabled = True
mnuEditCut.Enabled = True
Else
mnuEditCopy.Enabled = False
mnuEditCut.Enabled = False
End If

End Sub


When you run the project after adding this event procedure, you’ll see that the Copy, Cut, and Paste commands are enabled only when appropriate. Remember that the Paste command will be enabled if there’s anything in the Clipboard, whether or not it was placed there by this program.
Preventing Data Loss
One critical aspect of program design is preventing data loss. This means that the user should not be able to inadvertently exit the editor program, load another file, or start a new file if the current file contains changes that have not been saved to disk. The program should prompt the user to save the current file. Of course, the user can choose to exit without saving; the point here is to prevent accidents.

We’ll implement data loss insurance by maintaining a flag named TextChanged that is True if the contents of the editor’s Text Box have changed since they were loaded or last saved. The obvious place to set this flag is in the Text Box control’s Change event procedure, which is executed whenever the contents of the Text Box change. The flag will then be cleared when the file is saved. Whenever the user tries to load a file, start a new file, or quit when this flag is True, a warning message will be displayed (we’ll see how this is done later).
There’s a fly in this ointment, however. When a file is read from disk and loaded into the Text Box, the Change event procedure will be triggered and the TextChanged flag set, even though the text has not actually been changed. This will trigger false warnings if the user loads a file and then tries to exit without changing the file. The solution is to keep a second flag named JustLoaded that is True only when a file has just been loaded, but has not yet been modified. Then the Text1_Change event procedure is modified to set the TextChanged flag only if the JustLoaded flag is False. The code for this procedure, shown in Listing 11.8, executes whenever the contents of the Text Box change.
Listing 11.8 The Text1_Change event procedure.


Private Sub Text1_Change()

‘ If the Text Box’s contents change, set global
‘ variable TextChanged to True unless we have
‘ just loaded a file.

If JustLoaded = True Then
JustLoaded = False
Else
TextChanged = True
End If

End Sub






Previous
Table of Contents
Next






Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.



Wyszukiwarka

Podobne podstrony:
2014 11 04 Wyt SzSz 11DKPanc Szkolenie w 2015r
Niezwykła broń zbrodniarza wraca do Iraku (11 04 2009)
11 04
11 04 07 as Nuerburgring Karte englisch
2010 11 04 WIL Wyklad 04id 174
ustawa o kształtowaniu ustroju rolnego 11,04,2003
Ustny egzamin na pilota wycieczek 11 04 2006
11 04 Montazowy sprzet pomocniczy haki zawiesia trawersy stezenia montazowe
6 Międzynarodowy transfer wykład 11 04 2012
2014 11 04 Dec nr 434 MON Narodowe Centrum Kryptologii odznaka pamiątkowa
U (11 04 08) PRAWO ATOMOWE o zm ustawy [63p583]
VR@0 700DC Obsługa(205819 11 04) PL
Doctor Who Easter Special Planet of The Dead 11 04 09
Geodezja wykład 6 instrumenty geodezyjne (11 04 2011)
Personalfragebogen 11 04 2005
4 Sieci komputerowe 04 11 05 2013 [tryb zgodności]

więcej podobnych podstron