如何避免 MDIParent C# Win 表单中显示多个子表单
当用户尝试打开已在 MDIParent 中打开的子表单时,我想避免我的子表单多次出现。避免这种情况的一种方法是禁用控制器(在我的例子中为 BUTTON),但我也为此功能提供了一个快捷键(Ctrl+L)。因此,如果用户键入 Ctrl+L,则会打开同一个子窗体,并且我可以看到 MDI 中存在两个子窗体。
private void leadsToolStripMenuItem_Click(object sender, EventArgs e)
{
frmWebLeads formWeblead = new frmWebLeads();
formWeblead.MdiParent = this;
formWeblead.WindowState = System.Windows.Forms.FormWindowState.Maximized;
formWeblead.Show();
}
我想避免这种情况。我该怎么做?
在图像中,您可以看到打开了一个子表单“Name Online Leads”用户第一次使用菜单(LEADS)打开两次,第二次使用快捷键打开。我不希望这种事发生。如果表单已打开,则应避免打开另一个相同的表单...如何做到这一点?
I want to avoid my child form from appearing many times when a user tries to open the child form which is already open in MDIParent. One way to avoid this is by disabling the Controller (in my case BUTTON) but I have given a shortcut key (Ctrl+L) for this function as well. So if the user types Ctrl+L, the same child Form opens and I can see two child forms are in MDI.
private void leadsToolStripMenuItem_Click(object sender, EventArgs e)
{
frmWebLeads formWeblead = new frmWebLeads();
formWeblead.MdiParent = this;
formWeblead.WindowState = System.Windows.Forms.FormWindowState.Maximized;
formWeblead.Show();
}
I want to avoid this. How can I do this?
In the image you can see that a child form Name Online Leads is opened twice as the user opened first time using Menu (LEADS) and second time by Shortcut key. I don't want this to happen. If the form is already opened it should avoid opening another same form ... How to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
如果我只应该打开一个,我通常这样做的方式是这样的:
the way i usually do it if i am only supposed to have one open is something like:
在表单 main() 函数中设置它
,这是菜单条光滑事件中的代码
Set this in your form main() function
and this is the code the goes inside your menu strip slick event
我个人更喜欢通用实现:
然后您可以像这样使用它:
其中 myForm 是您要创建的表单的类型
I personally prefer a generic implementation:
Then you can just use it like this:
Where myForm is the TYPE of the form you want to create
当您从菜单打开表单时,变量 frmRep 为 null
...所以我在第一个“if”内添加另一个“if”以验证我的表单是否可见,因为我有另一个表单,那么如果它不可见我创建一个实例并显示表单,但如果可见,我只需使用 Activate()
Fists time when you open the form from menu, the variable frmRep is null
...so I add another "if" inside the first "if" to validate my form is visible, because I have another forms, then if its not visible I make an instance and show the form, but if is visible I just use Activate()
最简单的方法是保留对子窗体的引用,并且仅在子窗体不存在时生成一个新窗体。像这样的事情:
您还需要代码在关闭 formWeblead 时将其设置为 null,但我相信您可以弄清楚该部分:)
The easiest way is to keep a reference to the child form, and only spawn a new one if it doesn't already exist. Something like this:
You also need code to set formWeblead to null when you close it, but I'm sure you can figure that part out :)
为了允许 1-n 个实例存在,最好使用信号量。
Would have been nice to use semaphores instead in order to allow 1-n instances to exist.
这是我为在单击 MDIParent 中的菜单时仅调用打开一个表单而创建的“方法”。希望这个“方法”能够帮助到您!
用法:甚至在 ToolStripMenuItems 上。
Here is the "Method" that I created for Calling open only one form when u click on Menu in MDIParent. Hope this "Method" can help you!
Usage: On Even ToolStripMenuItems.
您可以检查当前打开的表格来实现这一点:
You could check current open forms to achieve that:
我使用这个解决方案,有一个小技巧
I use this solution, with a little trick
防止 MDI 表单中出现相同子表单的代码
The code which prevents the same child form in an MDI form
防止多个子实例的最简单方法:
然后像这样调用它:
The simplest ways of preventing multiple instance of child:
Then call it like this:
防止多个子实例的最简单方法
the simpleist ways of preventing multiple instance of child