DockPanel Suiteの使い方 その3

DockingMDIとDockingWindowの違いは未だに分からないままですが、ひとまずDockingWindowで進めていこうと思います。

メニュー部には、ToolStripPanelを使用しました。
これは、ToolStripContainerの単体版のようなものらしいです。
標準ではツールボックスに表示されていませんので、ツールボックスで右クリック>”アイテムの選択”から追加します。
ToolStripPanelを使用すると、MenuStripとToolStripを一まとめにして扱うことが可能になります。


次のようなコードで、DockContentフォームを生成することができます。
DockPanelを配置した初期状態では、どのようなDockContentフォームも好きなところに配置できます。
(DockContentFormには、AzukiControlをDock = Fillで配置してあるだけです。)

using WeifenLuo.WinFormsUI.Docking;
...

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DockContentForm frm = new DockContentForm();
            frm.Text = "Test";
            frm.Show(this.dockPanel1, DockState.Float);  //Float状態で表示
        }


しかし、これだけではプログラムを終了するときに、例外が発生してしまうようです。

System.NullReferenceException はハンドルされませんでした。
Message="オブジェクト参照がオブジェクト インスタンスに設定されていません。"
Source="Azuki"

どうやら、メインフォームのDispose()にて、AzukiコンポーネントがNULL参照例外に陥っているようです。
フォームのClosingイベントにて、次のような、DockWindowを破棄するコードを追加すると、とりあえずこの現象は起きなくなりました。

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            foreach ( DockWindow x in this.dockPanel1.DockWindows )
            {
                //持っているDockWindow以下全てを問答無用で捨てる
                x.Dispose();
            }

            //Throw ArgumentOutOfRangeException
            //this.dockPanel1.Dispose();

            //Throw System.InvalidOperationException
            //メインフォームを閉じた時、DockContentFormが一つだけ閉じられたように見え、例外発生
            //foreach ( DockContent x in this.dockPanel1.Contents )
            //{
            //    x.Dispose();
            //}
        }


Azukiの問題か、DockPanelの問題か、自分自身の理解不足が問題かは分かりません。
でも、何となく自分のコーディングミスな気がします。
恐らく、DockContentFormを使いまわしたことが原因で、DockPanel+Azukiが捨て切れなかったのかな?と。
(もしかして、DockingWindowにしている関係?とも思ったりします。)

念のため、この時のStackTraceも残しておきます。

StackTrace:
場所 Sgry.Azuki.Windows.AzukiControl.Control_Resized(Object sender, EventArgs e)
場所 System.Windows.Forms.Control.OnResize(EventArgs e)
場所 System.Windows.Forms.Control.OnSizeChanged(EventArgs e)
場所 System.Windows.Forms.Control.UpdateBounds(Int32 x, Int32 y, Int32 width, Int32 height, Int32 clientWidth, Int32 clientHeight)
場所 System.Windows.Forms.Control.UpdateBounds(Int32 x, Int32 y, Int32 width, Int32 height)
場所 System.Windows.Forms.Control.SetBoundsCore(Int32 x, Int32 y, Int32 width, Int32 height, BoundsSpecified specified)
場所 System.Windows.Forms.Control.System.Windows.Forms.Layout.IArrangedElement.SetBounds(Rectangle bounds, BoundsSpecified specified)
場所 System.Windows.Forms.Layout.DefaultLayout.xLayoutDockedControl(IArrangedElement element, Rectangle newElementBounds, Boolean measureOnly, Size& preferredSize, Rectangle& remainingBounds)
場所 System.Windows.Forms.Layout.DefaultLayout.LayoutDockedControls(IArrangedElement container, Boolean measureOnly)
場所 System.Windows.Forms.Layout.DefaultLayout.xLayout(IArrangedElement container, Boolean measureOnly, Size& preferredSize)
場所 System.Windows.Forms.Layout.DefaultLayout.LayoutCore(IArrangedElement container, LayoutEventArgs args)
場所 System.Windows.Forms.Layout.LayoutEngine.Layout(Object container, LayoutEventArgs layoutEventArgs)
場所 System.Windows.Forms.Control.OnLayout(LayoutEventArgs levent)
場所 System.Windows.Forms.ScrollableControl.OnLayout(LayoutEventArgs levent)
場所 System.Windows.Forms.Form.OnLayout(LayoutEventArgs levent)
場所 System.Windows.Forms.Control.PerformLayout(LayoutEventArgs args)
場所 System.Windows.Forms.Control.System.Windows.Forms.Layout.IArrangedElement.PerformLayout(IArrangedElement affectedElement, String affectedProperty)
場所 System.Windows.Forms.Layout.LayoutTransaction.DoLayout(IArrangedElement elementToLayout, IArrangedElement elementCausingLayout, String property)
場所 System.Windows.Forms.Control.OnResize(EventArgs e)
場所 System.Windows.Forms.Form.OnResize(EventArgs e)
場所 System.Windows.Forms.Control.OnSizeChanged(EventArgs e)
場所 System.Windows.Forms.Control.UpdateBounds(Int32 x, Int32 y, Int32 width, Int32 height, Int32 clientWidth, Int32 clientHeight)
場所 System.Windows.Forms.Control.UpdateBounds(Int32 x, Int32 y, Int32 width, Int32 height)
場所 System.Windows.Forms.Control.SetBoundsCore(Int32 x, Int32 y, Int32 width, Int32 height, BoundsSpecified specified)
場所 System.Windows.Forms.Form.SetBoundsCore(Int32 x, Int32 y, Int32 width, Int32 height, BoundsSpecified specified)
場所 System.Windows.Forms.Control.SetBounds(Int32 x, Int32 y, Int32 width, Int32 height, BoundsSpecified specified)
場所 System.Windows.Forms.Control.set_Bounds(Rectangle value)
場所 WeifenLuo.WinFormsUI.Docking.DockPane.SetContentBounds()
場所 WeifenLuo.WinFormsUI.Docking.DockPane.OnLayout(LayoutEventArgs levent)
場所 System.Windows.Forms.Control.PerformLayout(LayoutEventArgs args)
場所 System.Windows.Forms.Control.System.Windows.Forms.Layout.IArrangedElement.PerformLayout(IArrangedElement affectedElement, String affectedProperty)
場所 System.Windows.Forms.Layout.LayoutTransaction.DoLayout(IArrangedElement elementToLayout, IArrangedElement elementCausingLayout, String property)
場所 System.Windows.Forms.Control.OnResize(EventArgs e)
場所 System.Windows.Forms.UserControl.OnResize(EventArgs e)
場所 System.Windows.Forms.Control.OnSizeChanged(EventArgs e)
場所 System.Windows.Forms.Control.UpdateBounds(Int32 x, Int32 y, Int32 width, Int32 height, Int32 clientWidth, Int32 clientHeight)
場所 System.Windows.Forms.Control.UpdateBounds(Int32 x, Int32 y, Int32 width, Int32 height)
場所 System.Windows.Forms.Control.SetBoundsCore(Int32 x, Int32 y, Int32 width, Int32 height, BoundsSpecified specified)
場所 System.Windows.Forms.Control.SetBounds(Int32 x, Int32 y, Int32 width, Int32 height, BoundsSpecified specified)
場所 System.Windows.Forms.Control.set_Bounds(Rectangle value)
場所 WeifenLuo.WinFormsUI.Docking.VisibleNestedPaneCollection.Refresh()
場所 WeifenLuo.WinFormsUI.Docking.DockWindow.OnLayout(LayoutEventArgs levent)
場所 System.Windows.Forms.Control.PerformLayout(LayoutEventArgs args)
場所 System.Windows.Forms.Control.System.Windows.Forms.Layout.IArrangedElement.PerformLayout(IArrangedElement affectedElement, String affectedProperty)
場所 System.Windows.Forms.Layout.LayoutTransaction.DoLayout(IArrangedElement elementToLayout, IArrangedElement elementCausingLayout, String property)
場所 System.Windows.Forms.Control.OnResize(EventArgs e)
場所 System.Windows.Forms.Panel.OnResize(EventArgs eventargs)
場所 System.Windows.Forms.Control.OnSizeChanged(EventArgs e)
場所 System.Windows.Forms.Control.UpdateBounds(Int32 x, Int32 y, Int32 width, Int32 height, Int32 clientWidth, Int32 clientHeight)
場所 System.Windows.Forms.Control.UpdateBounds(Int32 x, Int32 y, Int32 width, Int32 height)
場所 System.Windows.Forms.Control.SetBoundsCore(Int32 x, Int32 y, Int32 width, Int32 height, BoundsSpecified specified)
場所 System.Windows.Forms.Control.System.Windows.Forms.Layout.IArrangedElement.SetBounds(Rectangle bounds, BoundsSpecified specified)
場所 System.Windows.Forms.Layout.DefaultLayout.xLayoutDockedControl(IArrangedElement element, Rectangle newElementBounds, Boolean measureOnly, Size& preferredSize, Rectangle& remainingBounds)
場所 System.Windows.Forms.Layout.DefaultLayout.LayoutDockedControls(IArrangedElement container, Boolean measureOnly)
場所 System.Windows.Forms.Layout.DefaultLayout.xLayout(IArrangedElement container, Boolean measureOnly, Size& preferredSize)
場所 System.Windows.Forms.Layout.DefaultLayout.LayoutCore(IArrangedElement container, LayoutEventArgs args)
場所 System.Windows.Forms.Layout.LayoutEngine.Layout(Object container, LayoutEventArgs layoutEventArgs)
場所 System.Windows.Forms.Control.OnLayout(LayoutEventArgs levent)
場所 System.Windows.Forms.ScrollableControl.OnLayout(LayoutEventArgs levent)
場所 WeifenLuo.WinFormsUI.Docking.DockPanel.OnLayout(LayoutEventArgs levent)
場所 System.Windows.Forms.Control.PerformLayout(LayoutEventArgs args)
場所 System.Windows.Forms.Control.PerformLayout()
場所 System.Windows.Forms.Control.ResumeLayout(Boolean performLayout)
場所 System.Windows.Forms.Layout.LayoutTransaction.Dispose()
場所 System.Windows.Forms.Control.SetVisibleCore(Boolean value)
場所 System.Windows.Forms.Control.set_Visible(Boolean value)
場所 WeifenLuo.WinFormsUI.Docking.DockWindow.OnLayout(LayoutEventArgs levent)
場所 System.Windows.Forms.Control.PerformLayout(LayoutEventArgs args)
場所 System.Windows.Forms.Control.System.Windows.Forms.Layout.IArrangedElement.PerformLayout(IArrangedElement affectedElement, String affectedProperty)
場所 System.Windows.Forms.Layout.LayoutTransaction.DoLayout(IArrangedElement elementToLayout, IArrangedElement elementCausingLayout, String property)
場所 System.Windows.Forms.Control.ControlCollection.Remove(Control value)
場所 System.Windows.Forms.Control.Dispose(Boolean disposing)
場所 System.ComponentModel.Component.Dispose()
場所 WeifenLuo.WinFormsUI.Docking.DockPane.Dispose(Boolean disposing)
場所 System.ComponentModel.Component.Dispose()
場所 WeifenLuo.WinFormsUI.Docking.DockPane.Close()
場所 WeifenLuo.WinFormsUI.Docking.DockPaneCollection.Dispose()
場所 WeifenLuo.WinFormsUI.Docking.DockPanel.Dispose(Boolean disposing)
場所 System.ComponentModel.Component.Dispose()
場所 System.Windows.Forms.Control.Dispose(Boolean disposing)
場所 System.Windows.Forms.ContainerControl.Dispose(Boolean disposing)
場所 System.Windows.Forms.Form.Dispose(Boolean disposing)
場所 SourceAnalyzer.MainForm.Dispose(Boolean disposing)
場所 System.ComponentModel.Component.Dispose()
場所 System.Windows.Forms.Form.WmClose(Message& m)
場所 System.Windows.Forms.Form.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.DefFrameProc(IntPtr hWnd, IntPtr hWndClient, Int32 msg, IntPtr wParam, IntPtr lParam)
場所 System.Windows.Forms.Form.DefWndProc(Message& m)
場所 System.Windows.Forms.Control.WndProc(Message& m)
場所 System.Windows.Forms.ScrollableControl.WndProc(Message& m)
場所 System.Windows.Forms.ContainerControl.WndProc(Message& m)
場所 System.Windows.Forms.Form.WmSysCommand(Message& m)
場所 System.Windows.Forms.Form.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.DefFrameProc(IntPtr hWnd, IntPtr hWndClient, Int32 msg, IntPtr wParam, IntPtr lParam)
場所 System.Windows.Forms.Form.DefWndProc(Message& m)
場所 System.Windows.Forms.Control.WndProc(Message& m)
場所 System.Windows.Forms.ScrollableControl.WndProc(Message& m)
場所 System.Windows.Forms.ContainerControl.WndProc(Message& m)
場所 System.Windows.Forms.Form.WmNcButtonDown(Message& m)
場所 System.Windows.Forms.Form.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)
場所 SourceAnalyzer.Program.Main()
場所 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()