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

网友投稿 522 2023-11-12

今天写了一个英汉词典小程序,我加了好多注释,适合初学者一起参考,哪里写的不好请帮忙指出,一起学习进步。

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

这里用到了,泛型,泛型字典,一些控件的操作,split的应用,数组的应用,时间间隔,linkLabel的使用。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
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();
}
}
}
}
}

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

您可能感兴趣的文章:C#开发微信 二维码鼠标滑动 图像显示隐藏效果(推荐)C# 绘制统计图大全(柱状图, 折线图, 扇形图)C# ComboBox控件“设置 DataSource 属性后无法修改项集合”的完美解决方法C#实现组合排列的方法C#完成word文档打印的方法基于c#用Socket做一个局域网聊天工具C#实现较为实用的SQLhelperc#图片上传和显示的实现方法.NET的深复制方法(以C#语言为例)C#操作XML通用方法汇总C#实现win10 uwp 右击浮出窗在点击位置PC蓝牙通信C#代码实现C#登入实例

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:警务移动平台车-加强执法救援的利器
下一篇:开放银行理财生态 - 打破传统束缚,创造财务自由
相关文章

 发表评论

暂时没有评论,来抢沙发吧~