栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

适合初学者开发的C#在线英汉词典小程序

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

适合初学者开发的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 dic = new Dictionary();
    //new 一个泛型list
    List list = new List();

    //读取英汉词典.TXT文件,这就要知道它的路径了
    //我个人建议是把英汉词典.txt文件放在相对路径下,因为打包之后方便使用

    //绝对路径下读取文件
    //加上@,便于后面的符号转换
    //Encoding.Default是选择当前系统默认的字体编码
    //string[] strarr = File.ReadAllLines(@"C:UsersAdministratorDesktop英汉词典.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)在线翻译:" + "rnnt" + textBox1.Text.Trim();
     timer.Stop();
     Ltime = 0;

     //使linkWebSearch控件显示的网址在textbox控件上面
     linkLabel1.BringToFront();
   }
   else//那就是20秒内显示出结果了
   {
     linkLabel1.Visible = false;
     label1.Text = Ltime.ToString();
   }
 };
    }

    /// 
    /// 在textBox1文本框内点击回车的事件
    /// 
    /// 
    /// 
    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)在线翻译:" + "rnnt" + textBox1.Text.Trim();

   timer.Stop();
   Ltime = 0;
   linkLabel1.BringToFront();
 }

      }
    }


  }
}








以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/123986.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号