需要有关自定义消息框抛出处理错误的帮助 [英] Need help with a Custom message box throwing disposal error

查看:81
本文介绍了需要有关自定义消息框抛出处理错误的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用不同的主题制作我自己的自定义消息框,但当我尝试强制显示更多的框时,它会抛出一个错误



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>显示然后关闭,它会被处理掉,所以你不能再打开它。避免它。使用以下方法之一:



When you show a form with Show and then close, it gets disposed, so you cannot open it again. Just avoid it. Use one of these approaches:

  • Never use Show, only use ShowDialog, 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 method System.Windows.Forms.Form.OnFormClosing. Depending on CloseReason passed in the event arguments (if this is CloseReason.UserClosing), set Cancel 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屋!

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