[英]Spell Checker using word
我正在用C#編寫代碼進行拼寫檢查。 我在網上找到了這段代碼。 我是C#的新手,無法理解代碼。
我在以下網站上找到了此代碼: http : //www.codeproject.com/Articles/4572/Using-Word-s-spellchecker-in-C
我可以讓我了解代碼中實際發生的一般准則:
using Word;
using System.Reflection;
private void button1_Click(object sender, System.EventArgs e)
{
fSpellCheck(textBox1 , label1 );
}
public void fSpellCheck(TextBox tBox, Label lLbl)
{
int iErrorCount = 0;
Word.Application app = new Word.Application();
if (tBox.Text.Length > 0)
{
app.Visible=false;
// Setting these variables is comparable
// to passing null to the function.
// This is necessary because the C# null
// cannot be passed by reference.
object template=Missing.Value;
object newTemplate=Missing.Value;
object documentType=Missing.Value;
object visible=true;
object optional = Missing.Value;
_Document doc = app.Documents.Add(ref template,
ref newTemplate, ref documentType, ref visible);
doc.Words.First.InsertBefore (tBox.Text );
Word.ProofreadingErrors we = doc.SpellingErrors;
iErrorCount = we.Count;
doc.CheckSpelling( ref optional, ref optional, ref optional,
ref optional, ref optional, ref optional, ref optional,
ref optional, ref optional, ref optional,
ref optional, ref optional);
if (iErrorCount == 0)
lLbl.Text = "Spelling OK. No errors corrected ";
else if (iErrorCount == 1)
lLbl.Text = "Spelling OK. 1 error corrected ";
else
lLbl.Text = "Spelling OK. " + iErrorCount +
" errors corrected ";
object first=0;
object last=doc.Characters.Count -1;
tBox.Text = doc.Range(ref first, ref last).Text;
}
else
lLbl.Text = "Textbox is empty";
object saveChanges = false;
object originalFormat = Missing.Value;
object routeDocument = Missing.Value;
app.Quit(ref saveChanges, ref originalFormat, ref routeDocument);
}
這基本上就是上面的代碼所做的。
1)它以隱藏模式打開一個新的Word實例
2)它將文本框中的文本插入文檔的第一部分
3)調用word文檔上的拼寫檢查器
4)從拼寫檢查器獲取錯誤計數並將錯誤數量打印到標簽上
5)它要求Word糾正文檔中的錯誤。
6)它將更正后的文本從Word文檔復制回您的文本框
7)它關閉文檔並退出隱藏的Word實例。
您在項目中需要做的事情:
1.創建一個標簽(如果您還沒有標簽)
2.創建一個文本框(如果您還沒有的話)
3.創建一個按鈕(如果您還沒有按鈕)
在您的按鈕上添加一個click事件,然后在該代碼中像在此代碼中一樣完成調用fSpellCheck ,並將標簽和文本框作為參數。
我在C#方面經驗不足,但是我想您有一個帶有GUI,按鈕,標簽和文本框的應用程序。
需要方法button1_Click
將操作分配給該按鈕,單擊該按鈕時,該操作是該按鈕的一種事件偵聽器。 當您單擊此按鈕時,將執行button1_Click
,並執行方法fSpellCheck(textBox1 , label1 );
被調用。
方法fSpellCheck(textBox1 , label1 );
實現用於檢查由文本框插入的單詞的算法(請注意fSpellCheck
的參數中的文本框引用)。 該方法檢查單詞中是否有錯誤,如果單詞正確或錯誤(有錯誤)或文本框為空,則該方法在標簽lLbl
打印單詞控件的結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.