欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C#教程

當(dāng)前位置:主頁 > 軟件編程 > C#教程 >

適合初學(xué)者開發(fā)的C#在線英漢詞典小程序

來源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C#教程|點(diǎn)擊: 次

今天寫了一個(gè)英漢詞典小程序,我加了好多注釋,適合初學(xué)者一起參考,哪里寫的不好請幫忙指出,一起學(xué)習(xí)進(jìn)步。
這里用到了,泛型,泛型字典,一些控件的操作,split的應(yīng)用,數(shù)組的應(yīng)用,時(shí)間間隔,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數(shù)據(jù)源的內(nèi)容儲(chǔ)存起來,方便使用
    //首先用一個(gè)泛型字典存儲(chǔ)英漢詞典.TXT里的內(nèi)容
    //反省字典是(Dictionary<,>)這樣的,里面是鍵值對
    //每行數(shù)據(jù)必須要有一個(gè)唯一的鍵不可以重復(fù),尾隨的數(shù)據(jù)可以重復(fù)

    //new 一個(gè)泛型字典
    Dictionary<string, string> dic = new Dictionary<string, string>();
    //new 一個(gè)泛型list
    List<string> list = new List<string>();

    //讀取英漢詞典.TXT文件,這就要知道它的路徑了
    //我個(gè)人建議是把英漢詞典.txt文件放在相對路徑下,因?yàn)榇虬蠓奖闶褂?

    //絕對路徑下讀取文件
    //加上@,便于后面的符號轉(zhuǎn)換
    //Encoding.Default是選擇當(dāng)前系統(tǒng)默認(rèn)的字體編碼
    //string[] strarr = File.ReadAllLines(@"C:\Users\Administrator\Desktop\英漢詞典.txt",Encoding.Default);
    //相對路徑下讀取文件
    //我選擇的是相對路徑
    string[] strarr = File.ReadAllLines(@"英漢詞典.txt", Encoding.Default);

    //窗體加載時(shí)自動(dòng)運(yùn)行
    private void Form1_Load(object sender, EventArgs e)
    {
      Stime();
      label2.Text = "您查詢的結(jié)果:";

      //遍歷每一個(gè)行,每行都是兩個(gè)元素,英文和中文
      for (int i = 0; i < strarr.Length; i++)
      {
        //使用split方法移除單個(gè)空字符
        string[] strarr1 = strarr[i].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
        //避免重復(fù)添加
        //contains是包含的意思
        if (!dic.Keys.Contains(strarr1[0]))
        {
          //其實(shí)這樣也就可以了,但是作為一個(gè)嚴(yán)謹(jǐn)?shù)某绦騿T,我還是給這一段加個(gè)判斷
          //將數(shù)組里的英文和中文填到泛型字典里
          dic.Add(strarr1[0], strarr1[1]);
          //將英文添加到泛型list里
          //這樣list內(nèi)的數(shù)據(jù)都是dic內(nèi)的鍵值
          list.Add(strarr1[0]);
        }
      }
      //為了讓程序運(yùn)行起來想過能高大上一些,就填了這一下的代碼
      AutoCompleteStringCollection strings = new AutoCompleteStringCollection();
      // 所有l(wèi)ist泛型的英文單詞轉(zhuǎn)換成數(shù)組 添加到 strings里
      strings.AddRange(list.ToArray());
      textBox1.AutoCompleteCustomSource = strings; //然后賦給文本框的 自動(dòng)補(bǔ)全 所需的資源 屬性
      textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource; //指定 CustomSource 為數(shù)據(jù)源
      textBox1.AutoCompleteMode = AutoCompleteMode.Suggest; //啟動(dòng)自動(dòng)補(bǔ)全模式
    }
    //以上讀取英漢字典.txt的操作,已經(jīng)搞定
    //接下來就開始實(shí)現(xiàn)了


    private void textBox1_TextChanged(object sender, EventArgs e)
    {
      //文本框內(nèi)若是沒有數(shù)據(jù),就不顯示label1
      if (textBox1.Text == "")
      {
        label1.Text = "";
      }

      //開始查找,文本框內(nèi)與泛型字典鍵相同就把數(shù)據(jù)顯示出來
      //trim()是把空白的字符去掉
      if (dic.Keys.Contains(textBox1.Text.Trim()))
      {
        //用鍵值找到數(shù)據(jù),顯示在textBox2中
        textBox2.Text = dic[textBox1.Text.Trim()];

        //因?yàn)樗阉鞯搅私Y(jié)果,所以在線搜索不顯示
        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 = "正在搜索";
        //計(jì)時(shí)開始
        timer.Start();
      }

    }
    //以上顯示部分也基本搞定
    //對了,把在線查詢實(shí)現(xiàn)出來
    private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
      //因?yàn)槲疫@有360瀏覽器,經(jīng)常被終結(jié),我就添加了try catch
      try
      {
        System.Diagnostics.Process.Start("explorer.exe", "http://www.youdao.com/w/" + textBox1.Text.Trim());
      }
      catch
      {
        MessageBox.Show("通過其他方式已將查詢關(guān)閉");
      }
    }

    private void label2_Click(object sender, EventArgs e)
    {

    }

    //為了讓程序能高大上,我設(shè)置在20秒內(nèi)若是沒有查到結(jié)果就顯示在線查找
    //也可以按鍵盤回車鍵直接進(jìn)行查詢結(jié)果

    //定義個(gè)查找所用時(shí)間
    public int Ltime = 0;
    //定義個(gè)計(jì)時(shí)器
    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 = "收索時(shí)間大于20秒已超時(shí)";
            label2.Text = "對不起,系統(tǒng)不包含您輸入的單詞";
            textBox2.Text = "";
            //顯示網(wǎng)站鏈接
            linkLabel1.Visible = true;
            linkLabel1.Text = "對不起請嘗試使用(有道youdao)在線翻譯:" + "\r\n\n\t" + textBox1.Text.Trim();
            timer.Stop();
            Ltime = 0;

            //使linkWebSearch控件顯示的網(wǎng)址在textbox控件上面
            linkLabel1.BringToFront();
          }
          else//那就是20秒內(nèi)顯示出結(jié)果了
          {
            linkLabel1.Visible = false;
            label1.Text = Ltime.ToString();
          }
        };
    }

    /// <summary>
    /// 在textBox1文本框內(nèi)點(diǎn)擊回車的事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
      //判斷是否點(diǎn)擊了回車按鈕
      if (e.KeyCode == Keys.Enter)
      {
        //我這是把上面的復(fù)制下來了,直接查出結(jié)果
        if (dic.Keys.Contains(textBox1.Text.Trim()))
        {
          textBox2.Text = dic[textBox1.Text.Trim()];
          linkLabel1.Visible = false;
          Ltime = 0;
        }
        else
        {
          label1.Text = "收索時(shí)間大于30秒已超時(shí)";
          label2.Text = "對不起,系統(tǒng)不包含您輸入的單詞";
          textBox2.Text = "";

          linkLabel1.Visible = true;

          linkLabel1.Text = "對不起請嘗試使用(有道youdao)在線翻譯:" + "\r\n\n\t" + textBox1.Text.Trim();

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

      }
    }


  }
}








以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。

上一篇:C#檢查foreach判讀是否為null的方法

欄    目:C#教程

下一篇:C#窗體布局方式詳解

本文標(biāo)題:適合初學(xué)者開發(fā)的C#在線英漢詞典小程序

本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6239.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有