Adding spelling and grammar checking features of the .NET application: Meng will come from: [Meng] will be the best of the world Date: 2003-8-28 23:44:09
Add spelling and syntax check functions in our .NET application, you can reduce your input spelling and syntax errors, or make our application look more professional. This article describes how to add these features in the Visual Basic.Net Windows Form project. It is very simple to implement this feature in the .NET framework, just need to install Microsoft Word, such as Microsoft Word 97, 2000, XP, and 2003. Let's take a look at the process of implementation. 1. Open Visual Studio.net, create a Visual Basic's Windows application engineering, add two Button on the form FORM1, names: btnspellcheck and btngrammarCheck, Text attributes are: "Spell Check" and "Syntax Check" , Add a TextBox, name, and Text attributes: TextBox1 and "Please enter the text you want to check here.". 2. To use the properties and methods of the Microsoft Word object, you need to reference the Word component. On the References in the Solution Browser, right-click, add a reference, select "COM" tab in the tab of the pop-up dialog, then find "Microsoft Word 9.0 Object Library" and double-click it, click "OK "Button. Note: The number in Microsoft Word 9.0 Object Library is different based on the Microsoft Word version, which is the version number of Word 2000. 3, you can write the code next. First, you must add a reference in the code:
Imports system.Runtime.InteropServices
Below is the code for spelling checks and grammar checking, for convenience test, put the code together, and annotated:
'Spell check and grammatical examination process, transfer parameters to spell only spell checked variable' syntax check simultaneously spell check, and spell check only spell check Private Sub SpellorGrammarcheck (Byval Blnspellonly as boolean) Try 'creates a Word object and Temporary Document DIM Objword As Object Dim Objtempdoc as Object 'Declaration IdataObject Store Data Nettata As IdataObject' Returned from Clipboard DIM Idata As IdataObject 'If there is no data to check, exit if TextBox1.Text.trim () = "" "" Instantiate the Word object objword = new word.application objtempdoc = objword.documents.add objword.visible = false 'Locating the Word window, making it incoming objword.windowState = 0 objword.top = -3000' Copy the input box of text to the clip CLIPBOARD.SETDATAOBJECT (TextBox1.text) 'Spelling or syntax for temporary documentation or syntax check with objtempdoc .content.paste () .actiVate () if blspellonly kil damcheckspelling () else .checkgrammar () end if' spell check or syntax After the user has modified, 'Use the clipboard to return the modified data to the text input box. Content.copy () iData = clipboard.getdataObject () if idata.text) TEXTBOX1.TEXT = Ctype (iData.Getdata (DataFormats.Text), String) end if .saved = true .close () end with 'exits word objword.quit () MessageBox.show ("Spell Check has been completed!", "Spell check", Messageboxbuttons.ok, _ MessageBoxi Con. information, if Microsoft Word is not installed, prompting users to install, 'In practical applications, you need to check if the client has installed Microsoft Word Catch comXcep as comexception messagebox.show ("must install Microsoft Word to spell or syntax check.
"" Spell check ", _ MessageBoxButtons.OK, MessageBoxIcon.Warning) Catch Excep As Exception MessageBox.Show (" Error: " Excep.Message," spell check ", _ MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub ' "spell check" button to execute the event Private Sub btnSpellCheck_Click (ByVal sender As Object, ByVal e As EventArgs) _ Handles btnSpellCheck.Click SpellOrGrammarCheck (True) End Sub' event "grammar check" button to execute the Private Sub btnGrammarCheck_Click (ByVal sender As Object, Byval E as Eventargs _ handles btngrammarcheck.click spellorgrammarcheck (false) End Sub After adding the above code, then execute, you can see the result of "Syntax Check" below: