初学C#,请问如何在一个窗体用ToolStripMenuItem_Click控制另一个子窗体的打开关闭,并且焦点不变。
目前已经实现了通过Click事件开关子窗体,但是一开就变了,而且开了之后如果用ToolStripMenuItem的checked属性来控制开关没问题,可是手动关闭了子窗体后,再点击,就会抛出异常未处理
System.ObjectDisposedException
Message="无法访问已释放的对象。\r\n对象名:“Form2”。"
Source="System.Windows.Forms"
ObjectName="Form2"
StackTrace:
在 System.Windows.Forms.Control.CreateHandle()
在 System.Windows.Forms.Form.CreateHandle()
在 System.Windows.Forms.Control.get_Handle()
在 System.Windows.Forms.Control.SetVisibleCore(Boolean value)
在 System.Windows.Forms.Form.SetVisibleCore(Boolean value)
在 System.Windows.Forms.Control.Show()
在 ActionEditer.Form1.打开预览窗口() 位置 E:\Projects\VS Projects\ActionEditer\ActionEditer\Form1.cs:行号 28
在 ActionEditer.Form1.动画ToolStripMenuItem_Click(Object sender, EventArgs e) 位置 E:\Projects\VS Projects\ActionEditer\ActionEditer\Form1.cs:行号 154
在 System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
在 System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
在 System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
在 System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
在 System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
在 System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
在 System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
在 System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.ScrollableControl.WndProc(Message& m)
在 System.Windows.Forms.ToolStrip.WndProc(Message& m)
在 System.Windows.Forms.ToolStripDropDown.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)
在 ActionEditer.Program.Main() 位置 E:\Projects\VS Projects\ActionEditer\ActionEditer\Program.cs:行号 18
在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
在 System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
在 System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
在 System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
在 System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
在 System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext)
在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()
InnerException:
-----------------------------------------------------
以下是我的代码
-----------------------------------------------------
public Form2 fm2;
public Form1()
{
InitializeComponent();
this.IsMdiContainer = true;
fm2 = new Form2();
this.打开预览窗口();
}
private void 打开预览窗口()
{
if (fm2 != null)
{
fm2.Show();
}
}
private void 关闭预览窗口()
{
fm2.Hide();
}
private void 动画ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.动画ToolStripMenuItem.Checked = !this.动画ToolStripMenuItem.Checked;
if (this.动画ToolStripMenuItem.Checked)
{
this.打开预览窗口();
}
else
{
this.关闭预览窗口();
}
}
我最终想实现的一个功能就像是PhotoShop里面那些图层,信息之类的子窗体的控制一样。
恳请各位大哥帮帮忙