Visual Studio 2010——C#的通用对话框控件的使用
2013-04-01 21:35
513 查看
实验环境:Windows XP,Visual Studio 2010 Ultimate
1 创建项目
文件>>新建>>项目,选中“Windows窗体应用程序”,如下图所示:
![](http://img.my.csdn.net/uploads/201303/29/1364539493_4315.png)
2 点击菜单栏的“视图”,依次找到“解决方案资源管理器”,“工具箱”和“属性窗口”,分别点击它们。菜单如下图所示。
![](http://img.my.csdn.net/uploads/201303/29/1364539781_3835.png)
创建好工程后,效果图如下图所示
![](http://img.my.csdn.net/uploads/201303/29/1364539995_6723.png)
3 添加控件并且修改属性。
3.1 向Form1中添加1个toolStrip控件,一个label控件和一个openFileDialog控件。修改tooStrip控件的Items属性,添加5个Item项。其属性设置如下表所示。
说明:lable控件保持默认值。
3.2 toolStrip的Items属性——详细可以参考《Visual Studio 2010——C#的工具栏控件的使用》
![](http://img.my.csdn.net/uploads/201304/01/1364822197_8711.png)
4 添加代码。
4.1 双击各个button,添加代码如下所示意。
4.2 在class Form1中添加如下函数。
5 调试
单击Visual Studio 2010菜单栏的“调试”|"启动调试",然后点击各个工具,看看效果。如下图所示。
![](http://img.my.csdn.net/uploads/201304/01/1364824276_2671.png)
6 工程源码。点击下载:http://download.csdn.net/detail/q1302182594/5211020。
参考资料
《C#实用编程百例》,清华大学出版社,何鹏飞,王征等 编著
《C#程序设计——基础教程与实验指导》——清华大学出版社,孙晓非 牛小平 冯冠 李乃文 编著
《C#程序设计与案例教程》,清华大学出版社,杨树林,胡洁萍 编著
1 创建项目
文件>>新建>>项目,选中“Windows窗体应用程序”,如下图所示:
![](http://img.my.csdn.net/uploads/201303/29/1364539493_4315.png)
2 点击菜单栏的“视图”,依次找到“解决方案资源管理器”,“工具箱”和“属性窗口”,分别点击它们。菜单如下图所示。
![](http://img.my.csdn.net/uploads/201303/29/1364539781_3835.png)
创建好工程后,效果图如下图所示
![](http://img.my.csdn.net/uploads/201303/29/1364539995_6723.png)
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 |
3.2 toolStrip的Items属性——详细可以参考《Visual Studio 2010——C#的工具栏控件的使用》
![](http://img.my.csdn.net/uploads/201304/01/1364822197_8711.png)
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菜单栏的“调试”|"启动调试",然后点击各个工具,看看效果。如下图所示。
![](http://img.my.csdn.net/uploads/201304/01/1364824276_2671.png)
6 工程源码。点击下载:http://download.csdn.net/detail/q1302182594/5211020。
参考资料
《C#实用编程百例》,清华大学出版社,何鹏飞,王征等 编著
《C#程序设计——基础教程与实验指导》——清华大学出版社,孙晓非 牛小平 冯冠 李乃文 编著
《C#程序设计与案例教程》,清华大学出版社,杨树林,胡洁萍 编著
相关文章推荐
- Visual Studio 2010—— C#的ComboBox控件使用
- Visual Studio 2010——C#的DateTimePicker与MonthCalendar控件的使用
- Visual Studio 2010——C#的TreeView和ImageList控件的使用
- Visual Studio 2010——C#工具栏控件的使用
- Visual Studio 2010——C#的Splitter和Panel控件的使用
- Visual Studio 2010——C#的ProgressBar和TrackBar控件的使用
- Visual Studio 2010——C#的TabControl和UpDown控件的使用
- Visual Studio 2010——C#状态栏控件的使用
- 使用Visual Studio的动态连接库创建通用数据库连接对话框
- Visual Studio 2010 使用Windows Media Player控件实现播放音乐视频
- Visual Studio 2010——C#的ListView控件的使用
- Visual C# 2008+SQL Server 2005 数据库与网络开发-- 13.3 使用公共对话框控件
- Visual Studio 2010——C#的ScrollBar和PictureBox控件的使用
- Visual Studio 2010——C#的CheckBox和RadioButton使用
- Visual Studio 2010使用combobox控件就崩溃
- 使用Visual Studio 2010写Data Url生成工具C#版本
- 第六章:模式对话框和Windows通用控件---第八章:使用ActiveX控件
- Visual Studio 2010——C#的NotityIcon和ToolTip的使用
- 10个C#编程和Visual Studio 2010 使用技巧
- Visual Studio 2010——C#的LinkLabel控件的使用