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

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

问题描述

我试图做一个定制的消息框,我的控制。

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.

我知道我不能从表继承类,因为它不是一成不变的。但我不知道如何的MessageBox 的实施,因为它是静态的。这是被称为像 MessageBox.show(一些消息!);

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 实现呢?​​

推荐答案

您的窗体类不必是静态。事实上,静态类不能继承所有

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

相反,创建一个内部 form类从表格导出并提供了公共静态 helper方法来显示它

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 ();
        }
    }
}

边注:为塔拉巴尼<一个href=\"http://stackoverflow.com/questions/6932792/how-to-do-a-c-messagebox-like-static-form/6932846#6932846\">points出,你没有使一个类静态,以便有在它的静态方法。不过,我仍然会与实际表格分开帮手类,以便调用者不能以构造创建表单(除非他们当然在相同的程序集)。

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).

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

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