字体选择打开和保存文件的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
System.Windows.Forms; using
using System.IO;
namespace WordForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnFont_Click(object sender, EventArgs e)
{
FontDialog fd = new FontDialog();
fd.ShowColor = true;
fd.ShowApply = true;
fd.Apply += new EventHandler(fd_Apply);
fd.ShowHelp = true;
if (fd.ShowDialog() == DialogResult.OK)
{
tbEdit.Font = fd.Font;
tbEdit.ForeColor = fd.Color;
}
}
void fd_Apply(object sender, EventArgs e)
{
FontDialog fd = sender as FontDialog;
tbEdit.Font = fd.Font;
tbEdit.ForeColor = fd.Color;
}
private void btnColor_Click(object sender, EventArgs e)
{
ColorDialog cd = new ColorDialog();
cd.CustomColors = new int[3] { 1647214, 1425785, 1647258 };
if (cd.ShowDialog() == DialogResult.OK)
{
tbEdit.BackColor = cd.Color;
}
}
private void btnFolderBrower_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.SelectedPath = "c:\\";
fbd.ShowNewFolderButton = true;
fbd.ShowNewFolderButton = true;
fbd.Description = "请选择目录";
if (fbd.ShowDialog() == DialogResult.OK)
{
tbEdit.Text = fbd.SelectedPath;
}
}
private void btnOpenFile_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory = "c:\\";
ofd.Filter = "Text.Document(*.text)|*.text|All Files(*.*)|*.*";
ofd.FilterIndex = 1;
if(ofd.ShowDialog()==DialogResult.OK)
{
if (ofd.FileName != "")
{
ReadFile(ofd.FileName);
}
}
}
private void ReadFile(String fileName)
{
try
{
StreamReader sr = new StreamReader(fileName);
tbEdit.Text = sr.ReadToEnd();
sr.Close();
}
catch(IOException err)
{
MessageBox.Show(err.Message);
}
}
private void btnSaveFile_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
; sfd.AddExtension = true
sfd.DefaultExt = "txt";
sfd.Filter = "Text Document(*.txt)|*.txt|All Files(*.*)|*.*";
sfd.CheckFileExists = true;
sfd.CheckPathExists = true;
if (sfd.ShowDialog() == DialogResult.OK)
{
if (sfd.FileName != "")
{
WriteToFile(sfd.FileName);
}
}
}
private void WriteToFile(string fileName)
{
try
{
FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(tbEdit.Text);
sw.Close();
sw.Close();
}
catch(IOException err)
{
MessageBox.Show(err.Message );
}
}
}
}