VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • C#实现单词本功能

本文实例为大家分享了C#实现单词本功能的具体代码,供大家参考,具体内容如下

看到网上有类似的教程视频实现单词本,于是自己敲了一个实现单词本功能的小项目,在实现期间,发现可以有两种版本来实现单词本功能,此处选择A版来为大家分享经验,同时留作自己日后复习巩固,有疏漏错误的地方,还望大神热心指出,在下感激不尽.

A版实现以下功能:

1.利用文件流读取文本文件
2.利用openfiledialog实现查找文件的功能
3.存取文本文件路径和显示文本文件内容的功能

?
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
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 WindowsFormsApp6
{
 public partial class Form1 : Form
 {
  public Form1()
  {
   InitializeComponent();
  }
 
  private void button2_Click(object sender, EventArgs e)
  {
   textBox1.Clear();//每点击一次选择文件按钮自动清空文本框内已经打开的文件内容
   textBox2.Clear();//清空文件路径的内容
   OpenFileDialog filename = new OpenFileDialog();//定义打开文件
   filename.InitialDirectory = Application.StartupPath;//初始路径
   filename.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";//设置打开文件的类型
   filename.FilterIndex = 2;//设置文件类型的显示顺序
   
   if (filename.ShowDialog() == DialogResult.OK)
   {
    textBox2.Text = filename.FileName.ToString();//将路径显示在文本框
    StreamReader sr = new StreamReader(filename.FileName, Encoding.Default);
    //将文件通过stream流读取
    string content = sr.ReadToEnd();//把文件读完存给content
    string[] lines = content.Split('\n');//将文件内容分割成一行一行存给数组
    for (int i = 0; i < lines.Length; i++)
    {
 
     textBox1.Text += lines[i++]+"\n";
 
 
     //List<string> english = new List<string>();
     //List<string> chinese = new List<string>();
     //string[] words = lines[i].Trim().Split('\t');
     //if (words.Length < 2)
     //continue;
     //english.Add(words[0]);
     //chinese.Add(words[1]);
      
    
    sr.Close();
   }
  }
 
  private void button1_Click(object sender, EventArgs e)
  {
   textBox1.Clear();//此处点击返回按钮同样自动清空文本框文件内容
   textBox2.Clear();//清除文件路径内容
  }
 }
}

以上为我编写的文件的代码,被注释掉的代码部分用以实现分割音标出来再显示的功能,还未完善好,所以注释掉了,有感兴趣的可以完善完善共同探讨。

这是我的窗口

我的文本文件内容是这样

这是实现的效果

项目任存在可改进的空间,有兴趣的伙伴可以改进改进,将你的作品放在评论区共同讨论


相关教程