Add the spelling and syntax check function to the. NET application.

Source: Internet
Author: User
Add the spelling and syntax check function to the. NET application.
Author: Meng xianhui from: [wonderful world of Meng xianhui] Release Date: 23:44:09

Adding the spelling and syntax check function to our. NET application can reduce spelling and syntax errors of input, and make our application look more professional. This article describes how to add these functions to the Visual Basic. Net windows form project. To implement this function in the. NET Framework, you only need to install Microsoft Word on the client, such as Microsoft Word 97,200 0, XP, and 2003. The following describes the implementation process.

1. Open Visual Studio. net, create a Windows application project of Visual Basic, add two buttons on the form form1, the names are: btnspellcheck and btngrammarcheck, the text properties are: "spelling check" and "syntax check", and then add a textbox. The name and text attributes are textbox1 and "Enter the text to be checked here. ".

2. To use Microsoft Word attributes and methods, you must reference the word component. Right-click "Reference" in the solution browser, add reference, select "com" in the pop-up dialog box, find "Microsoft Word 9.0 Object Library", and double-click it, click OK. Note: depending on the Microsoft Word version, the numbers in the Microsoft Word 9.0 Object Library are different, which is the version number of Word 2000.

3. Now you can write the code. First, you must add references to the Code:

Imports system. runtime. interopservices <br/>

The following code is used to check spelling and syntax. For the convenience of testing, the code is put together and commented out:

'During the spelling check and syntax check, the pass parameter is a variable that only performs the spelling check <br/> 'the syntax check performs the spelling check at the same time, the spelling check only performs the spelling check <br/> private sub spellorgrammarcheck (byval blnspellonly as Boolean) <br/> try <br/> 'create a word object and a temporary document <br/> dim objword as object <br/> dim objtempdoc as object </P> <p>' declare that idataobject stores the data returned from the clipboard <br/> dim idata as idataobject </P> <p> 'if no data needs to be checked, exit directly <br/> If textbox1.text. trim () = "" Then <br/> exit sub <br/> end if </P> <p> 'instantiate a word object <Br/> objword = new word. application <br/> objtempdoc = objword. documents. add <br/> objword. visible = false </P> <p> 'locate the word window to make it invisible <br/> objword. windowstate = 0 <br/> objword. top =-3000 </P> <p> 'Copy the text in the input box to the clipboard <br/> clipboard. setdataobject (textbox1.text) </P> <p> 'checks the spelling or syntax of the temporary document. <br/> with objtempdoc <br/>. content. paste () <br/>. activate () <br/> If blnspellonly then <br/>. checkspelling () <br/> Else <br/>. checkgrammar () <br/> end if </P> <p> 'After the spelling check or syntax check, <br/> 'use the clipboard to return the modified data to the text input box. <br/>. content. copy () <br/> idata = clipboard. getdataobject () <br/> If idata. getdatapresent (dataformats. text) Then <br/> textbox1.text = ctype (idata. getdata (dataformats. text), string) <br/> end if <br/>. saved = true <br/>. close () <br/> end with </P> <p> 'exit word <br/> objword. quit () <br/> messagebo X. Show ("spell check completed! "," Spell check ", messageboxbuttons. OK, _ <br/> messageboxicon. information) </P> <p> 'If Microsoft Word is not installed, you are prompted to install it, <br/> 'in actual applications, check whether Microsoft Word is installed on the client </P> <p> catch comexcep as comexception <br/> MessageBox. show ("Microsoft Word must be installed to check spelling or syntax. "," Spell check ", _ <br/> messageboxbuttons. OK, messageboxicon. warning) </P> <p> catch excep as exception <br/> MessageBox. show ("error:" + excep. message, "spelling check", _ <br/> messageboxbuttons. OK, messageboxicon. error) <br/> end try <br/> end sub </P> <p> events executed by the 'spelling check' button <br/> private sub btnspellcheck_click (byval sender as object, byval e as eventargs) _ <br/> handles btnspellcheck. click <br/> spellorgrammarcheck (true) <br/> end sub </P> <p> event executed by the 'syntax check' button <br/> private sub btngrammarcheck_click (byval sender as object, byval e as eventargs) _ <br/> handles btngrammarcheck. click <br/> spellorgrammarcheck (false) <br/> end sub <br/>

After adding the above Code in the Code Editor and executing it, you can see the following result of "syntax check:

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: info-contact@alibabacloud.com 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.