如何自定义消息框 [英] How to customize message box

查看:158
本文介绍了如何自定义消息框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做的C#应用​​程序,我想换一个消息框的样式。 ?是否有可能与否

I am doing C# application, and I want to change the style of a message box. Is it possible or not?

例如:改变按钮样式,前景色,等等

Example: change button style, fore color, etc.

推荐答案

您不能restyle默认的MessageBox因为这是依赖于当前的Windows操作系统的主题,但是你可以轻松地创建自己的MessageBox。只是一个新的形式(即MyNewMessageBox)添加到您的项目中使用这些设置:

You can't restyle the default MessageBox as that's dependant on the current Windows OS theme, however you can easily create your own MessageBox. Just add a new form (i.e. MyNewMessageBox) to your project with these settings:

FormBorderStyle    FixedToolWindow
ShowInTaskBar      False
StartPosition      CenterScreen

要显示它使用 myNewMessageBoxInstance.ShowDialog(); 。和一个标签和按钮添加到您的形式,如确定和取消,并适当设置其DialogResults,即增加一键 MyNewMessageBox 并称之为 btnOK 。在属性窗口中 DialogResult.OK 的DialogResult 属性。当按下该按钮将返回OK结果:

To show it use myNewMessageBoxInstance.ShowDialog();. And add a label and buttons to your form, such as OK and Cancel and set their DialogResults appropriately, i.e. add a button to MyNewMessageBox and call it btnOK. Set the DialogResult property in the property window to DialogResult.OK. When that button is pressed it would return the OK result:

MyNewMessageBox myNewMessageBoxInstance = new MyNewMessageBox();
DialogResult result = myNewMessageBoxInstance.ShowDialog();
if (result == DialogResult.OK)
{
    // etc
}

这将是明智的补充,把你需要的文字和其他选项自己的显示方式:

It would be advisable to add your own Show method that takes the text and other options you require:

public DialogResult Show(string text, Color foreColour)
{
     lblText.Text = text;
     lblText.ForeColor = foreColour;
     return this.ShowDialog();
}

这篇关于如何自定义消息框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆