需要有关自定义消息框抛出处理错误的帮助 [英] Need help with a Custom message box throwing disposal error
本文介绍了需要有关自定义消息框抛出处理错误的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用不同的主题制作我自己的自定义消息框,但当我尝试强制显示更多的框时,它会抛出一个错误
System.Windows.Forms.dll中出现未处理的System.ObjectDisposedException类型异常
如果有人知道如何解决我的问题,将不胜感激
课程代码
i am trying to make my own custom message boxes using a different theme but when i try to force the box to show up more then once it throws a error
An unhandled exception of type 'System.ObjectDisposedException' occurred in System.Windows.Forms.dll
it would be greatly appreciated if any one knows how to fix my issue
The classes code
namespace Zeron
{
public class Message
{
public enum Type
{
Error,
Info,
Warning
}
public void Show(String Text, Type Type)
{
Message_Form msg = new Message_Form();
msg.sTitle = Type.ToString();
msg.sText = Text;
switch (Type)
{
case Type.Error:
msg.Style = MetroColorStyle.Red;
break;
case Type.Info:
msg.Style = MetroColorStyle.Blue;
break;
case Type.Warning:
msg.Style = MetroColorStyle.Yellow;
break;
default:
msg.Style = MetroColorStyle.Silver;
break;
}
msg.Show();
}
public void ShowDialog(String Text, Type Type)
{
Message_Form msg = new Message_Form();
msg.sTitle = Type.ToString();
msg.sText = Text;
switch (Type)
{
case Type.Error:
msg.Style = MetroColorStyle.Red;
break;
case Type.Info:
msg.Style = MetroColorStyle.Blue;
break;
case Type.Warning:
msg.Style = MetroColorStyle.Yellow;
break;
default:
msg.Style = MetroColorStyle.Silver;
break;
}
msg.ShowDialog();
}
}
}
表格中的代码
Code from the form
namespace Zeron
{
public partial class Message_Form : MetroForm
{
public Message_Form()
{
InitializeComponent();
}
public string sTitle { set { this.Text = value; } }
public string sText { set { rtb_Text.Text = value; } }
private void Message_Form_FormClosing(object sender, FormClosingEventArgs e)
{
this.Text = "Title";
rtb_Text.Text = "Text";
this.Style = MetroColorStyle.Silver;
this.Theme = MetroThemeStyle.Light;
}
}
}
推荐答案
当您使用<显示表单时code>显示然后关闭,它会被处理掉,所以你不能再打开它。避免它。使用以下方法之一:
- 从不使用
显示
,仅使用ShowDialog
,然后它会按你的意愿工作:你可以多次显示它,重复使用表格。- 如果你需要使用
显示
,在尝试用户关闭时隐藏它,而不是关闭;为此,重写虚方法System.Windows.Forms.Form.OnFormClosing
。根据事件参数中传递的CloseReason
(如果这是CloseReason.UserClosing
),设置取消
为true并隐藏表单:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.onformclosing%28v=vs.110%29。 aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.windows.forms.formclosingeventargs%28v=vs.110%29.aspx 一> [ ^ ],
http://msdn.microsoft.com/en-us/library/system.windows.forms.formclosingeventargs.closereason%28v=vs。 110%29.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.windows.forms.closereason%28v=vs.110%29。 aspx [ ^ ]。
它将允许您再次显示表单,因为它不会被处理。此外,表单将在节目之间保留其状态。- 不要重复使用
显示
显示的表单,而是创建表单的新实例每次。
When you show a form withShow
and then close, it gets disposed, so you cannot open it again. Just avoid it. Use one of these approaches:
- Never use
Show
, only useShowDialog
, then it will work as you want: you will be able to show it multiple times, reusing the form.- If you need to use
Show
, hide it on attempt to close by the user, instead of closing; to do so, override the virtual methodSystem.Windows.Forms.Form.OnFormClosing
. Depending onCloseReason
passed in the event arguments (if this isCloseReason.UserClosing
), setCancel
to true and hide the form instead:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.onformclosing%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.formclosingeventargs%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.formclosingeventargs.closereason%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.closereason%28v=vs.110%29.aspx[^].
It will allow you to show the form again, as it won't be disposed. Besides, the form will preserve its states between shows.- Don't reuse the form shown via
Show
, instead, create a new instance of the form each time.
这篇关于需要有关自定义消息框抛出处理错误的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文