C#常用对话框

1、OpenFileDialog:打开文件

        private string GetOpenFileName()
        {
            openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Title = "打开文本文件";
            openFileDialog1.CheckFileExists = true;
            openFileDialog1.CheckPathExists = true;
            openFileDialog1.AddExtension = true;
            openFileDialog1.Multiselect = false;
            openFileDialog1.Filter = " txt files(*.txt)|*.txt|All files(*.*)|*.*";
            //openFileDialog1.FilterIndex = 1;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                return openFileDialog1.FileName;
            }
            else
            {
                return null;
            }
        }
        private void OpenFile(string filename)
        {
            try
            {
                FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
                FileInfo f = new FileInfo(filename);
                StreamReader fileread = new StreamReader(fs, Encoding.Default);
                richTextBox1.Text = fileread.ReadToEnd();
                fileread.Close();
                this.Text = f.Name;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

2、SaveFileDialog:保存文件

        private string GetSaveFileName()
        {
            saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.Title = "文件另存为";
            saveFileDialog1.OverwritePrompt = true; //如果文件已存在,弹出提示
            saveFileDialog1.CreatePrompt = true; //如果文件不存在,提示是否创建文件
            saveFileDialog1.AddExtension = true;
            saveFileDialog1.Filter = "txt files(*.txt)|*.txt|All files(*.*)|*.*";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                return saveFileDialog1.FileName;
            }
            else
            {
                return null;
            }
        }
        private void SaveFile(string filename)
        {
            try
            {
                StreamWriter filewrite = new StreamWriter(filename);
                filewrite.Write(richTextBox1.Text);
                filewrite.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

3、FontDialog:字体设置

        private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            fontDialog1 = new FontDialog();
            fontDialog1.ShowColor = true;
            fontDialog1.AllowScriptChange = true;
            fontDialog1.AllowVectorFonts = true;
            fontDialog1.ShowEffects = true;
            if (fontDialog1.ShowDialog() == DialogResult.OK && richTextBox1.SelectedText != "")
            {
                richTextBox1.SelectionFont = fontDialog1.Font;
                richTextBox1.SelectionColor = fontDialog1.Color;
            }
        }

4、ColorDialog:颜色设置

        private void 颜色ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            colorDialog1 = new ColorDialog();
            colorDialog1.AllowFullOpen = true;
            colorDialog1.AnyColor = true;
            colorDialog1.FullOpen = true;
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                richTextBox1.BackColor = colorDialog1.Color;
            }
        }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

__简言

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值