Intermediary transaction SEO diagnosis Taobao guest Cloud host technology Hall
Content: Discusses how to use VB to write in the diary to automatically add the log to the end of the document, to determine whether to save or saved as, to confirm the need to save files and other functions.
Windows comes with Notepad, Notepad, there are some features are attractive, such as automatically at the end of a specific document to add the computer clock time and date, the interception has been opened or saved the pure document name [see Notepad title bar] and search, to bring a lot of convenience to users. This article discusses how to implement these features in the TextBox control.
Add a log to a document
In Windows, if the first line of the document has the leftmost. LOG, Notepad automatically adds a time date to the end of the document each time you open this text in Notepad.
". LOG "Total four bytes, and placed in the first line of the document shelf, so you can use the left function of VB to read this string, if there is, the CStr function to automatically add time and date."
We can add the following code to the program that opens the file:
Dimaasstring
A=left (text1.text,4) ' Gets the first 4 bytes
Ifa= ". LOG "Then ' if:
Text1.selstart=len (text1.text) ' cursor to end of file
TEXT1.SELTEXT=VBCRLF&CSTR (now) ' Add line and time
Else ' if not, exit process
Exitsub
EndIf
In this way, our Notepad written in VB has the same function of automatically adding logs to the document as Windows Notepad.
Second, the interception filename in the pure document name
If we use Notepad to open the file D:\VBFILE\NEW\Textbox control usage example. The caption of the Txt,notepad title bar changes to: TextBox control Usage Example-Notepad. This, we use VB to complete or relatively easy.
To facilitate use throughout the program, we may want to write a function that intercepts the string:
' Intercept a plain-text column name function
Functiongetfiletitle (oldstrasstring) asstring
Onerrorresumenext
Dimnasinteger,masinteger ' Declare string variable
Dimiasstring,rasstring
Dimpasinteger
I= the specified character to find
Forn=1tolen (OLDSTR) ' computes the number of bytes of a known string with the Len function
The location of the M=instrrev (oldstr,i,-1) ' "\" (where 1 is the default)
Nextn ' Get down!
' Intercept the string after the last ' \ '
R=right (Oldstr,len (OLDSTR)-m) ' Get title
P=instrrev (R, ".",-1) '. ' Location
Getfiletitle=left (r,p-1) ' Remove suffix
Endfunction
Now let's call this function to get the plain document name:
Dimmystrasstring,resultasstring
Mystr= "D:\games\6do\oldtucom\heart\Story001.TXT"
Result=getfiletitle (MYSTR)
me.caption=result& "-Notepad"
Execution, the title of the form becomes: story001-Notepad, and the effect of Notepad is no different!
Third, to determine the preservation or save as
Usually, we save the file by using the CommonDialog control, however, the common dialog box provided by VB Showsave is actually just SaveAs, if you do not do it, every time you save the file in the running program will jump out of the Save As dialog box, in order to avoid this inconvenience, We can declare a form-level or module-level filename variable [type: String], and in each related operation, assign a value to this variable and keep it in memory, then recognize it when saving the file, or save the file directly if the variable is not empty, or Save as a dialog box to let the user enter the filename.
Try:
Dimsavefilenameasstring ' form or module-level variable
' In open File event join:
Savefilename=commondialog1.filename
' Save file
Ifsavefilename<> "" Then
Opensavefilenameforinputas#1
Print#1,text1.text
Close#1
Else
' Write the code to save the file in a public dialog box
EndIf
In this way, our Notepad becomes as smart as Notepad: The annoying Save As dialog box doesn't always bounce!
Four, drag files from my Computer and open
When you drag a file from my computer to the Notepad editing interface, the file is formatted correctly and opens automatically. In VB, we can handle this:
1. In the Form_Load incident, add:
Text1.oledropmode=1 ' makes Text1 an OLE container for acceptable file drag and drop
2. Implement drag-and-drop and turn on features using OLE technology:
' When the file is dragged to the text box
Privatesubtext1_oledragover (dataasdataobject,effectaslong,_
Buttonasinteger,shiftasinteger,xassingle,yassingle,_
Stateasinteger)
Ifdata.getformat (vbCFFiles) Then ' as file displays a drop icon
Effect=vbdropeffectcopyandeffect
Else ' Otherwise ' show not drop icon
Effect=vbdropeffectnone
EndIf
Endsub
' When the file is put down
Privatesubtext1_oledragdrop (dataasdataobject,effectaslong,_
Buttonasinteger,shiftasinteger,xassingle,yassingle)
Dimsfilename$asstring ' down filename variable
' Check down something is not the filename
Ifdata.getformat (vbCFFiles) =truethen ' as Sfilename=data. Files (1) ' Read only the first file information
Onerrorresumenext ' ERROR handling: ignoring
' Open a file in a textbox
Opensfilenameforinputas#1
Ifmnucomb (0). Checkedthentext1.text=text1.text&strconv (inputb$_
(LOF (1), 1), vbunicode): Mylen=len (Text1.Text)
Ifmnucomb (1). Checkedthentext1.text=strconv (inputb$ (LOF (1), 1), _
Vbunicode): Mylen=len (Text1.Text)
Close#1
EndIf
Endsub
Note: You should remember to write the wrong handling code, otherwise ... You know.
V. Confirm that the document needs to be saved
Quit a program from the current state or move to another state such as a new file, open a file, etc., and if the current file has changed and the user is not saved, should remind the user not to save the changes made. This is some Notepad, in VB how to do?
We can declare a Boolean variable that records whether a text box has changed, and use the Change event of the TextBox control to monitor the state of the text box, and change the value of the variable if there is a change.
1. module or form level declaration:
Dimaskasboolean
2. In Form_Load:
Ask=false ' initial value
3. By adding in Text1_change:
Ask=true
4. When exiting a program or moving to a new state:
Ifask=truethen
Dimflagasinteger,msgstrasstring
msgstr= "file changed. Prompt
Flag=msgbox (Msgstr,vbyesnocancel, "hint") ' give a hint
Ifflag=vbyesthensaveit ' Select OK to save: This assumes that you have a child procedure to save the file SaveIt
Ifflag=vbcancelthenexitsub
Ifflag=vbnothencancel=false
EndIf
'...... Here are the other major processing event codes
Ask=false ' Operation end makes it false, lest old hint
Vi. search function
Notepad provides the ability to find and continue to find strings. In VB, the control textbox does not provide a Find method like the RichTextBox control. So we have to turn to VB some of the internal functions. The following is the author of a specific text in the TextBox control to find the string function, the function used in the VB InStr and other internal functions, here is not much to explain.
' Find string function [can be placed in a module or form level]
Functionfindmystr (mynameastextbox,searchstrasstring) Asinteger
Dimwhere ' Get the string variable you want to find
Dimstartasinteger
Myname.setfocus ' text box gets focus to show what is found
Start=myname.selstart Myname.sellength 1
Where=instr (START,MYNAME.TEXT,SEARCHSTR) ' Find strings in text
Ifwherethen
' If found, set the selected starting position and highlight the found string
' Findstr=where-1
Myname.selstart=where-1
Myname.sellength=len (SEARCHSTR)
' Otherwise give a hint
Else:msgbox The string you are looking for is not found. ", vbinformation," hint "
EndIf
Endfunction
With this function, we can find and continue to find strings like this:
1. Declare at form level:
Dimsearchasstring ' declares the variable to find
2. First search:
Dimsfindasstring
' If the cursor is not at the beginning, bring it back to the beginning.
Iftext1.selstart<>0thentext1.selstart=0
Search=inputbox ("Please enter the words you want to find:")
Sfind=findmystr (Text1,search)
3. Continue to find [simple]:
Dimsfindasstring
Sfind=findmystr (Text1,search)
In this way, the search function is basically available, such as to provide upward lookup function, it is more satisfactory.
The above is the author in the study, the exploration of some experience, in PWin98, VB Chinese Enterprise version 6.0 debugging successfully. If there is any improper place, please all the master corrections!http://hi.baidu.com/huazhongxu http://www.hv30.com