如何创建自定义消息框? [英] How to create a custom MessageBox?

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

问题描述

我正在尝试使用我的控件制作自定义消息框.

I'm trying to make a custom message box with my controls.

public static partial class Msg : Form
{
    public static void show(string content, string description)
    {

    }
}

实际上我需要在这个表单中放置一些控件(一个gridview)并且我必须为这个窗口应用我自己的主题,所以我不想使用MessageBox.我想从我的其他形式中调用它,例如

Actually I need to place some controls (a gridview) in this form and I have to apply my own theme for this window, so I don't want to use MessageBox. I want to call this from my other forms like

Msg.show(parameters);

我不想为此表单创建对象.

I don't wish to create an object for this form.

我知道我不能从 Form 类继承,因为它不是静态的.但我想知道 MessageBox 是如何实现的,因为它是静态的.它被称为 MessageBox.show("Some message!");

I know I can't inherit from Form class because it isn't static. But I wonder how MessageBox is implemented, because it is static. It is being called like MessageBox.show("Some message!");

现在我收到一个错误,因为不允许继承:

Now I'm getting an error because inheritance is not allowed:

静态类MyFormName"不能从类型System.Windows.Forms.Form"派生.静态类必须从对象派生

Static class 'MyFormName' cannot derive from type 'System.Windows.Forms.Form'. Static classes must derive from object

MessageBox是如何实现的?

推荐答案

您的表单类不必是 static.事实上,静态类根本无法继承.

Your form class needs not to be static. In fact, a static class cannot inherit at all.

相反,创建一个从 Form 派生的 internal 表单类,并提供一个 public static 辅助方法来显示它.

Instead, create an internal form class that derives from Form and provide a public static helper method to show it.

如果您不希望调用者甚至知道"底层表单,则此静态方法可以在不同的类中定义.

This static method may be defined in a different class if you don't want the callers to even "know" about the underlying form.

/// <summary>
/// The form internally used by <see cref="CustomMessageBox"/> class.
/// </summary>
internal partial class CustomMessageForm : Form
{
    /// <summary>
    /// This constructor is required for designer support.
    /// </summary>
    public CustomMessageForm ()
    {
        InitializeComponent(); 
    } 

    public CustomMessageForm (string title, string description)
    {
        InitializeComponent(); 

        this.titleLabel.Text = title;
        this.descriptionLabel.Text = description;
    } 
}

/// <summary>
/// Your custom message box helper.
/// </summary>
public static class CustomMessageBox
{
    public static void Show (string title, string description)
    {
        // using construct ensures the resources are freed when form is closed
        using (var form = new CustomMessageForm (title, description)) {
            form.ShowDialog ();
        }
    }
}

旁注:正如 Jalal 指出,您不必为了在其中包含 static 方法而创建一个 static 类.但是我仍然会将helper"类与实际表单分开,这样调用者就无法使用构造函数创建表单(当然,除非它们在同一个程序集中).

Side note: as Jalal points out, you don't have to make a class static in order to have static methods in it. But I would still separate the "helper" class from the actual form so the callers cannot create the form with a constructor (unless they're in the same assembly of course).

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

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