码迷,mamicode.com
首页 > Windows程序 > 详细

【2017-5-2】Winform 对话框控件

时间:2017-05-03 17:11:52      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:查找   selectall   click   ogr   result   lte   toolstrip   open   tostring   

对话框控件:
ColorDialog - 颜色选择
ShowDialog()
DialogResult 类型
对象.Color

FontDialog - 字体选择
ShowDialog()
DialogResult
对象.Font
对象.Color

--------------------
OpenFileDialog - 文件打开对话框

ShowDialog()
对象.FileName

有文件路径,如何打开此文件?
使用 流
using System.IO

SaveFileDialog - 文件保存位置对话框

folderBrowserDialog - 选择文件对话框,只能看到路径,看不到文件

 

 

 

 

写在构造函数下面

private void 撤消UToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textBox1.Undo();
        }

        private void 剪切TToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textBox1.Cut();
        }

        private void 粘贴PToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textBox1.Paste();
        }

        private void 复制CToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textBox1.Copy();
        }

        private void 全选AToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textBox1.SelectAll();
        }

        private void 时间ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textBox1.Text += DateTime.Now.ToString("HH:mm yyyy-MM-dd");
        }

        private void 查找ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2(this);
            f2.Owner = this;
            f2.Show();
        }

        private void 自动换行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (textBox1.WordWrap)
            {
                textBox1.WordWrap = false;
                textBox1.ScrollBars = ScrollBars.Both;
            }
            else
            {
                textBox1.WordWrap = true;
                textBox1.ScrollBars = ScrollBars.Vertical;
            }
        }

        private void 颜色ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult drr = colorDialog1.ShowDialog();

            if (drr == DialogResult.OK)
            {
                textBox1.ForeColor = colorDialog1.Color;
            }


        }

        private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult dr = fontDialog1.ShowDialog();
            if (dr == DialogResult.OK)
            {
                textBox1.Font = fontDialog1.Font;
                textBox1.ForeColor = fontDialog1.Color;

            }


        }

        private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "文本文件|*.txt|文档|*.doc|所有文件|*.*";
            DialogResult dr = openFileDialog1.ShowDialog();
            if (dr == DialogResult.OK)
            {
                StreamReader sr = new StreamReader(openFileDialog1.FileName, Encoding.Default);
                textBox1.Text = sr.ReadToEnd();



            }
        }

        string savePath = "";
        private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (savePath == "")
            {
                saveFileDialog1.FileName = "*.txt";
                saveFileDialog1.Filter = "文本文件|*.txt";
                DialogResult dr = saveFileDialog1.ShowDialog();

                if (dr == DialogResult.OK)
                {
                    StreamWriter sw = new StreamWriter(saveFileDialog1.FileName);
                    sw.Write(textBox1.Text);
                    sw.Flush();
                    sw.Close();

                    savePath = saveFileDialog1.FileName;
                }
            }
            else
            {
                StreamWriter sw = new StreamWriter(saveFileDialog1.FileName);
                sw.Write(textBox1.Text);
                sw.Flush();
                sw.Close();
                savePath = saveFileDialog1.FileName;
            
            }
        }

 

【2017-5-2】Winform 对话框控件

标签:查找   selectall   click   ogr   result   lte   toolstrip   open   tostring   

原文地址:http://www.cnblogs.com/hanqi0216/p/6802369.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!