国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

適合初學者開發的C#在線英漢詞典小程序

2020-01-24 00:58:41
字體:
來源:轉載
供稿:網友

今天寫了一個英漢詞典小程序,我加了好多注釋,適合初學者一起參考,哪里寫的不好請幫忙指出,一起學習進步。
這里用到了,泛型,泛型字典,一些控件的操作,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是選擇當前系統默認的字體編碼    //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();      // 所有list泛型的英文單詞轉換成數組 添加到 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 = "對不起,系統不包含您輸入的單詞";            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 = "對不起,系統不包含您輸入的單詞";          textBox2.Text = "";          linkLabel1.Visible = true;          linkLabel1.Text = "對不起請嘗試使用(有道youdao)在線翻譯:" + "/r/n/n/t" + textBox1.Text.Trim();          timer.Stop();          Ltime = 0;          linkLabel1.BringToFront();        }      }    }  }}








以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 肇东市| 白银市| 罗平县| 宜兰市| 阿瓦提县| 全南县| 建水县| 南京市| 巫溪县| 丰台区| 桓台县| 河池市| 青川县| 翁牛特旗| 河西区| 航空| 会宁县| 黔江区| 刚察县| 旬邑县| 丰县| 榆林市| 修武县| 抚宁县| 连城县| 青铜峡市| 清镇市| 常熟市| 淮北市| 广元市| 正阳县| 新安县| 蓝田县| 康乐县| 沐川县| 平顺县| 娄底市| 彩票| 武清区| 鄂伦春自治旗| 永川市|