首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C#第12周实验-任务二(设计一个窗体)-打开对话框

2012-11-23 
C#--第12周实验--任务2(设计一个窗体)--打开对话框/* (程序头部注释开始)* 程序的版权和版本声明部分* Cop

C#--第12周实验--任务2(设计一个窗体)--打开对话框

/* (程序头部注释开始)   * 程序的版权和版本声明部分   * Copyright (c) 2011, 烟台大学计算机学院学生    * All rights reserved.   * 文件名称:消息对话框 * 作 者: 雷恒鑫    * 完成日期: 2012 年 11 月 12 日   * 版 本 号: V1.0    * 对任务及求解方法的描述部分   * 输入描述:在窗体上增加一个按钮,并设置按钮显示文字为“结束程序”。单击该按钮显示如下消息框,若选择是,则结束程序,否则只是关闭消息框。 * 关键代码:                DialogResult result = MessageBox.Show("确定要结束程序吗?[Yes|No]", "提示",                             MessageBoxButtons.YesNo, MessageBoxIcon.Warning,                              MessageBoxDefaultButton.Button2);                             if (result==DialogResult.Yes)                             {                                Application.Exit();                             } * 问题描述:   * 程序输出:   * 程序头部的注释结束   */using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.IO;namespace WindowsFormsApplication1{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void Form1_Load(object sender, EventArgs e)        {            this.pictureBox1.Image = Image.FromFile("风景1.jpg");//动态加载图片            this.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;//设置图片显示方式            this.pictureBox2.Image = Image.FromFile("风景2.jpg");//动态加载图片            this.pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;//设置图片显示方式            toolTip1.SetToolTip(pictureBox1, "这是山东省阳谷县的风光");            toolTip1.SetToolTip(pictureBox2, "这是山东省阳谷县武松打虎的自然风光");        }        private void button1_Click(object sender, EventArgs e)        {            if (DialogResult.Yes == MessageBox.Show("确定要结束程序吗?[Yes|No]", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button3))            {                Application.Exit();            }        }        private void button2_Click(object sender, EventArgs e)        {            this.saveFileDialog1.Filter = "*.txt|*.txt";            this.saveFileDialog1.ShowDialog();            string file = this.saveFileDialog1.FileName;            if (string.IsNullOrEmpty(file))                return;            //以下为写字符到文本文件,需要添加System.IO引用            //创建一个文件流            FileStream fs = new FileStream(file, FileMode.OpenOrCreate,               FileAccess.Write);            //创建一个StreamWriter对象            StreamWriter sw = new StreamWriter(fs);            textBox1.SelectAll();            sw.Write(textBox1.SelectedText);            //释放StreamWriter对象,文件流对象            sw.Dispose();            fs.Dispose();        }        private void button3_Click(object sender, EventArgs e)        {            this.openFileDialog1.Filter = "*.txt|*.txt";            this.openFileDialog1.ShowDialog();            string file = this.openFileDialog1.FileName;            if (string.IsNullOrEmpty(file)) return;            //以下为写字符到文本文件,需要添加System.IO引用            //创建一个文件流            FileStream fs = new FileStream(file, FileMode.Open,                FileAccess.Read);            //创建一个StreamWriter对象            StreamReader sr = new StreamReader(fs);            this.textBox1.Text = sr.ReadToEnd();            //释放StreamWriter对象,文件流对象            sr.Dispose();            fs.Dispose();        }    }}


运行结果:

 

C#第12周实验-任务二(设计一个窗体)-打开对话框

 

C#第12周实验-任务二(设计一个窗体)-打开对话框

 

 

热点排行