基本信息
源码名称:C#非常好用的英汉词典源代码
源码大小:2.16M
文件格式:.rar
开发语言:C#
更新时间:2024-11-13
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 1 元 
   源码介绍

C#非常好用的英汉词典源代码

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 frmDictionary : Form
    {
        public frmDictionary()
        {
            InitializeComponent();
            this.Load = (s, e) => { InitializeTimer(); }; //窗体加载的时候 初始化计时器
        }
        //new一个泛型字典来装载 英汉词典.txt里的内容(键值对)
        Dictionary<string, string> dic = new Dictionary<string, string>();

        //----------------------------新增代码---------------------------//
        //new一个 泛型list  用来装载智能提示的单词
        List<string> lsSmart = new List<string>();
        //----------------------------新增代码---------------------------//

        //读取 绝对路径下的英汉词典.txt的每一行数据 采用的是系统默认的Encoding编码格式
        //string[] strArr = File.ReadAllLines(@"D:\study\博客园cnblogs\博客日志\C#日志\英汉词典\英汉词典\英汉词典.txt", Encoding.Default);

        //读取 英汉词典.txt的相对 路径
        string[] strArr = File.ReadAllLines(@"英汉词典.txt", Encoding.Default);

        //在窗体加载的时候自动运行
        private void frmDictionary_Load(object sender, EventArgs e)
        {
            //遍历每一行 
            //每行有2个数 英文单词 和 中文翻译
            for (int i = 0; i < strArr.Length; i )
            {
                //使用Split方法 移除空的单个字符
                string[] strArr1 = strArr[i].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                //如果 泛型字典的键key值里 不包含数组里strArr1第0个数
                //避免重复添加
                if (dic.Keys.Contains(strArr1[0]) == false)
                {
                    //则将数组里的键(英文单词) 和值(中文翻译) 添加到泛型字典里
                    dic.Add(strArr1[0], strArr1[1]);

                    //----------------------新增代码-------------------------------//
                    lsSmart.Add(strArr1[0]);    //将每行 数组里的第0个元素 即英文单词 添加到泛型list中
                    //----------------------新增代码-------------------------------//

                }
            }
            //----------------------新增代码-------------------------------//
            // C#封装了一个AutoCompleteStringCollection 类 用于完成某些 窗体控件 自动补全 功能的字符串集合
            AutoCompleteStringCollection strings = new AutoCompleteStringCollection();
            // 将获取所有英文单词的 list泛型转换成数组 添加到 strings里
            strings.AddRange(lsSmart.ToArray());
            txtInput.AutoCompleteCustomSource = strings;  //然后赋给文本框的 自动补全 所需的资源 属性
            txtInput.AutoCompleteSource = AutoCompleteSource.CustomSource;  //指定 CustomSource 为数据源
            txtInput.AutoCompleteMode = AutoCompleteMode.Suggest; //启动自动补全模式
            //----------------------新增代码-------------------------------//

        }

        //在文本框的TextChanged 事件下进行 键值查询
        private void txtInput_TextChanged(object sender, EventArgs e)
        {
            //如果泛型字典里的包含有key 与文本框输入相同的话
            //则在 下面的文本框里显示结果Value
            if (dic.Keys.Contains(txtInput.Text.Trim()))
            {
                //如果输入文本框的内容 存在于泛型字典的键key中
                //则显示该键相对应的值 并赋给文本框
                txtResult.Text = dic[txtInput.Text.Trim()];
                //由于此时搜索到了翻译结果 所以请求在线搜索变为false
                linkWebSearch.Visible = false;
                timer.Stop();
                TimeLength = 0;
            }
            //如果输入的数据为空
            else if (txtInput.Text.Trim() == "")
            {
                //提示 输入单词
                txtResult.Text = "请输入你要查询的单词";
                //此时没有进行查询  所以请求在线搜索变为false
                linkWebSearch.Visible = false;
                timer.Stop();
                TimeLength = 0;
            }
            else
            {   //否则显示正在搜索中
                txtResult.Text = "正在搜索...";
                timer.Start(); //开始计时
            }
        }

        //这里 调用了 LinkLable的 click事件
        private void linkWebSearch_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            //显示出 网络链接 http://www.iciba.com/ 后面加上 当前在没有搜索到的单词
            //点击后 直接在线搜索 http://www.iciba.com/单词 
            System.Diagnostics.Process.Start("explorer.exe", "http://www.iciba.com/" txtInput.Text.Trim());
        }


        //定义一个初始化正在搜索时间为0
        public int TimeLength = 0;

        public Timer timer;
        /// <summary>
        /// 初始化计时器
        /// </summary>
        public void InitializeTimer()
        {
            timer = new Timer();
            timer.Interval = 1000; //间隔为1秒
            timer.Tick = (s, e) => { ShowOnlineSearch(); };
        }

        /// <summary>
        /// 判断正在搜索的时间 如果超过5秒则启动在线搜索
        /// </summary>
        public void ShowOnlineSearch()
        {
            TimeLength ;
            //当时间到5秒的时候
            if (TimeLength >= 5)
            {
                //将txtResult文本框的 内容清空
                txtResult.Text = "";
                //在线翻译网站链接显示出来
                linkWebSearch.Visible = true;
                //显示LinkLabel的文本内容
                linkWebSearch.Text = "没找到? 试下在爱词霸在线翻译里查找:" "\r\n" txtInput.Text.Trim() "  :)";
                //计时器停止计时
                timer.Stop();
                TimeLength = 0;
                lblTimer.Text = TimeLength.ToString();
            }
            else
            {
                //否则 网站链接不显示
                linkWebSearch.Visible = false;
                lblTimer.Text = TimeLength.ToString();
            }
        }
    }
}