一个简单的打开文档对话框

来源:互联网 发布:小霸王网络机顶盒 编辑:程序博客网 时间:2024/05/19 05:32
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace WindowsFormsApplication3
{
    public partial class Form1 : Form//在form类中实例一个form1
    {
        public Form1()
        {
            InitializeComponent();//初始化
        }


        private void button1_Click(object sender, EventArgs e)//对click事件进行描述
        {


            openFileDialog1.Filter = "*.txt|*.txt";//选择一个文本,多个文本是filenames;
            DialogResult dr = openFileDialog1.ShowDialog();//显示打开文件对话框
            if (dr == DialogResult.OK)//如果你按的按钮是ok
            {
                richTextBox1.Text = System.IO.File.ReadAllText(openFileDialog1.FileName, Encoding.Default);//向richtext中添加你所选择的文本


            }
        }
    }
}
原创粉丝点击