博客
关于我
Windows应用程序~~MessageBox的使用
阅读量:523 次
发布时间:2019-03-07

本文共 2383 字,大约阅读时间需要 7 分钟。

一. 增强窗体的友好性 MessageBox的使用

在Windows开发中,MessageBox是一种常用的提示信息工具,通过Show方法可以创建信息框。它能够有效地向用户显示提示信息,并与用户交互。

1. 添加.MessageBox提示信息

使用Show方法显示提示信息,无需参数即可创建信息框。

525. Syntax: `MessageBox.Show("提示信息")` Example: `MessageBox.Show("用户名或密码有误!")`

2. 添加.MessageBox显示标题

为了显示信息框的标题,可以在Show方法中添加第二个参数。

Syntax: `MessageBox.Show("提示信息","显示标题")` Example: `MessageBox.Show("登录验证不通过","系统提示")`

3. 添加.MessageBox按钮

需要对信息框添加按钮,通常使用MessageBoxButtons枚举类型,例如OK、Cancel、Yes、No等。

Syntax: `MessageBox.Show("提示信息","显示标题",信息框按钮)` Example: `MessageBox.Show("登录验证失败","系统提示",MessageBoxButtons.YesNo)`

4. 添加.MessageBoxIcon图标

为了增强信息框的视觉效果,可以使用MessageBoxIcon枚举类型添加图标。

Syntax: `MessageBox.Show("提示信息","显示标题",信息框按钮,信息框图标)` Example: `MessageBox.Show("登录验证失败","系统提示",MessageBoxButtons.YesNo, MessageBoxIcon.Error)`

二. 常用的.MessageBoxButtons按钮

根据需求不同,常用的按钮组合有:

  • **AbortRetryIgnore (中止、重试、忽略)**
  • **OK (确定)**
  • **OKCancel (确定、取消)**
  • **RetryCancel (重试、取消)**
  • **YesNo (是、否)**
  • **YesNoCancel (是、否、取消)**

三. 常用的.MessageBoxIcon图标

图标可以用来增强信息框的视觉效果,常用的有:

  • **Information (信息标识)**
  • **Error (错误标识)**
  • **Exclamation (警告标识)**
  • **Question (询问标识)**

四. 获取信息框按钮的值

如果需要根据用户选择的按钮执行不同的操作,可以通过DialogResult枚举类型获取返回值。

按钮名称 返回值
确定 1
取消 2
中止 3
重试 4
忽略 5
6
7

可以通过对

`DialogResult result = MessageBox.Show(...)`
使用条件语句判断用户选择的按钮。例如:

```csharpif (result == DialogResult.OK){ // 确定按钮被点击}```

五. 通过.MessageBox做一个验证提示

在代码中使用MessageBox可以实现各种提示需求。例如,在登录按钮点击时,可以先验证输入是否有效。

双击登录按钮即可打开代码编辑器,以下是验证登录功能的代码示例:

private void btnLogin_Click(object sender, EventArgs e){    // 判断用户名、密码和用户类型是否不为空    if (CheckInput())    {        MessageBox.Show("登录成功!", "登录提示", MessageBoxButtons.OK);    }}/// /// 判断用户名、密码和用户类型的非空验证/// private bool CheckInput(){    // 判断用户名是否为空    if (txtAccount.Text == string.Empty)    {        MessageBox.Show("请输入用户名", "输入提示", MessageBoxButtons.OK, MessageBoxIcon.Information);        this.txtAccount.Focus();        return false;    }    // 判断密码是否为空    if (txtPassword.Text == string.Empty)    {        MessageBox.Show("请输入密码", "输入提示", MessageBoxButtons.OK, MessageBoxIcon.Information);        this.txtPassword.Focus();        return false;    }    // 判断用户类型是否为空    if (cmbHType.Text == string.Empty)    {        MessageBox.Show("请选择登录类型", "输入提示", MessageBoxButtons.OK, MessageBoxIcon.Information);        this.cmbHType.Focus();        return false;    }    // 如果都不为空则返回true    return true;}

以上代码中,CheckInput函数会检查用户输入是否为空,如果有空格即可调用对应的提示信息并跳转到相应字段输入聚焦。同时,登录成功时会显示确认信息。

转载地址:http://tounz.baihongyu.com/

你可能感兴趣的文章
Openlayers layer 基础及重点内容讲解
查看>>
Openlayers map三要素(view,target,layers),及其他参数属性方法介绍
查看>>
Openlayers Map事件基础及重点内容讲解
查看>>
Openlayers Select的用法、属性、方法、事件介绍
查看>>
Openlayers Source基础及重点内容讲解
查看>>
Openlayers view三要素(zoom,center,projection)及其他参数属性方法介绍
查看>>
Openlayers 入门教程(一):应该如何学习 Openlayers
查看>>
openlayers 入门教程(三):view 篇
查看>>
openlayers 入门教程(九):overlay 篇
查看>>
openlayers 入门教程(二):map 篇
查看>>
openlayers 入门教程(五):sources 篇
查看>>
openlayers 入门教程(八):Geoms 篇
查看>>
openlayers 入门教程(十三):动画
查看>>
openlayers 入门教程(十二):定位与轨迹
查看>>
openlayers 入门教程(十五):与 canvas、echart,turf 等交互
查看>>
openlayers 入门教程(十四):第三方插件
查看>>
openlayers 入门教程(四):layers 篇
查看>>
OpenLayers 项目分析(三)-OpenLayers中定制JavaScript内置类
查看>>
Openlayers中使用Cluster+Overlay实现点击单个要素和聚合要素时显示不同弹窗
查看>>
Openlayers中使用Cluster实现点位元素重合时动态聚合与取消聚合
查看>>