一、開啟檔案
Stream myStream = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "d:\\";
openFileDialog1.Filter = "ext files (*.txt)|*.txt|All files(*.*)|*>**";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
StreamReader st = new StreamReader(openFileDialog1.FileName, Encoding.GetEncoding("gb2312"));
string str = st.ReadLine();
while (str != null)
{
richTextBox1.AppendText(str);
richTextBox1.AppendText("\n");
str = st.ReadLine();
}
}
}catch(Exception ex){
MessageBox.Show("開啟檔案出錯:"+ex.Message);
}
二、儲存檔案
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.InitialDirectory = "d:\\";
saveFileDialog1.Filter = "ext files (*.txt)|*.txt|All files(*.*)|*>**";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
DialogResult dr = saveFileDialog1.ShowDialog();
if (dr == DialogResult.OK && saveFileDialog1.FileName.Length > 0)
{
richTextBox1.SaveFile(saveFileDialog1.FileName,RichTextBoxStreamType.PlainText);
MessageBox.Show("儲存檔案成功!","儲存檔案");
}
三、顏色對話方塊
ColorDialog colorDialog1 = new ColorDialog();
colorDialog1.AllowFullOpen = false;
colorDialog1.Color = richTextBox1.ForeColor;
colorDialog1.ShowHelp = true;
if(colorDialog1.ShowDialog()==DialogResult.OK)
{
richTextBox1.ForeColor = colorDialog1.Color;
}
四、字型對話方塊
FontDialog fontDialog = new FontDialog();
fontDialog.Font = richTextBox1.Font;
fontDialog.Color = richTextBox1.ForeColor;
if (fontDialog.ShowDialog() != DialogResult.Cancel)
{
richTextBox1.Font = fontDialog.Font;
richTextBox1.ForeColor = fontDialog.Color;
}