温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C#中怎么实现单词本功能

发布时间:2021-08-07 14:47:25 来源:亿速云 阅读:149 作者:Leah 栏目:编程语言

今天就跟大家聊聊有关C#中怎么实现单词本功能,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

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

看完上述内容,你们对C#中怎么实现单词本功能有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI