如何创建自定义消息框?

发布于 2024-11-27 22:07:38 字数 749 浏览 2 评论 0原文

我正在尝试用我的控件制作一个自定义消息框。

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

    }
}

实际上,我需要在此表单中放置一些控件(网格视图),并且我必须为此窗口应用我自己的主题,所以我不想使用 MessageBox。我想从其他表单中调用它,就像

Msg.show(parameters);

我不希望为此表单创建对象一样。

我知道我不能从 Form 类继承,因为它不是静态的。但我想知道 MessageBox 是如何实现的,因为它是静态的。它的调用方式类似于 MessageBox.show("Some message!");

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

静态类“MyFormName”无法从类型“System.Windows.Forms.Form”派生。静态类必须从对象派生

静态类必须从对象Screenshot of my form

,那么 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)
    {

    }
}

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.

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:

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

Screenshot of my form

How MessageBox is implemented then?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夏了南城 2024-12-04 22:07:38

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

相反,创建一个派生自 Form内部表单类,并提供一个public static辅助方法来显示它

如果您不希望调用者甚至“了解”底层表单,则可以在不同的类中定义此静态方法。

/// <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”类与实际表单分开,以便调用者无法使用构造函数创建表单(当然,除非它们位于同一个程序集中)。

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

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

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

阳光下的泡沫是彩色的 2024-12-04 22:07:38

您不需要该类是静态的。
只需执行以下操作:

public partial class Msg : Form
{
    public static void show(string content, string description)
    {
         Msg message = new Msg(...);
         message.show();
    }
}

You don't need the class to be static.
Just do something like:

public partial class Msg : Form
{
    public static void show(string content, string description)
    {
         Msg message = new Msg(...);
         message.show();
    }
}
初见 2024-12-04 22:07:38

您无需将类设为静态即可静态调用其方法之一 - 将特定方法声明为静态就足够了。

public partial class DetailedMessageBox : Form
{
    public DetailedMessageBox()
    {
        InitializeComponent();
    }

    public static void ShowMessage(string content, string description)
    {
        DetailedMessageBox messageBox = new DetailedMessageBox();
        messageBox.ShowDialog();
    }
}

我们使用 messageBox.ShowDialog() 将表单显示为模式窗口。您可以使用 DetailedMessageBox.ShowMessage("Content", "Description"); 显示消息框。

顺便说一句,您应该重新考虑您的命名并坚持一致的命名模式。 Msgshow 是与 命名指南 - 您肯定想检查一下!

You don't need to make the class static in order to call one of its methods statically — it's sufficient to declare the particular method as static.

public partial class DetailedMessageBox : Form
{
    public DetailedMessageBox()
    {
        InitializeComponent();
    }

    public static void ShowMessage(string content, string description)
    {
        DetailedMessageBox messageBox = new DetailedMessageBox();
        messageBox.ShowDialog();
    }
}

We are using messageBox.ShowDialog() to have the form being displayed as a modal window. You can display the message box using DetailedMessageBox.ShowMessage("Content", "Description");.

By the way, you should rethink your naming and stick to a consistent naming pattern. Msg and show are weak names that do no match the Naming Guidelines — you would definitely want to check those out!

小霸王臭丫头 2024-12-04 22:07:38

在 WPF 项目中,您可以添加一个新窗口并将其命名为 MessageBoxCustom,然后在 C# 中的 Void 中您可以找到 InitialiseComponent();您添加 2 个属性并将这些属性绑定到您应该在 XAML 视图中创建的 textBlock
例子:

public MessageBoxCustom(string Message, string Title)
{
    InitialiseComponent();//this comes first to load Front End
    textblock1.Text = Title;
    textblock2.Text = Message;
}

Just position your TextBlocks where you want them to be displayed in XAML


From your Main Window you can call that message box like this


private void Button_Click()
{
    MessageBoxCustom msg = new MessageBoxCustom("Your message here","Your title her");
    msg.ShowDialog();
}

In a WPF project you can add a new window and call it MessageBoxCustom then inside C# the Void where you can find InitialiseComponent(); you add 2 properties and bind those properties to the textBlocks you should have created inside your XAML view
Example:

public MessageBoxCustom(string Message, string Title)
{
    InitialiseComponent();//this comes first to load Front End
    textblock1.Text = Title;
    textblock2.Text = Message;
}

Just position your TextBlocks where you want them to be displayed in XAML


From your Main Window you can call that message box like this


private void Button_Click()
{
    MessageBoxCustom msg = new MessageBoxCustom("Your message here","Your title her");
    msg.ShowDialog();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文