您的位置:首页 > 其它

richTextBox1.LoadFile("d:\\source.rtf", RichTextBoxStreamType.RichText);提示文件格式无效

2010-11-17 21:20 387 查看
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ComboBoxExam
{
public partial class FormComboBox : Form
{
private void EditEnable(object sender, EventArgs e)
{
//由于comBoxName可以由用户输入新姓名,判断时不能使用SelectedIndex属性
if (comboBoxName.Text != "" && comboBoxDepartment.SelectedIndex > -1)
{
richTextBox1.Enabled = true;
buttonOpenFile.Enabled = true;
buttonSaveFile.Enabled = true;
}
}
public FormComboBox()
{
InitializeComponent();
}

private void buttonAddName_Click(object sender, EventArgs e)
{
if (comboBoxName.Text != "")
{
bool newitem = true;
//判断当前comboBoxName中用户输入的姓名是否已经存在于下拉列表中
for (int i = 0; i < comboBoxName.Items.Count; i++)
{
string oneitem = Convert.ToString(comboBoxName.Items[i]);
if (oneitem == comboBoxName.Text)
{
newitem = false;
}
}

//如果用户输入的姓名不在下拉列表中,则添加
if (newitem)
{
comboBoxName.Items.Add(comboBoxName.Text);
}
}

}

private void buttonOpenFile_Click(object sender, EventArgs e)
{
richTextBox1.LoadFile("d:\\source.rtf", RichTextBoxStreamType.RichText);
}

private void buttonSaveFile_Click(object sender, EventArgs e)
{
//保存文件,并清除richTextBox1中的文本,给出提示信息
richTextBox1.SaveFile("d:\\result.rtf",RichTextBoxStreamType.RichText);
richTextBox1.Clear();
MessageBox.Show(" 文件保存完毕!");
richTextBox1.Enabled = false;
buttonOpenFile.Enabled = false;
buttonSaveFile.Enabled = false;
}
}
}

由于是直接在d盘新建一个source.doc,或者是source.txt,然后改后缀为.rtf,运行程序会提示文件格式不对,新建文件另存为source.rtf,运行正常,不存在上述文件格式无效的提示,发这个东西的目的在于让和自己一样的初学者少走弯路。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐