当前位置: 移动技术网 > IT编程>开发语言>c# > C#实现单词本功能

C#实现单词本功能

2019年09月08日  | 移动技术网IT编程  | 我要评论

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

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

a版实现以下功能:

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

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();//清除文件路径内容
  }
 }
}

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

这是我的窗口

我的文本文件内容是这样

这是实现的效果

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

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网