適合初學者開發(fā)的C#在線英漢詞典小程序
更新時間:2016年10月08日 15:58:57 作者:畢畢love丹丹
這篇文章主要為大家詳細介紹了適合初學者開發(fā)的C#在線英漢詞典小程序,具有一定的參考價值,感興趣的小伙伴們可以參考一下
今天寫了一個英漢詞典小程序,我加了好多注釋,適合初學者一起參考,哪里寫的不好請幫忙指出,一起學習進步。
這里用到了,泛型,泛型字典,一些控件的操作,split的應用,數組的應用,時間間隔,linkLabel的使用。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; namespace 英漢詞典最終版 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //第一步,我是先把英漢詞典.txt數據源的內容儲存起來,方便使用 //首先用一個泛型字典存儲英漢詞典.TXT里的內容 //反省字典是(Dictionary<,>)這樣的,里面是鍵值對 //每行數據必須要有一個唯一的鍵不可以重復,尾隨的數據可以重復 //new 一個泛型字典 Dictionary<string, string> dic = new Dictionary<string, string>(); //new 一個泛型list List<string> list = new List<string>(); //讀取英漢詞典.TXT文件,這就要知道它的路徑了 //我個人建議是把英漢詞典.txt文件放在相對路徑下,因為打包之后方便使用 //絕對路徑下讀取文件 //加上@,便于后面的符號轉換 //Encoding.Default是選擇當前系統(tǒng)默認的字體編碼 //string[] strarr = File.ReadAllLines(@"C:\Users\Administrator\Desktop\英漢詞典.txt",Encoding.Default); //相對路徑下讀取文件 //我選擇的是相對路徑 string[] strarr = File.ReadAllLines(@"英漢詞典.txt", Encoding.Default); //窗體加載時自動運行 private void Form1_Load(object sender, EventArgs e) { Stime(); label2.Text = "您查詢的結果:"; //遍歷每一個行,每行都是兩個元素,英文和中文 for (int i = 0; i < strarr.Length; i++) { //使用split方法移除單個空字符 string[] strarr1 = strarr[i].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); //避免重復添加 //contains是包含的意思 if (!dic.Keys.Contains(strarr1[0])) { //其實這樣也就可以了,但是作為一個嚴謹的程序員,我還是給這一段加個判斷 //將數組里的英文和中文填到泛型字典里 dic.Add(strarr1[0], strarr1[1]); //將英文添加到泛型list里 //這樣list內的數據都是dic內的鍵值 list.Add(strarr1[0]); } } //為了讓程序運行起來想過能高大上一些,就填了這一下的代碼 AutoCompleteStringCollection strings = new AutoCompleteStringCollection(); // 所有l(wèi)ist泛型的英文單詞轉換成數組 添加到 strings里 strings.AddRange(list.ToArray()); textBox1.AutoCompleteCustomSource = strings; //然后賦給文本框的 自動補全 所需的資源 屬性 textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource; //指定 CustomSource 為數據源 textBox1.AutoCompleteMode = AutoCompleteMode.Suggest; //啟動自動補全模式 } //以上讀取英漢字典.txt的操作,已經搞定 //接下來就開始實現了 private void textBox1_TextChanged(object sender, EventArgs e) { //文本框內若是沒有數據,就不顯示label1 if (textBox1.Text == "") { label1.Text = ""; } //開始查找,文本框內與泛型字典鍵相同就把數據顯示出來 //trim()是把空白的字符去掉 if (dic.Keys.Contains(textBox1.Text.Trim())) { //用鍵值找到數據,顯示在textBox2中 textBox2.Text = dic[textBox1.Text.Trim()]; //因為搜索到了結果,所以在線搜索不顯示 linkLabel1.Visible = false; label1.Text = ""; timer.Stop(); Ltime = 0; } else if (textBox1.Text == "") { textBox2.Text = "請輸入要查詢單詞"; linkLabel1.Visible = false; timer.Stop(); Ltime = 0; } else { textBox2.Text = "正在搜索"; //計時開始 timer.Start(); } } //以上顯示部分也基本搞定 //對了,把在線查詢實現出來 private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { //因為我這有360瀏覽器,經常被終結,我就添加了try catch try { System.Diagnostics.Process.Start("explorer.exe", "http://www.youdao.com/w/" + textBox1.Text.Trim()); } catch { MessageBox.Show("通過其他方式已將查詢關閉"); } } private void label2_Click(object sender, EventArgs e) { } //為了讓程序能高大上,我設置在20秒內若是沒有查到結果就顯示在線查找 //也可以按鍵盤回車鍵直接進行查詢結果 //定義個查找所用時間 public int Ltime = 0; //定義個計時器 public Timer timer; public void Stime() { timer = new Timer(); //一秒間隔 timer.Interval = 1000; timer.Tick += (s, e) => { Ltime++; label1.Text = Ltime.ToString();//顯示查詢幾秒 if (Ltime >= 20) { label1.Text = "收索時間大于20秒已超時"; label2.Text = "對不起,系統(tǒng)不包含您輸入的單詞"; textBox2.Text = ""; //顯示網站鏈接 linkLabel1.Visible = true; linkLabel1.Text = "對不起請嘗試使用(有道youdao)在線翻譯:" + "\r\n\n\t" + textBox1.Text.Trim(); timer.Stop(); Ltime = 0; //使linkWebSearch控件顯示的網址在textbox控件上面 linkLabel1.BringToFront(); } else//那就是20秒內顯示出結果了 { linkLabel1.Visible = false; label1.Text = Ltime.ToString(); } }; } /// <summary> /// 在textBox1文本框內點擊回車的事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void textBox1_KeyDown(object sender, KeyEventArgs e) { //判斷是否點擊了回車按鈕 if (e.KeyCode == Keys.Enter) { //我這是把上面的復制下來了,直接查出結果 if (dic.Keys.Contains(textBox1.Text.Trim())) { textBox2.Text = dic[textBox1.Text.Trim()]; linkLabel1.Visible = false; Ltime = 0; } else { label1.Text = "收索時間大于30秒已超時"; label2.Text = "對不起,系統(tǒng)不包含您輸入的單詞"; textBox2.Text = ""; linkLabel1.Visible = true; linkLabel1.Text = "對不起請嘗試使用(有道youdao)在線翻譯:" + "\r\n\n\t" + textBox1.Text.Trim(); timer.Stop(); Ltime = 0; linkLabel1.BringToFront(); } } } } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Visual Studio 未能加載各種Package包的解決方案
打開Visual Studio 的時候,總提示未能加載相應的Package包,有時候還無法打開項目,各種錯誤提示,怎么解決呢?下面小編給大家?guī)砹薞isual Studio 未能加載各種Package包的解決方案,一起看看吧2016-10-10