C# 用于错误和信息的自定义MessageBox
根据我的研究,让MessageBox表单以父表单为中心的唯一方法是编写自定义MessageBox类。我已成功实现CustomMessageBox表单,并且能够将我的错误和信息消息集中在父表单上。但是,我不知道如何使CustomMessageBox表单保持静态,以便不必实例化新的CustomMessageBox表单。我希望能够调用静态方法,如下所示: CustomMessageBox.Show(类型、消息等) 下面是我的MessageBox类的基本版本。理想情况下,我希望有显示此表单的功能,而不必实例化CustomMessageForm。这可能吗C# 用于错误和信息的自定义MessageBox,c#,messagebox,C#,Messagebox,根据我的研究,让MessageBox表单以父表单为中心的唯一方法是编写自定义MessageBox类。我已成功实现CustomMessageBox表单,并且能够将我的错误和信息消息集中在父表单上。但是,我不知道如何使CustomMessageBox表单保持静态,以便不必实例化新的CustomMessageBox表单。我希望能够调用静态方法,如下所示: CustomMessageBox.Show(类型、消息等) 下面是我的MessageBox类的基本版本。理想情况下,我希望有显示此表单的功能,而不必
namespace MarineService
{
public partial class CustomMessageForm : DevExpress.XtraEditors.XtraForm
{
private static CustomMessageForm form = new CustomMessageForm();
public CustomMessageForm()
{
InitializeComponent();
}
public void ShowDialog(string type, string message)
{
this.Text = type + "Information";
this.groupMessage.Text = type + "Information";
this.memoEditMessage.Lines[0] = message;
}
}
}
大概是这样的:
public partial class CustomMessageForm : DevExpress.XtraEditors.XtraForm
{
private static CustomMessageForm form = new CustomMessageForm();
public CustomMessageForm()
{
InitializeComponent();
}
private void ShowDialog(string type, string message)
{
form .Text = type + "Information";
form .groupMessage.Text = type + "Information";
form .memoEditMessage.Lines[0] = message;
form.ShowDialog();
}
public static Show(string type, string message)
{
if(form.Visible)
form.Close();
ShowDialog(type, message);
}
}
使用如下方式:
CustomMessageForm.Show("customtype", "warning!");
像这样的,只是个想法
如果这不是您想要的,请澄清。我在处理CustomMessageBox时想到的一个想法是创建一个静态实用程序类,该类上有一个静态方法,该方法创建CustomMessageBox的新实例,然后显示它。但是,如果实现了此解决方案,我必须将父窗体传递到实用程序类上的静态方法中,以将新CustomMessageBox置于父窗口的中心。我不确定这是否是一个好的设计,因为我是新的windows编程。欢迎任何意见和/或反馈。可能重复感谢,这正是我所需要的。我读了Hans的帖子,但是,在我读了你的帖子之前,它没有点击我可以使Show方法保持静态而不使整个类保持静态。谢谢你的帮助。