初学C#,请问如何在一个窗体用ToolStripMenuItem_Click控制另一个子窗体的打开关闭,并且焦点不变。

tianyuansky 2009-03-03 09:05:05
目前已经实现了通过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里面那些图层,信息之类的子窗体的控制一样。
恳请各位大哥帮帮忙
...全文
2685 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
buns.li 2011-07-15
  • 打赏
  • 举报
回复
把第二个窗口做为一个类 然后去定义一个类的对象 在来用show()
buns.li 2011-07-15
  • 打赏
  • 举报
回复
把第二个控件作为一个类就行了
程序小海 2009-11-01
  • 打赏
  • 举报
回复
同情中 ……
Aaron-Android 2009-10-21
  • 打赏
  • 举报
回复
你的代码乱七八糟,根本的用法不对。你定义的打开预览窗口() 方法是将FROM2显示啊,怎么用THIS代表当前窗体,总体来说你的代码就是。this.from2.show()
这肯定不对了,
定义form2的父窗体是当前窗体
form2 frm2=new form2();//实例化form2
frm2.mdiparent=this;//form2的父窗体是当前窗体
frm2.show();//显示子窗体form2时的语句

tianyuansky 2009-03-05
  • 打赏
  • 举报
回复
等待回答。。。
indeep 2009-03-04
  • 打赏
  • 举报
回复
不知道 请教高手
tianyuansky 2009-03-04
  • 打赏
  • 举报
回复
看来真是没有人知道了?
haixia0912 2009-03-03
  • 打赏
  • 举报
回复
我也很关心这个问题,而且对于如何实现对照片的大小调整如何实现?
tianyuansky 2009-03-03
  • 打赏
  • 举报
回复
大哥们~~帮帮忙啊~~~这里好火啊,刚问的问题就沉下去了,难道这个没有人知道吗?
tianyuansky 2009-03-03
  • 打赏
  • 举报
回复
没有人知道吗?顶起来,等高手

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧