用C#编写一个记事本,在打开一个已经建立好的记事本出现乱码问题。
privatevoid打开OToolStripMenuItem_Click(objectsender,EventArgse){OpenFileDialogfileone=...
private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog fileone = new OpenFileDialog();
fileone.Filter = "文本文件(*.txt)|*.txt|RTF文件(*.rtf)|*.rtf|所有文件(*.*)|*.*";
fileone.FilterIndex = 1;
if (fileone.ShowDialog() == DialogResult.OK)
{
try
{
this.richTextBox1.LoadFile(fileone.FileName, RichTextBoxStreamType.PlainText);
}
catch (Exception)
{
MessageBox.Show("打开不成功", "提示");
}
}
}
private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
{
saveFileDialog1.Filter = "文本文档.TXT|*.txt|.rtf文档|*.rtf";
saveFileDialog1.ShowDialog();
MessageBox.Show(saveFileDialog1.FileName);
try
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.RichText);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void 另存ToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog mysave = new SaveFileDialog();
mysave.Filter = "txt格式文件|*.txt|C#类文件|*.cs|所有格式|*.*";
mysave.InitialDirectory = @"D:\dog\pig";
mysave.FilterIndex = 1;
if (mysave.ShowDialog() == DialogResult.OK)
{
richTextBox1.SaveFile(mysave.FileName, RichTextBoxStreamType.TextTextOleObjs);
}
}
以上是打开,保存和另存为的代码。 知道是编码问题,可不知道怎么修改,百度了。不懂。
求帮忙,详细点 展开
{
OpenFileDialog fileone = new OpenFileDialog();
fileone.Filter = "文本文件(*.txt)|*.txt|RTF文件(*.rtf)|*.rtf|所有文件(*.*)|*.*";
fileone.FilterIndex = 1;
if (fileone.ShowDialog() == DialogResult.OK)
{
try
{
this.richTextBox1.LoadFile(fileone.FileName, RichTextBoxStreamType.PlainText);
}
catch (Exception)
{
MessageBox.Show("打开不成功", "提示");
}
}
}
private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
{
saveFileDialog1.Filter = "文本文档.TXT|*.txt|.rtf文档|*.rtf";
saveFileDialog1.ShowDialog();
MessageBox.Show(saveFileDialog1.FileName);
try
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.RichText);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void 另存ToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog mysave = new SaveFileDialog();
mysave.Filter = "txt格式文件|*.txt|C#类文件|*.cs|所有格式|*.*";
mysave.InitialDirectory = @"D:\dog\pig";
mysave.FilterIndex = 1;
if (mysave.ShowDialog() == DialogResult.OK)
{
richTextBox1.SaveFile(mysave.FileName, RichTextBoxStreamType.TextTextOleObjs);
}
}
以上是打开,保存和另存为的代码。 知道是编码问题,可不知道怎么修改,百度了。不懂。
求帮忙,详细点 展开
2个回答
展开全部
你用RTF格式保存 用纯文本打开当然乱码了
Load代码改为
this.richTextBox1.LoadFile(fileone.FileName, RichTextBoxStreamType.RichText);
Load代码改为
this.richTextBox1.LoadFile(fileone.FileName, RichTextBoxStreamType.RichText);
追问
没有load代码。
不知道把这段代码放在哪里
追答
private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog fileone = new OpenFileDialog();
fileone.Filter = "文本文件(*.txt)|*.txt|RTF文件(*.rtf)|*.rtf|所有文件(*.*)|*.*";
fileone.FilterIndex = 1;
if (fileone.ShowDialog() == DialogResult.OK)
{
try
{
// this.richTextBox1.LoadFile(fileone.FileName, RichTextBoxStreamType.PlainText);
this.richTextBox1.LoadFile(fileone.FileName, RichTextBoxStreamType.RichText); }
catch (Exception)
{
MessageBox.Show("打开不成功", "提示");
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询