今天就跟大家聊聊有關(guān)C#中怎么實(shí)現(xiàn)單詞本功能,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
A版實(shí)現(xiàn)以下功能:
1.利用文件流讀取文本文件2.利用openfiledialog實(shí)現(xiàn)查找文件的功能3.存取文本文件路徑和顯示文本文件內(nèi)容的功能
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();//每點(diǎn)擊一次選擇文件按鈕自動清空文本框內(nèi)已經(jīng)打開的文件內(nèi)容 textBox2.Clear();//清空文件路徑的內(nèi)容 OpenFileDialog filename = new OpenFileDialog();//定義打開文件 filename.InitialDirectory = Application.StartupPath;//初始路徑 filename.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";//設(shè)置打開文件的類型 filename.FilterIndex = 2;//設(shè)置文件類型的顯示順序 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');//將文件內(nèi)容分割成一行一行存給數(shù)組 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();//此處點(diǎn)擊返回按鈕同樣自動清空文本框文件內(nèi)容 textBox2.Clear();//清除文件路徑內(nèi)容 } }}
看完上述內(nèi)容,你們對C#中怎么實(shí)現(xiàn)單詞本功能有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
當(dāng)前題目:C#中怎么實(shí)現(xiàn)單詞本功能-創(chuàng)新互聯(lián)
標(biāo)題URL:http://jinyejixie.com/article24/djceje.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、Google、App開發(fā)、ChatGPT、響應(yīng)式網(wǎng)站、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容