C # Online English-Chinese dictionary applet _c# tutorial for Beginners

Source: Internet
Author: User
Tags trim try catch

Today wrote an English-Chinese dictionary applet, I added a lot of comments, suitable for beginners to refer to, where the bad writing please help point out, learning progress together.
Here is a generic, generic dictionary, manipulation of some controls, application of split, application of arrays, time intervals, use of 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 the final version of the English-Chinese dictionary {public partial class Form1:form {public Form1 () {InitializeComponent (); //The first step, I was first to the English-Chinese dictionary txt data source content storage, easy to use//first, use a generic dictionary to store English-Chinese dictionaries. TXT content//Introspection dictionary is (dictionary<,>) such, inside is the key value pairs//each row of data must have a unique key can not be repeated, the trailing data can be repeated//new a generic dictionary dictionary&lt
    ; string, string> dic = new dictionary<string, string> ();

    New A generic list list<string> list = new list<string> (); Read an English-Chinese dictionary. TXT file, it is necessary to know its path//My personal suggestion is to put the English-Chinese dictionary. txt file in a relative path, because after packaging easy to use//the absolute path to read the file//Plus @, easy to follow the symbolic conversion//encoding.default is the choice when
    Previous system default font encoding//string[] Strarr = File.ReadAllLines (@ "C:\Users\Administrator\Desktop\ English-Chinese dictionary. txt", encoding.default); Read the file on the relative path//I chose the relative path string[] Strarr = File.ReadAllLines (@ "English-Chinese dictionary. txt", encoding.default);

    Automatically run the private void Form1_Load (object sender, EventArgs e) {stime () when the form is loaded; Label2.

      Text = "The result of your query:"; Iterate through each row, each row is two elements, English and Chinese for (int i = 0; i < Strarr. Length; i++) {//Remove a single null character string[] strarr1 = Strarr[i using the Split method.
        Split (new char[] {'}, stringsplitoptions.removeemptyentries); Avoid repeating add//contains is included meaning if (!dic).
          Keys.contains (Strarr1[0]) {//In fact, this is OK, but as a rigorous programmer, I would like to add a judgment//to the array of English and Chinese into the generic dictionary Dic.
          ADD (Strarr1[0], strarr1[1]); Add English to the generic list the data in this list is the key value list within DIC.
        ADD (Strarr1[0]); }//In order to allow the program to run up the thought can be tall some, fill out this code autocompletestringcollection strings = new Autocompletestringcollectio
      N (); All list generics are added to strings in the English word conversion array strings. AddRange (list.
      ToArray ()); textBox1.AutoCompleteCustomSource = strings; Then assign the required resource attributes to the automatic completion of the text box Textbox1.autocompletesource = Autocompletesource.cusTomsource; Specifies CustomSource as the data source Textbox1.autocompletemode = Autocompletemode.suggest; Start automatic Full Mode}//above reading the English-Chinese dictionary. txt operation, has been done//then began to implement the private void textBox1_TextChanged (object sender, Eventar GS e) {//text box if there is no data, do not show Label1 if (TextBox1.Text = = "") {Label1.
      Text = ""; //Start lookup, display the data in the same text box as the generic Dictionary key//trim () is to remove the blank characters from the IF (DIC). Keys.contains (TextBox1.Text.Trim ()) {//Find data with key value, displayed in TextBox2 TextBox2.Text = Dic[textbox1.text.trim

        ()];
        Because search results, online search does not show linklabel1.visible = false; Label1.
        Text = ""; Timer.
        Stop ();
      Ltime = 0;
        else if (TextBox1.Text = "") {TextBox2.Text = "Please enter the word to query";
        Linklabel1.visible = false; Timer.
        Stop ();
      Ltime = 0;
        else {TextBox2.Text = "searching"; The timer starts the timer.
      Start (); }//The above Display section is also basically done//right, the online query to achieve private void linklAbel1_linkclicked (object sender, LinkLabelLinkClickedEventArgs e) {//Because I have 360 browsers that are often terminated, I added a try catch try
      {System.Diagnostics.Process.Start ("explorer.exe", "http://www.youdao.com/w/" + TextBox1.Text.Trim ());
      catch {MessageBox.Show ("the query was closed by other means");
    } private void Label2_click (object sender, EventArgs e) {} In order for the program to be tall, I set it in 20 seconds. Show Online Search if no results found
    You can also press the key to enter the query result directly//define a lookup time public int ltime = 0;

    Define a timer public timer timer;
      public void Stime () {timer = new timer (); One second interval timer.
      Interval = 1000; Timer.
          Tick + = (s, e) => {ltime++; Label1. Text = ltime.tostring ()///display query for a few seconds if (Ltime >=) {Label1.
            Text = "The collection time is greater than 20 seconds has timed out"; Label2.
            Text = "Sorry, the system does not contain the words you entered";
            TextBox2.Text = "";
            Show site link linklabel1.visible = true; Linklabel1.text = "Sorry please try to use (Youdao Youdao) Online Translator:" + "\r\n\n\t" + TextBox1.Text.Trim (); Timer.
            Stop ();

            Ltime = 0;
          Make the URL displayed by the Linkwebsearch control Linklabel1.bringtofront () above the TextBox control;
            else//that's the result in 20 seconds. {linklabel1.visible = false; Label1.
          Text = Ltime.tostring ();
    }
        }; ///<summary>///Click the event in the TextBox1 text box to return///</summary>///<param name= "Sender" ></p
    aram>///<param name= "E" ></param> private void Textbox1_keydown (object sender, KeyEventArgs e) {//Decide whether to click the return button if (E.keycode = = Keys.enter) {//I this is to copy down the above, directly detect the results if (DIC).
          Keys.contains (TextBox1.Text.Trim ())) {TextBox2.Text = Dic[textbox1.text.trim ()];
          Linklabel1.visible = false;
        Ltime = 0; else {Label1.
          Text = "The collection time is greater than 30 seconds has timed out"; Label2.
Text = "Sorry, the system does not contain the words you entered";          TextBox2.Text = "";

          Linklabel1.visible = true;

          Linklabel1.text = "Sorry please try to use (Youdao Youdao) Online Translator:" + "\r\n\n\t" + TextBox1.Text.Trim (); Timer.
          Stop ();
          Ltime = 0;
        Linklabel1.bringtofront ();

 }

      }
    }


  }
}








The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.