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

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,运行正常,不存在上述文件格式无效的提示,发这个东西的目的在于让和自己一样的初学者少走弯路。

posted on 2010-11-17 21:20  傲视群雄  阅读(3079)  评论(0编辑  收藏  举报