合適初學者開辟的C#在線英漢辭書小法式。本站提示廣大學習愛好者:(合適初學者開辟的C#在線英漢辭書小法式)文章只能為提供參考,不一定能成為您想要的結果。以下是合適初學者開辟的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是選擇以後體系默許的字體編碼
//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();
}
}
}
}
}








以上就是本文的全體內容,願望對年夜家的進修有所贊助,也願望年夜家多多支撐。