2017-5-2Winform 对话框控件

Posted Fengbao.2333

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2017-5-2Winform 对话框控件相关的知识,希望对你有一定的参考价值。

对话框控件:
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-2Winform 对话框控件的主要内容,如果未能解决你的问题,请参考以下文章

MFC:从控件本身的代码限制子控件的大小

在GridView控件中,添加了一个删除的功能,当点击删除时弹出对话框,确定是不是删除,请问如何用代码实现?

控件

MFC 中关于Edit控件问题

QT如何获得对话框中控件的指针?

MFC中如何实现Tab控件里子对话框里的控件跟着对主话框伸缩而改变位置