MDI中的表单只打开一次 [英] forms in MDI open once only

查看:83
本文介绍了MDI中的表单只打开一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#.NET和SQL SERVER制作桌面应用程序

i已经制作了mdi父表单,其中有3个表单

但表单只打开一次。我再次打开表单我收到消息

无法访问已处置的对象



第一表格

 私人  void  mediaMethodOfPublicityToolStripMenuItem_Click ( object  sender,EventArgs e)
{
Media_Method_of_Publicity objmedia = Media_Method_of_Publicity.MIDI();
objmedia.MdiParent = ;
objmedia.Show();
objmedia.BringToFront();
}



第二种形式

  private   void  publicityCampaignToolStripMenuItem_Click( object  sender,EventArgs e)
{
Publicity_Campaign publicity = Publicity_Campaign.MIDI();
publicity.MdiParent = ;
publicity.Show();
publicity.BringToFront();
}





我已经完成了两个课程

clsglobal.cs和编码是



 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
命名空间 Integrates_System_PSM
{
public 密封 class clsglobal
{
public < span class =code-keyword> static frmMDI MDI;
}
}





clsmain.cs

 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
命名空间 Integrates_System_PSM
{
class ClSmain
{
ClSmain()
{
}

}
}



和函数MIDI creted

  private   static  Media_Method_of_Publicity media; 
public static Media_Method_of_Publicity MIDI()
{
if (media == null // 如果尚未创建,则创建实例
media = new Media_Method_of_Publicity();
返回媒体;刚刚创建或创建的 // 返回
// 这是为了确保我们点击父表单上的'Media_Method_of_Publicity'菜单两次
// 它不应该打开同一子表单的两个实例
}



与其他3种形式相同

解决方案

考虑替代方案:不使用MDI。这种UI风格非常不方便,即使是微软也非常不鼓励。没有使用此风格的专业软件。最简单但功能强大的替代方案之一是基于 System.Windows.Forms.TabControl 的选项卡式界面。



有关此处的更多详细信息:

可以使用许多胜利形式环境 [ ^ ](参见我的解决方案)。



-SA


< blockquote>你可以通过隐藏mdi子窗体而不是关闭mdi子窗口来绕过这个问题。



捕获Form_Closing事件并将de event.Cancel属性设置为true。这将阻止mdi孩子关闭。然后你隐藏了mdi孩子。



当你想重新打开mdi子节点时,搜索mdi父节点的子节点以检查表单是否已经存在。如果是这样,只需取消隐藏mdi子项,否则创建一个新的mdi子实例。


hi,



问题是因为您正在尝试访问已经处理的对象而发生。



初始化Form对象时可能会出现问题。



  public   static  Media_Method_of_Publicity MIDI( )
{
if (media == null // 如果尚未创建,请创建实例
{
media = new Media_Method_of_Publicity();
media.Disposed + = new EventHandler(media_Disposed);
}
返回媒体;
}

静态 void media_Disposed( object sender,EventArgs e)
{
media = null ;
}





只需MIDI功能,并将此事件处理程序代码添加到您的班级。





问候

Ankit


i am making desktop application using C#.NET and SQL SERVER
i have made mdi parent form in which there are 3 forms
but the form openes once only.when i again open the form i get the message
cannot access disposed object

1st form

private void mediaMethodOfPublicityToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Media_Method_of_Publicity objmedia = Media_Method_of_Publicity.MIDI();
            objmedia.MdiParent = this;
            objmedia.Show();
            objmedia.BringToFront();
        }


2nd form

private void publicityCampaignToolStripMenuItem_Click(object sender, EventArgs e)
{
    Publicity_Campaign publicity =  Publicity_Campaign.MIDI();
    publicity.MdiParent = this;
    publicity.Show();
    publicity.BringToFront();
}



I have made two classes
clsglobal.cs and coding is

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Integrates_System_PSM
{
   public sealed class clsglobal
    {
       public static frmMDI MDI;
    }
}



clsmain.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Integrates_System_PSM
{
    class ClSmain
    {
        ClSmain()
        {
        }

    }
}


and a function MIDI creted

private static Media_Method_of_Publicity media;
        public static Media_Method_of_Publicity MIDI()
        {
            if (media == null) //if not created yet, Create an instance
                media = new Media_Method_of_Publicity();
            return media; //just created or created earlier.Return it
            //This is to make sure that when we Click on a 'Media_Method_of_Publicity' menu on Parent form twice
            //it should not open two instance of the same child form
        }


same for other 3 forms

解决方案

Consider alternative: not using MDI. This UI style is extremely inconvenient and is strongly discourages, even by Microsoft. There is no professional software using this style. One of the simplest yet robust alternatives is the tabbed interface based on System.Windows.Forms.TabControl.

For some more details here:
works with many win form environment[^] (see my solution).

—SA


You can bypass this problem by hiding the mdi child form instead of closing the mdi child.

catch the Form_Closing event and set de event.Cancel property as true. This will prevent the mdi child from closing. Then you hide the mdi child.

When you want to reopen the mdi child, search the childs of the mdi parent to check if the form allready exist. If so, just unhide the mdi child, otherwise create a new instance of the mdi child.


hi,

Problem is occuring because you are trying to access a object which is already disposed.

It might be the problem when you are initializing the Form object.

public static Media_Method_of_Publicity MIDI()
        {
            if (media == null) //if not created yet, Create an instance
            {
                media = new Media_Method_of_Publicity();
                media.Disposed+=new EventHandler(media_Disposed);
            }
            return media; 
        }

static void media_Disposed(object sender, EventArgs e)
        {
            media = null;
        }



Just MIDI function like this and Add this event handler code into your class.


Regards
Ankit


这篇关于MDI中的表单只打开一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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