如何创建自定义消息框?
我正在尝试用我的控件制作一个自定义消息框。
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”派生。静态类必须从对象派生
静态类必须从对象
,那么 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
How MessageBox
is implemented then?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的表单类不必是
静态
。事实上,静态类根本无法继承。相反,创建一个派生自
Form
的内部
表单类,并提供一个public static
辅助方法来显示它。如果您不希望调用者甚至“了解”底层表单,则可以在不同的类中定义此静态方法。
旁注:正如 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 fromForm
and provide apublic 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.
Side note: as Jalal points out, you don't have to make a class
static
in order to havestatic
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).您不需要该类是静态的。
只需执行以下操作:
You don't need the class to be static.
Just do something like:
您无需将类设为
静态
即可静态调用其方法之一 - 将特定方法声明为静态
就足够了。我们使用
messageBox.ShowDialog()
将表单显示为模式窗口。您可以使用DetailedMessageBox.ShowMessage("Content", "Description");
显示消息框。顺便说一句,您应该重新考虑您的命名并坚持一致的命名模式。
Msg
和show
是与 命名指南 - 您肯定想检查一下!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 asstatic
.We are using
messageBox.ShowDialog()
to have the form being displayed as a modal window. You can display the message box usingDetailedMessageBox.ShowMessage("Content", "Description");
.By the way, you should rethink your naming and stick to a consistent naming pattern.
Msg
andshow
are weak names that do no match the Naming Guidelines — you would definitely want to check those out!在 WPF 项目中,您可以添加一个新窗口并将其命名为 MessageBoxCustom,然后在 C# 中的 Void 中您可以找到 InitialiseComponent();您添加 2 个属性并将这些属性绑定到您应该在 XAML 视图中创建的 textBlock
例子:
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: