您的位置:首页 > 编程语言 > C#

C#课后习题---消息对话框实验

2016-05-03 21:15 501 查看
题目要求:

窗体上有两个按钮:一个显示文本,一个显示图片。单击上面按钮或按下Ait+B键,可以弹出如图实验所示的消息框。单击下面的按钮也可弹出如图实验所示的消息框。

输入代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Xin
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("单击了我", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("单击了我", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
}

private void Form1_Load(object sender, EventArgs e)
{
this.KeyPreview = true;
button2.BackgroundImage = Image.FromFile(Application.StartupPath + @"\12.jpg");
button2.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult dt = MessageBox.Show("确认要退出吗?","结束提示框", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
if(DialogResult.Cancel==dt)
{
e.Cancel = true;//取消,不做任何操作
}
if(DialogResult.Yes==dt)
{
Application.Exit();
}
}

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Alt && e.KeyCode == Keys.B)
{
MessageBox.Show("单击了我", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
}
}
}
}


运行截图:

1.初始运行截图






2.单击按钮出现的消息框截图






3.单击运行窗口右上角“关闭”按钮显示的消息框





总结:

参考MessageBox.show函数详解,完成了该题目,并拓展了功能,感觉还好吧!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: