只允许 Menustrip 此时只有一个子项
当我单击菜单条中的菜单时,如何一次只允许一个窗口?
例如:我有 Menustrip Ordre、Tarif 等...当我第一次单击 Ordre 时,它将打开一个新表单,但第二次我想禁止它。
private void ordresToolStripMenuItem_Click(object sender, EventArgs e)
{
if (Already open)
{
}
else
{
Lordre newMDIChild = new Lordre(ClientId);
// Set the Parent Form of the Child window.
newMDIChild.MdiParent = this;
// Display the new form.
newMDIChild.Show();
}
}
提前谢谢你
How can i allow only one window at a time when i click menu in menustrip ?
Ex: i have Menustrip Ordre, Tarif, etc... when i click Ordre for the first time it will open a new form, but for the second time i want to disallow it.
private void ordresToolStripMenuItem_Click(object sender, EventArgs e)
{
if (Already open)
{
}
else
{
Lordre newMDIChild = new Lordre(ClientId);
// Set the Parent Form of the Child window.
newMDIChild.MdiParent = this;
// Display the new form.
newMDIChild.Show();
}
}
Thanks you in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您希望仅在第一次创建表单,然后在下次选择菜单项时显示相同的表单,则可以使用以下方法:
If you want the form to be created only the first time, and then show that same form the next time the menu item is selected, something like this could work:
也许是这样的:
这完全是在浏览器中编写的,我已经很长时间没有编写Windows应用程序了,所以确切的类和属性可能会有所不同。
Maybe something like this:
This was written entirely in the browser and I haven't written a windows app for a long time, so the exact classes and properties may be different.
我通常处理单实例表单的方式就是用一个成员变量来保存它,然后检查它是否为空。
因此,有一个成员变量:
private TestForm myTestForm = null;
然后当您检查时只需检查它是否为 null;如果没有,当您创建表单时,将其分配给您的成员变量,并附加到子表单关闭事件的事件处理程序。
在关闭处理程序中,只需将其设置回 null 即可。
另外,我做了一些搜索,您可以将条件更改为:
The way I usually handle single instance forms is just to have a member variable to hold it, and then check to see whether it is null.
so, have a member variable:
private TestForm myTestForm = null;
and then when you are checking just check whether it's null; if not, when you create your form assign it to your member variable and attach to the event handler for the closing event of the child form.
and in the closing handler, just set it back null.
also, I did a bit of searching and rather than having the FormClosing event and handler, you can just change your conditional to be:
感谢大家的回复。
我发现这
就是我想要的,但是代码太多了。我想知道是否可以减少它。
我必须将这个添加到我的每个脱衣舞菜单中。
Thanks you all for your response.
i have found this one
it is what i want, but it is too much code. i wonder if i can reduce it.
i have to make this to each of my stripmenu.
我这样做的方法是首先将屏幕存储在类中的变量中,如下所示:
然后为其创建一个属性,如下所示:
然后在表单的 Form Closing 事件上将值设置为 null,如下所示:
现在最后您可以设置一个按钮单击事件处理程序中的 if 语句如下:
The way I do it is by first storing the screen in a variable in a class like so:
Then make a property for it like so:
Then on the form's Form Closing event set the value to null like so:
Now Finally you can set an if statement in the button click event handler like so: