1、用 vb做文本编辑器(Using VB as text editor)It means making a notepadLook at the followingIts not complicated to make a notepad with VB. We can do it easily by the wizard. But this article is just going to talk about making Notepad manually, to show to VB Beginners: its so easy to learn VB!By reading, this p
2、aper according to the experiment, beginners will learn a lot of things, such as how to use the RichText control to open and save the file, how to make a menu, toolbar and status bar and how to write code.Chapter one let our Notepad run right awayEager for success is the common aspiration of beginner
3、s. Well, please do the following three steps, and our wishes can be realized immediately!Step one: draw the interface.Create a standard EXE project, change its Caption attribute to “super NOTEPAD“, and click the Icon attribute to find a suitable Icon icon for it. Click the menu “project“ - “componen
4、t“, in the pop-up “component“ dialog box, find Microsoft RichText Box 6 and public dialog box Microsoft Common Dialog 6 and select them, click the “OK“ button. When the left toolbar appears two controls we just added. RichText Box and Commn Dialog drawing on the form, the size and location of RichTe
5、xt Box can not be ignored, we will deal with it in the code, of course, it is necessary to put its ScrollBar property to 2-rtfVertical, so in the open and edit files can be used when the vertical scroll bar.Step two: edit the menu.To bring up the menu editor by Ctrl+E, lets do the following menus:Fi
6、le menu:File (layer 1) mnuFileNew (second layer) mnuNewOpen (second layer) mnuOpenSave (second layers) mnuSave- (second layers) mnuFileSep (dividing line)Exit (second layer) mnuExitTwo. Edit menu:Editor (layer 1) mnuEditCopy (second layers) mnuCopyShear (second layer) mnuCutPaste (second layers) mnu
7、Paste- (second layers) mnuEditSep (dividing line)Select (second) mnuSelecAllThree. Search menu:Search (layer 1) mnuSearchFind (second layers) mnuFindFind the next (second layer) mnuFindOnFour. Help menu:Help (layer 1) mnuHelpInstructions (second layers) mnuUsageAbout (second layers) mnuAbout(Note: s
8、hortcut keys for each menu item are set by themselves)OK, other menu items will be added as soon as needed. Enter now:Step three: write code.declare lookup variablesDim sFind As StringStatement file typeDim FileType, FiType As StringInitialization programPrivate Sub Form_Load ()Set the size of the p
9、rogram startupMe.Height = 6000Me.Width = 9000End SubSet the edit boxs position and sizePrivate Sub Form_Resize ()On Error Resume Next error handlingRichTextBox1.Top=20RichTextBox1.Left=20RichTextBox1.Height = ScaleHeight-40RichTextBox1.Width = ScaleWidth-40End Subnew filePrivate Sub mnuNew_Click ()R
10、ichTextBox1.Text = empty the text boxFileName = “unnamed“Me.Caption = FileNameEnd SubOpen filePrivate Sub mnuOpen_Click ()CommonDialog1.Filter = “text document (*.txt) |*.txt|RTF (*.rtf) |*.rtf| document all files (*. *) |*.*“CommonDialog1.ShowOpenRichTextBox1.Text = empty the text boxFileName = Com
11、monDialog1.FileNamerichtextbox1.loadfile文件名我的标题“超级记事本:“与文件名端子“保存文件私有子 mnusave_click()commondialog1。过滤器=“文本文档(*. txt)| *. txt | RTF 格式(RTF 文档*。*)| .rtf |所有文件(*。*)| * *。 ”commondialog1.showsave文件类型= commondialog1.filetitlefitype = 1例(右(文件类型,3) )文件名= commondialog1.filename选择案例 fitype案例“TXT”richtextbox1
12、.savefile文件名,rtftext“RTF”richtextbox1.savefile文件名,rtfrtf案件“*”richtextbox1.savefile文件名最后选择我的标题“超级记事本:“与文件名端子“退出私有子 mnuexit_click()终点端子“复制私有子 mnucopy_click()清除剪贴板。clipboard.settext richtextbox1.seltext端子“剪切私有子 mnucut_click()清除剪贴板。clipboard.settext richtextbox1.seltextrichtextbox1.seltext =“”端子“全选私有子 m
13、nuselectall_click()richtextbox1.selstart = 0richtextbox1.sellength = Len(控件的文本)端子“粘贴私有子 mnupaste_click()richtextbox1.seltext = clipboard.gettext端子“查找私有子 mnufind_click()后= InputBox(“请输入要查找的字、词:” , “查找内容”后)找到后控件。端子“继续查找私有子 mnufindon_click()richtextbox1.selstart = richtextbox1.selstart + richtextbox1.s
14、ellength + 1控件。找到后,Len(控件)端子“使用说明私有子 mnureadme_click()关于错误处理程序richtextbox1.loadfile“readme。txt” ,rtftext”请写好readme。txt 文件并存入程序所在文件夹中我的标题“超级记事本:”和“使用说明”退出子汉德勒:本使用说明文档可能已经被移除,请与作者联系。 ”, “错误信息”信息端子“关于私有子 mnuabout_click()本超级记事本 ver1.0版权所有(C)2001 土人” 、信息“关于”端子“设置弹出式菜单(即在编辑框中单击鼠标右键时弹出的动态菜单)私有子 richtextbox
15、1_mousedown(按钮为整数,转变为整数,X,Y为单,单)如果按钮= 2 然后mnuedit页,vbpopupmenuleftalign其他的退出子最后如果端子“防止在切换输入法时字体自变(感谢王必成先生提供此方案)私有子 richtextbox1_keyup(整数编码为整数,移)如果 KeyCode = vbkeyspace然后richtextbox1.selfontname = commondialog1.fontname最后如果端子至此,我们的记事本可以编译使用了。点击菜单”文件”-“生成XXX。exe” ,回到桌面运行我们的记事本看看,是不是颇有成就感?当然,This kind
16、of Notepad is rough, and we need to do some work, please look at the next chapter.The second chapter beautification program interfaceMultiple digital processing software has toolbars and status bars. Toolbar and status bar can not only beautify our program and make it more professional, but also bri
17、ng convenience to users. Now lets do these two things.Toolbar(1) making toolbarsClick the “project“ - “component“, and select Microsoft Windows Common Control 6 and determine. At this point, the controls we are using appear on the toolbar on the left.To do the toolbar, you first need a control calle
18、d ImageList to load the image. Add it in the program interface, and then right-click the controls, click “properties“ pop-up “property page dialog box“ image “, and then click Insert picture“ can be a one-time loading picture (if not satisfied, can also be added). The picture can be selected in the
19、C:Microsoft Visual StudioCommonGraphicsBitmapsTlBr_W95 (here if you install VB on the C below). Notice: when you insert the picture, give each picture a keyword so that you dont get confused when youre quoting the picture. If you insert a “new“ picture, we mark “New“ in the keyword column“.With the
20、picture, then add toolbar (ToolBar) to the program interface. After adding, the toolbar appears under the menu, right-click it, select “attribute“, and set the “general“ item in the pop-up “property page“ dialog box, mainly as follows two items:1. “image list“: select ImageList12. “style“: choose 1-
21、trbStandard or 2-trbFlat according to your preferenceContinue clicking on the “button“ button of the property page and inserting several buttons. Button has a variety of styles, please set according to the need. Here please note: every user operation of the button must be annotated keywords, loading
22、 pictures, such as the “new“ button in the “key“ item marked “new“, in the “image“ type “New“ (namely ImageList1 pictures, keywords) needed to fill in the appropriate the prompt in the tooltip text “.(two) write toolbar button codeToolbar button code editing is very simple, you can write in accordan
23、ce with the following format:Private Sub ToolBar1_ButtonClick (ByVal Button As MSComctlLib.Button)On Error Resume Next error handlingSelect Case Button.Key keyword selectionCase “new“MnuNew_Click is equal to the menu item new and is clickedCase “open“ is equal to the menu item “open“ and is clickedM
24、nuOpen_Click(continue writing code for other buttons)End SelectEnd SubAfter the completion of trial operation of our program, we found that after the toolbar, the program becomes more beautiful, but there is a problem: open a long document, edit box drop-down scroll arrow is missing. The reason is:
25、the toolbar takes up a lot of space. Solution: set the edit box to the size and location of the “RichTextBox1.Top = 20 and RichTextBox1.Height = Me.ScaleHeight - 40 to RichTextBox1.Top = 380, RichTextBox1.Height = Me.ScaleHeight - 400.Two. Status bar(1) make the status barThe English name of the sta
26、tus bar is called StatusBar. After adding it on the form, it will appear at the bottom of the form by default. Right click on it, call the property page dialog box, click the pane item, and insert some panes. You can set the styles of each pane to the “pane“:0-sbrText display text, need to write cod
27、e1-sbrCaps displays a write case without programming2-sbrNum displays the NumLock key switch status without programming3-sbrIns displays the Insert key state,No programming required4-sbrScr15-sbrTime displays time and doesnt change with system clock when not programming6-sbrDate displays dates witho
28、ut programmingNote: after adding the status bar, you need to change RichTextBox1.Height = Me.ScaleHeight - 400 in Form_Resze to RichTextBox1.Height = Me.ScaleHeight - 600.(two) the status bar determines whether to write code without using the “style“ attribute (as mentioned above). Here are some exa
29、mples, readers can seriously try to figure out, so as to achieve the effect of drawing inferences from others.Example 1: after the user selects the “new“, the first pane shows: “the current state: opening the file“ “. Please write the code below in the “open“ menu:StatusBar1.Panels (1).Text = “curre
30、nt status: opening file“ then write code for the Timer control:Private Sub Timer1_Timer ()If StatusBar1.Panels (3).Text CStr (Time) Then StatusBar1.Panels (3).Text = TimeEnd IfEnd SubExample three: when the text of the edit box changes, let the first pane show: “editing the document: file name.“.Pri
31、vate Sub RichTextBox1_Chang ()StatusBar1.Panels (1).Text = “editing document:“ & CommonDialog1.FileNameEnd SubHard for so long, we now have a decent model like Notepad. This Notepad uses RichText controls, and in theory it can open and edit any large document, and its really much easier to use than the notepad that comes with Windows. Of course, there are some other functions that need to be added and perfected, and thats how you do it.