文章目录 说明 Messagebox的作用 Messagebox的用法(参数) Messagebox的图标 自定义Messagebox 实例 说明 我将从Message的作用,用法(参数),图标,自定义及实例5个方面来解释,需要的可以跳转 正文 Messagebox的作用 释义: 命名空间: System.Windows.Forms 程序集: System.Windows.Forms.dll “Message Box”是 Visual Basic 中的一个函数,功能是弹出一个对话框,等待用户单击按钮,并返回一个 Integer 值表示用户单击了哪一个按钮。在英语中意为“信箱”。 作用: MessageBox.show 的作用类似alert 、confirm 和prompt ,因此适合展示较为简单的内容。如果需要弹出较为复杂的内容,ShowDialog更合适 消息提示 当用户进行操作时会被触发,该对话框中断用户操作,直到用户确认知晓后才可关闭。 确认消息 提示用户确认其已经触发的动作,并询问是否进行此操作时会用到此对话框。 提交内容 当用户进行操作时会被触发,中断用户操作,提示用户进行输入的对话框 自定义 可自定义配置不同内容。 Messagebox的用法(参数) 可以点击这里去微软官方查看微软给出的方法 Equals(Object) GetHashCode() GetType() MemberwiseClone() Message.Show() Message.Show(Text,Title,nType,MessageBoxlcon); 参数1:弹出框要显示的内容 参数2:弹出框的标题 参数3:也可写作MessageBoxButtons,弹出框的按钮格式 参数4:弹出框的图标样式 注意: 4个参数除了Text外都可以省略,Text也可以用""输出无内容提示框 C#代码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 WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { MessageBox.Show(""); } } } 结果: 只显示信息MessageBox.Show("Error!"); 加上标题MessageBox.Show("马老师,发生了甚么事","Error!"); 弹出框的类型 MessageBoxButtons.AbortRetryIgnoreMessageBox.Show("马老师,发生了甚么事","Error!",MessageBoxButtons.AbortRetryIgnore); OKCancelMessageBox.Show("马老师,发生了甚么事","Error!",MessageBoxButtons.OKCancel); RetryCancelMessageBox.Show("这瓜保熟吗?","Error!",MessageBoxButtons.RetryCancel); YesNoMessageBox.Show("我能卖给你生瓜蛋子吗?","Error!",MessageBoxButtons.YesNo); YesNoCancelMessageBox.Show("你能记得你吃过多少面包片吗?","Error!",MessageBoxButtons.YesNoCancel); 第4个参数是指图标 Messagebox的图标 AsteriskMessageBox.Show("人行道上不是很宽敞吗?","Error!",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Asterisk); ErrorMessageBox.Show("人行道上不是很宽敞吗?","Error!",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Error); ExclamationMessageBox.Show("人行道上不是很宽敞吗?","Error!",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Exclamation); HandMessageBox.Show("人行道上不是很宽敞吗?","Error!",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Hand); InformationMessageBox.Show("今天也是ViVi的男孩","Error!",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Information); NoneMessageBox.Show("今天也是ViVi的男孩","Error!",MessageBoxButtons.YesNoCancel,MessageBoxIcon.None); QuestionMessageBox.Show("肥肉肥肉咔嚓减掉","Error!",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Question); StopMessageBox.Show("禁止中小学生引用纯净水?","Error!",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Stop); WarningMessageBox.Show("《本草纲目》","Error!",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Warning); 自定义Messagebox 问题 假如我想改变提示框的字体大小或者颜色,Windows自带的提示框就不行了。 思路 自己建立一个新的Form,之后放入Label和Button,之后用Form.ShowDialog的方法显示出来 简单实现 新建windowsForms项目 简单设计 就是拖动两个按钮,文本框和图片框,只用作显示,没有写逻辑,需要可以自定义 Form1 Form2 懒惰使我不想搞得太花里胡哨,但是你可以按照自己的心意修改 Form1代码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 test4 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { InputDialog.Show(); } } } 都是系统自动生成的,只有一句方法调用 InputDialog.cs代码using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace test4 { class InputDialog { public static DialogResult Show() { Form2 inputDialog = new Form2(); DialogResult result = inputDialog.ShowDialog(); return result; } } } 运行结果 实例 这里我贴一个以前做来传递参数的例子 说明: 利用弹出框获取用户输入的值,并更改原有文本数值 代码Form1.csusing 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 test3 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string str = strText.Text; str = string.Empty; InputDialog.Show(out str); MessageBox.Show("strText的值是:" + str, ""); strText.Text = str; } } } 代码InputDialog.csusing System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace test3 { public static class InputDialog { public static DialogResult Show(out string strText) { string strTemp = string.Empty; FrmInputDialog inputDialog = new FrmInputDialog(); inputDialog.TextHandler = (str) => { strTemp = str; }; DialogResult result = inputDialog.ShowDialog(); strText = strTemp; return result; } } } FrmInputDialogusing 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 test3 { public partial class FrmInputDialog : Form { public delegate void TextEventHandler(string strText); public TextEventHandler TextHandler; public FrmInputDialog() { InitializeComponent(); } private void btnOK_Click(object sender, EventArgs e) { if (null != TextHandler) { TextHandler.Invoke(txtString.Text); DialogResult = DialogResult.OK; } } private void btnCancel_Click(object sender, EventArgs e) { DialogResult = DialogResult.Cancel; } private void txtString_KeyPress(object sender, KeyPressEventArgs e) { //如果输入的不是退格和数字,则屏蔽输入 //if (!(e.KeyChar == '\b' || (e.KeyChar >= '0' && e.KeyChar <= '9'))) //{ // e.Handled = true; //} if (Keys.Enter == (Keys)e.KeyChar) { if (null != TextHandler) { TextHandler.Invoke(txtString.Text); DialogResult = DialogResult.OK; } } } private void txtString_TextChanged(object sender, EventArgs e) { int i; if (int.TryParse(txtString.Text, out i) == false) { txtString.Text = " "; } } private void FrmInputDialog_Load(object sender, EventArgs e) { } } } Form1设计 FrmInputDialog设计 实现效果 后续我写出其它的样式或者应用到也会陆续贴上来 学习方法: 技术人以技术立命,必须掌握学习的方法,遇到不会的问题,首先要Geogle了解这个东西,明白是干什么用的,然后找实例一步一步的写,最后消化掉成为自己的知识。牢记及时的输入与输出。 参考资料.图标大全 |
|