英汉词典(可在线)

今天写了一个英汉词典小程序,我加了好多注释,适合初学者一起参考,哪里写的不好请帮忙指出,一起学习进步。。
这里用到了,泛型,泛型字典,一些控件的操作,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();
                }

            }
        }



    }
}

1036033-20161007193133176-555876613.png
1036033-20161007193213020-621226763.png
1036033-20161007193223785-932315662.png
1036033-20161007193240942-1685079794.png
1036033-20161007193258129-1672176075.png
1036033-20161007193317473-260628088.jpg
1036033-20161007193330645-1315690977.jpg
1036033-20161007193346676-2023423527.jpg

转载于:https://www.cnblogs.com/bb-love-dd/p/5936544.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值