Visual Studio 2010——C#的通用对话框控件的使用

实验环境:Windows XP,Visual Studio 2010  Ultimate


1 创建项目

    文件>>新建>>项目,选中“Windows窗体应用程序”,如下图所示:

Visual Studio 2010——C#的通用对话框控件的使用_第1张图片    

2 点击菜单栏的“视图”,依次找到“解决方案资源管理器”,“工具箱”和“属性窗口”,分别点击它们。菜单如下图所示。

Visual Studio 2010——C#的通用对话框控件的使用_第2张图片

创建好工程后,效果图如下图所示

Visual Studio 2010——C#的通用对话框控件的使用_第3张图片


3 添加控件并且修改属性。

3.1 向Form1中添加1个toolStrip控件,一个label控件和一个openFileDialog控件。修改tooStrip控件的Items属性,添加5个Item项。其属性设置如下表所示。

控件 属性

Form1 Text testDialog
toolStrip Items 如3.2所示。
openfileDialog Name MyOpenFileDg
  Filter Text Files(*.txt)|*.txt|All Files(*.*)|*.*
  RestoreDirectory True
  FilterIndex 2
toolStripButton1 Text 退出
  Name tSBExit
  Image 自选一张图片
toolStripButton2 Text 打开文件
  Name tSBOpen
  Image 自选一张图片
toolStripButton3 Text 保存文件
  Name tSBSave
  Image 自选一张图片
toolStripButton4 Text 选择颜色
  Name tSBColor
toolStripButton5 Text 选择字体
  Name tSBFont
说明:lable控件保持默认值。

3.2 toolStrip的Items属性——详细可以参考《Visual Studio 2010——C#的工具栏控件的使用》



4 添加代码。

4.1 双击各个button,添加代码如下所示意。

        private void tSBExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }


        private void tSBOpen_Click(object sender, EventArgs e)
        {
            MyOpenFile();
        }


        private void tSBSave_Click(object sender, EventArgs e)
        {
            MySaveFile();
        }


        private void tSBFont_Click(object sender, EventArgs e)
        {
            MySetFont();
        }


        private void tSBColor_Click(object sender, EventArgs e)
        {
            MySetColor();
        }




4.2 在class Form1中添加如下函数。

 private void MyOpenFile()
        {
            if (MyOpenFileDg.ShowDialog() == DialogResult.OK)
            {
                label1.Text = "你选择打开的文件是\n" + MyOpenFileDg.FileName;
            }
        }

        private void MySaveFile()
        {
            SaveFileDialog MySaveFileDg = new SaveFileDialog();
            MySaveFileDg.Filter = this.MyOpenFileDg.Filter;
            MySaveFileDg.CreatePrompt = true;
            MySaveFileDg.OverwritePrompt = true;
            MySaveFileDg.RestoreDirectory = true;
            if (MySaveFileDg.ShowDialog() == DialogResult.OK)
            {
                label1.Text = "你要保存的文件为\n" + MySaveFileDg.FileName;
            }
        }

        private void MySetColor()
        {
            ColorDialog MyColorDg = new ColorDialog();
            MyColorDg.CustomColors = new int[]{
                0x000000,0xffffff,0xff0000,0x00ff00,0x0000ff
            };
            if (MyColorDg.ShowDialog() == DialogResult.OK)
            {
                label1.BackColor = MyColorDg.Color;
            }
        }

        private void MySetFont()
        {
            FontDialog MyFontDg = new FontDialog();
            MyFontDg.Font = label1.Font;
            MyFontDg.ShowApply = true;
            MyFontDg.ShowEffects = true;
            if (MyFontDg.ShowDialog() == DialogResult.OK)
            {
                label1.Font = MyFontDg.Font;
            }
        }

5 调试

单击Visual Studio 2010菜单栏的“调试”|"启动调试",然后点击各个工具,看看效果。如下图所示。

Visual Studio 2010——C#的通用对话框控件的使用_第4张图片


6 工程源码。点击下载:http://download.csdn.net/detail/q1302182594/5211020。


参考资料

《C#实用编程百例》,清华大学出版社,何鹏飞,王征等 编著

《C#程序设计——基础教程与实验指导》——清华大学出版社,孙晓非 牛小平 冯冠  李乃文 编著

《C#程序设计与案例教程》,清华大学出版社,杨树林,胡洁萍 编著


你可能感兴趣的:(C#,Visual,Studio,2010)