问题描述
我用一下代码将窗体最小化到状态栏,但是我现在每点一次打开窗口的菜单,程序就又重新打开一次原先的窗体,我想实现当第二次打开窗体时只要激活第一次打开的窗体,应该如何实现?//先建一个FormBase的基类,子窗体都从这个基类继承publicclassFormBase:System.Windows.Forms.Form{publicdelegatevoidFormMinSize(objectsender,EventArgse);publiceventFormMinSizeOnFormMinSize;publicFormBase(){}//重载,用来处理自己定义的事件protectedoverridevoidOnSizeChanged(EventArgse){base.OnSizeChanged(e);if(this.OnFormMinSize!=null){this.OnFormMinSize(this,e);}}}//下面是父窗体的类publicpartialclassFormParent:Form{publicFormParent(){InitializeComponent();}//这里是show子窗体的按钮,用来测试,Form3是从FormBase继承的privatevoidform3ToolStripMenuItem_Click(objectsender,EventArgse){Form3form=newForm3();form.MdiParent=this;//添加事件form.OnFormMinSize+=newFormBase.FormMinSize(form_OnFormMinSize);form.Show();}//这里是主要的处理部分privatevoidform_OnFormMinSize(objectsender,EventArgse){//获取子窗体FormchildForm=(Form)sender;//最小化的时候,状态栏添加一个控件//加上childForm.Visible==true条件为了防止被添加两次,具体原因测试一下就知道了if(childForm.WindowState==FormWindowState.Minimized&&childForm.Visible==true){childForm.Hide();//状态栏添加一个控件ToolStripStatusLabellabel=newToolStripStatusLabel();label.Text=childForm.Text;label.BorderSides=ToolStripStatusLabelBorderSides.All;label.BorderStyle=Border3DStyle.RaisedInner;this.statusStrip1.Items.Add(label);}}}
解决方案
解决方案二:
foreach(FormFmOpeninFormParent.MdiChildren){if(FmOpen.Name==childForm.Name){FmOpen.Activate();FmOpen.WindowState=FormWindowState.Maximized;return;}}
解决方案三:
不行的,我的窗体已最小化到状态栏了,