现在的位置: 首页 > 综合 > 正文

OpenFileDialog

2013年06月25日 ⁄ 综合 ⁄ 共 2784字 ⁄ 字号 评论关闭

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFiledDialog=new OpenFileDialog();
            openFiledDialog.InitialDirectory = "D:\\";
            openFiledDialog.Filter = "文本文件|*.*|C#文件|*.cs|所有文件|*.*";
            openFiledDialog.FilterIndex= 1;
            if (openFiledDialog.ShowDialog() == DialogResult.OK)
            {
            //打开文件对话框中选择的文件名
                string fname = openFiledDialog.FileName;
                //创建从字符串进行读取的StringReader对象
                StreamReader sr = File.OpenText(fname);
                string str;
                while((str=sr.ReadLine())!=null)
                {
                //将读出的字符串在richTextBox1中显示;
                    this.richTextBox1.Text+=str;
                }

private void button2_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog=new SaveFileDialog();
            saveFileDialog.Filter = "文本文件|*.*|C#文件|*.cs|所有文件|*.*";
            saveFileDialog.FilterIndex = 2;
            saveFileDialog.RestoreDirectory = true;
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                string fName = saveFileDialog.FileName;
                //向指定路径(fName)的文件中追加内容;
                StreamWriter sw = File.AppendText(fName);
                //将richTextBox1控件中的内容写入到指定文件中;
                sw.Write(this.richTextBox1.Text);
                sw.Write("添加到文件中的内容:添加的内容信息!");
                sw.Flush();
                sw.Close();
            }

private void button3_Click(object sender, EventArgs e)
        {
            FontDialog fontDialog = new FontDialog();
            fontDialog.Color = richTextBox1.ForeColor;
            //指示用户能否更改"脚本"组合框中的字符集
            fontDialog.AllowScriptChange = true;
            //该值指示对话框是否显示颜色选择;
            fontDialog.ShowColor = true;
            if (fontDialog.ShowDialog() != DialogResult.Cancel)
            {
                richTextBox1.SelectionFont = fontDialog.Font;
            }

private void button4_Click(object sender, EventArgs e)
        {
            ColorDialog colorDialog = new ColorDialog();
            colorDialog.AllowFullOpen = true;
            colorDialog.FullOpen = true;
            colorDialog.ShowHelp = true;
            colorDialog.Color = Color.Black;
            //初始化当然前文本框中的颜色
            colorDialog.ShowDialog();
            richTextBox1.SelectionColor = colorDialog.Color;

        }

        private void button5_Click(object sender, EventArgs e)
        {
            PageSetupDialog pageSetupDialog = new PageSetupDialog();
            pageSetupDialog.Document =printDocument1 ;
            pageSetupDialog.ShowDialog();

        }
            }
        }
    }
}

抱歉!评论已关闭.