Program for notepad using VB

Source: Internet
Author: User
Content: This article discusses how to automatically add logs to the end of the document and determine them in the notes written in VB. Save Or save as, confirm whether the file needs to be saved, and so on.

Windows built-in Notepad That is, notepad has some attractive functions, such as automatically adding a computer clock at the end of a specific document. Time And the date to intercept the opened or saved file names. Title Bar], search, and so on, bring a lot of convenience to users. This article discusses how to implement the Textbox Control.

1. Add logs to documents

In Windows, if there is ". log" on the leftmost part of the first line of the document, every time you use NotePad to open this article, notepad automatically adds a time date at the end of the document.
". Log occupies four bytes and is placed at the top of the first line of the document. Therefore, you can use the left function of VB to read this string. If yes, the CSTR function is used to automatically add the time and date.
We can add the following to the program that opens the file: Code :

A = left (text1.text, 4) 'gets the first 4 bytes
If IFA = ". log" then' exists:
Text1.selstart = Len (text1.text) 'Move the cursor to the end of the file
Text1.seltext = vbcrlf & CSTR (now) 'adds the line feed and time
Else' exit the process if none exist

In this way, notepad compiled with VB has the ability to automatically add logs to documents like notepad of windows.

2. Extract the file name in Filename

If we use NotePad to open the file D:/vbfile/New/textboxcase example .txt, the caption of the title bar of notepad is changed to Textbox Control usage example-notepad. This is easier to do with VB.

To facilitate the use of various programs, we may wish to compile a function that intercepts strings:

'Intercept the pure text column name Function
Functiongetfiletitle (oldstrasstring) asstring

Dimnasinteger and masinteger declare string variables
Dimiasstring, rasstring
I = "/" 'specifies the character to be searched
Forn = 1 tolen (oldstr) 'Use the Len function to calculate the number of bytes of a known string
M = Limit Rev (oldstr, I,-1) '"/" location (where-1 is the default value)
Nextn 'find it!

'Intercept the string after the last '/"
R = right (oldstr, Len (oldstr)-m) 'get title
P = rev (R, ".",-1) '"." Location
Getfiletitle = left (R, P-1) 'remove the suffix


Now we call this function to obtain the document name:

Dimmystrasstring, resultasstring
Mystr = "D:/games/6do/oldtucom/heart/story001.txt"
Result = getfiletitle (mystr)
Me. Caption = Result & "-Notepad"

When executed, the title of the form is changed to story001-Notepad, which is similar to notepad!

Iii. Determine whether to save or save

Generally, we use the commondialog control to save files. However, the showsave provided by the VB public dialog box is actually only saveas, if no processing is performed, the Save As dialog box is displayed every time the file is saved in the running program. To avoid this inconvenience, we can declare a form level or Module Level object name variable [type: String], assign a value to this variable in each related operation and save it in Memory In, and then give recognition when saving the file. If the variable is not empty, save the file directly. Otherwise, the Save As dialog box is called up and the user enters the file name.

Dimsavefilenameasstring 'form or module-level variables

'Add in the file opening event:
Savefilename = commondialog1.filename

'Save the file
Ifsavefilename <> "" then
Opensavefilenameforinputas #1
Print #1, text1.text
Close #1
'Write the code for saving the file in the public dialog box.

In this way, our notepad becomes as smart as NotePad: the annoying Save As dialog box will not always pop up!

4. Drag a file from my computer and open it

When you drag a file from my computer to the notepad editing page, the file is automatically opened if the file format is correct. In VB, we can handle this as follows:

1. Add the following content to the form_load event:

Text1.oledropmode = 1' makes text1 an OLE container that accepts drag and drop of Files

2. Use OLE technology to implement drag-and-drop and open functions:

'Drag a file to the text box
Privatesubtext1_oledragover (dataasdataobject, effectaslong ,_
Buttonasinteger, shiftasinteger, xassingle, yassingle ,_
Ifdata. getformat (vbcffiles) then' if a file is used, the tag can be displayed.
Effect = vbdropeffectcopyandeffect
Else' otherwise, it cannot be marked
Effect = vbdropeffectnone

'When the file is put down
Privatesubtext1_oledragdrop (dataasdataobject, effectaslong ,_
Buttonasinteger, shiftasinteger, xassingle, yassingle)

Dimsfilename $ asstring 'put down the file name variable
'Check whether the Put object is a file name.
Ifdata. getformat (vbcffiles) = truetemedia' If yes, sfilename = data. Files (1) 'only reads the information of the first file
Onerrorresumenext: Ignore
'Open the file in 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

Note: Remember to write the error handling code. Otherwise ...... You know.

5. Check whether the file needs to be saved.

Exit the program from the current state or move to another state, such as creating a new file or opening a file. If the current file has changed and the user has not saved it, you should be reminded not to save the changes you have made. There are some notepad. What should I do in VB?
We can declare a Boolean variable to record whether the text box has changed, and use the change event of the Textbox Control to monitor the status of the text box. If there is a change, the variable value is changed.

1. module or form-level declaration:


2. In form_load:

Ask = false' Initial Value

3. Add:

Ask = true

4. When you exit the program or move to a new state:

Ifask = truettings
Dimflagasinteger, msgstrasstring
Msgstr = "the file has been changed. Do you want to save the disk? "'Prompt
Flag = msgbox (msgstr, vbyesnocancel, "")'
If ifflag = vbyesthensaveit is selected, save it: here we assume there is a sub-process saveit for saving the file.
Ifflag = vbcancelthenexitsub
Ifflag = vbnothencancel = false
'... Write other main event handling code here
Ask = false' indicates that the operation is ended to false, so as not to be prompted

6. search functions

Notepad provides the function of searching for and continuing to search for strings. In VB, the textbox control does not provide the find method as the RichTextBox Control does. In this way, we have to turn to some internal functions of VB. The following is a self-compiled function dedicated to searching strings in the Textbox Control. Internal functions such as instr of VB are used in the function. I will not explain it here.

'Query string functions [can be placed in the module or form level]
Functionfindmystr (mynameastextbox, searchstrasstring) asinteger
Dimwhere 'get the string variable to be searched
The myname. setfocus text box obtains the focus to display the found content.
Start = myname. selstart myname. sellength 1
Where = instr (START, myname. Text, searchstr) 'query strings in the text
'If found, set the starting position and highlight the found string
'Findstr = where-1
Myname. selstart = where-1
Myname. sellength = Len (searchstr)
'Otherwise, a prompt is displayed.
Else: msgbox "the string to be searched is not found. ", Vbinformation," prompt"

With this function, we can search for and continue to search for strings as follows:

1. Form-level declaration:

Dimsearchasstring 'declares the variable to be searched

2. First search:

'If the cursor is not at the beginning, it will bring it back to the beginning.
Iftext1.selstart <> 0thentext1. selstart = 0
Search = inputbox ("Enter the word to be searched :")
Sfind = findmystr (text1, search)

3. continue to search for [simple]:

Sfind = findmystr (text1, search)

In this way, the search function is basically available. For example, the upward search function is more satisfactory.

The above are some of my experiences in learning and exploration, which have been successfully debugged under pwin98 and VB enterprise 6.0. If anything is inappropriate, please give your experts an axe! ->


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.