close
C# 視窗程式範例--文字編輯器
專案下載:(1) 只有介面,沒有功能的版本 (英文) — Editor1.zip
專案下載:(2) 只有介面,沒有功能的版本 (中文) — Editor2.zip
專案下載:(3) 完整版 — Editor3.zip
2011 年新版:TextEditor20111206.zip (沒有儲存檔案,只有另存新檔) TextEditor20111207.zip (有儲存檔案)
執行結果

程式碼 (2012 年版, 有儲存檔案):FormEditor.cs
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class FormEditor : Form
{
String filePath = null;
public FormEditor()
{
InitializeComponent();
}
public static String fileToText(String filePath)
{
StreamReader file = new StreamReader(filePath);
String text = file.ReadToEnd();
file.Close();
return text;
}
public static void textToFile(String filePath, String text)
{
StreamWriter file = new StreamWriter(filePath);
file.Write(text);
file.Close();
}
private void openFileToolStripMenuItem_Click(object sender, EventArgs e)
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
String text = fileToText(openFileDialog.FileName);
richTextBox.Text = text;
filePath = openFileDialog.FileName;
}
}
private void newFileToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox.Text = "";
filePath = null;
}
private void saveFileToolStripMenuItem_Click(object sender, EventArgs e)
{
if (filePath == null)
{
dialogSaveFile();
}
else
{
textToFile(filePath, richTextBox.Text);
}
}
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
dialogSaveFile();
}
public void dialogSaveFile()
{
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
textToFile(saveFileDialog.FileName, richTextBox.Text);
filePath = saveFileDialog.FileName;
}
}
}
}
文章來源:陳鍾誠 (2010年11月30日),(網頁標題) C# 程式設計 — 文字編輯器,(網站標題) 免費電子書:C# 程式設計,2010年11月30日,取自 http://cs0.wikidot.com/texteditor ,網頁修改第 18 版。
文章標籤
全站熱搜