本帖最后由 a7373773 于 2009-09-11 11:32:03 编辑

解决方案 »

  1.   

    弹什么异常你不能描述下? StackOverFlowException或者是什么别的,,,
    另外
    用主窗体弹出Form2 在Form2里弹出Form3  个人认为你这做法很不好
    如果还有子窗体  你是不是要在FormN 里面弹出Form(N+1)呢?
      

  2.   


    不好意思 我真正的项目里 是有这样的需要
    请明白 第一级界面是ShowDialog 第二级界面而第二界面是 Show 第三界面,,
    这个demo 只是模拟这样的需求你可以下载运行看看异常是无法访问已释放的对象。
    未处理 System.ObjectDisposedException
      Message="无法访问已释放的对象。\r\n对象名:“TextBoxX”。"
      Source="System.Windows.Forms"
      ObjectName="TextBoxX"
      StackTrace:
           在 System.Windows.Forms.Control.CreateHandle()
           在 System.Windows.Forms.TextBoxBase.CreateHandle()
           在 System.Windows.Forms.Control.get_Handle()
           在 System.Windows.Forms.Control.PointToScreen(Point p)
           在 System.Windows.Forms.TextBoxBase.OnMouseUp(MouseEventArgs mevent)
           在 DevComponents.DotNetBar.Controls.TextBoxX.OnMouseUp(MouseEventArgs e)
           在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
           在 System.Windows.Forms.Control.WndProc(Message& m)
           在 System.Windows.Forms.TextBoxBase.WndProc(Message& m)
           在 System.Windows.Forms.TextBox.WndProc(Message& m)
           在 DevComponents.DotNetBar.Controls.TextBoxX.WndProc(Message& m)
           在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
           在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
           在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
           在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
           在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
           在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
           在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
           在 System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
           在 System.Windows.Forms.Form.ShowDialog()
           在 WindowsFormsApplication1.Form1.buttonX1_Click(Object sender, EventArgs e) 位置 D:\a7373773\Test项目\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs:行号 22
           在 System.Windows.Forms.Control.OnClick(EventArgs e)
           在 DevComponents.DotNetBar.ButtonX.OnClick(EventArgs e)
           在 DevComponents.DotNetBar.ButtonX.OnMouseUp(MouseEventArgs e)
           在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
           在 System.Windows.Forms.Control.WndProc(Message& m)
           在 DevComponents.DotNetBar.PopupItemControl.WndProc(Message& m)
           在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
           在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
           在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
           在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
           在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
           在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
           在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
           在 System.Windows.Forms.Application.Run(Form mainForm)
           在 WindowsFormsApplication1.Program.Main() 位置 D:\a7373773\Test项目\WindowsFormsApplication1\WindowsFormsApplication1\Program.cs:行号 18
           在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
           在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           在 System.Threading.ThreadHelper.ThreadStart()
      InnerException: 
      

  3.   

    无法访问已释放的对象。\r\n对象名:“TextBoxX"
    说明你访问对象是,其已释放想访问的对象,来一个全局变量引用它
      

  4.   

    问题出在这里:this.Close(); 以Show方式显示的Form,调用自身的Close方法之后,迅速进入销毁的过程。
    如果无意中写了其他处理的代码,那么就很容易出现这个问题。
      

  5.   

    原因就出在
    form3            Form2 form = (Form2)this.Owner;
                form.SetText(textBoxX1.Text);            this.Close(); //这个上面
    form1 中
                Form2 form = new Form2();
                form.ShowDialog();  //程序只是暂时堵塞在这里 form2 是不能释放的//还在显示着  你确在form3中 把form2的实例给释放掉了 
                                        // 执行这里的时候找不到form2了  被你关了
      

  6.   


    你这个解释 我也理解一样的代码,两种方式
    一种引发异常,一种却不会但是我要知道的是 释放的对象是什么时候释放的
    为什么被释放的对象又会被访问大家看这段代码 
    private void textBoxX1_ButtonCustomClick(object sender, EventArgs e) 
            { 
                Form2 form = (Form2)this.Owner; 
                form.SetText(textBoxX1.Text);             this.Close(); 
            } 这是 三个小点的按钮事件我直接Close(); 就释放的对象了 
    可是我压根儿就没回调任何释放的对象何来访问的对象已释放
      

  7.   


    这个例子的代码量很少 很直观yes 按钮也写了  this.Close();
    为什么就不引发异常
      

  8.   

    可是我没有关闭 form2 我只是关闭了form3 ,因为form2是一个等待form2 接受数据后显示数据的
    我在form3 接受完数据后 关闭它。回到form2很理想的方式。。你说的好像有道理,可是为什么yes按钮里也是执行同样的代码
     Form2 form = (Form2)this.Owner;
                 form.SetText(textBoxX1.Text);             this.Close(); //这个上面却不引发异常。。但是三个小点的按钮 也是一样的代码
     Form2 form = (Form2)this.Owner;
                 form.SetText(textBoxX1.Text);             this.Close(); //这个上面却引发异常
      

  9.   

    最让人郁闷的是,设置了断点进行调试,根本无法跟踪
    那我就加try catch    private void textBoxX1_ButtonCustomClick(object sender, EventArgs e)
            {
                try
                {
                    Form2 form = (Form2)this.Owner;
                    form.SetText(textBoxX1.Text);                this.Close();
                }
                catch(Exception ee)
                {
                    MessageBoxEx.Show(ee.Message);
                }
                
            }结果出人意外,try catch 肯定获取不到引发的异常
    我想异常不是在这段代码引发的。。
      

  10.   

    这里发不了图  可以看这里http://www.cnblogs.com/a7373773/archive/2009/09/11/1564608.html
      

  11.   

    改一下代码, 2种办法================================
    1:form2:form3 form;
    ....
    ...Click(..)
    {
      if(form == null) form = new form3();
      form3.show(this);
    }
    ...form3中this.close() 改为: this.hide();===================================
    2:或Form2改为:
    Form3 form = new Form3();
    form.ShowDialog(this);
    ===============
    怀疑是form2是dialog, 且form3非dialog而其父窗体又为form2造成的问题, 。
      

  12.   

    谢谢大家谢谢大家我把 private void textBoxX1_ButtonCustomClick(object sender, EventArgs e) 
    里的Close() 改成
    this.Dispose(false);
    居然成功解决问题 哈哈
      

  13.   

    namespace WindowsFormsApplication1
    {
        public partial class Form2 : Office2007Form
        {
            public Form2()
            {
                InitializeComponent();
            }        private void lblFindEmp_MarkupLinkClick(object sender, MarkupLinkClickEventArgs e)
            {
                Form3 form = new Form3();
                form.ShowDialog(this);
            }        public void SetText(string text)
            {
                textBoxX1.Text = text;
            }        private void lblFindEmp_Click(object sender, EventArgs e)
            {        }
        }
    }
    只要把form.Show(this);改为form.ShowDialog(this);就没有问题了,已经测试过了
      

  14.   

    那代码是已经改过来的,你看看你的,只是show
      

  15.   


    非常谢谢,可惜我不想要ShowDialog(this) 因为form3 一个小小的气球窗体 悬浮着
    我还能操作form2的其他控件。。