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)
{
}
el开发者_运维百科se
{
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
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:
private Lordre orderForm = null;
private void ordresToolStripMenuItem_Click(object sender, EventArgs e)
{
if (orderForm == null)
orderForm = new Lordre(ClientId);
// Set the Parent Form of the Child window.
orderForm .MdiParent = this;
}
// Display the form.
orderForm.Show();
orderForm.Activate();
}
Maybe something like this:
public class MyForm
{
private Window _openedWindow;
private void ordresToolStripMenuItem_Click(object sender, EventArgs e)
{
if ( _openedWindow != null && _openedWindow.Open)
{
//
}
else
{
Lordre newMDIChild = new Lordre(ClientId);
_openedWindow = newMDIChild;
// Set the Parent Form of the Child window.
newMDIChild.MdiParent = this;
// Display the new form.
newMDIChild.Show();
}
}
}
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.
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.
if (myTestForm != null)
{
MessageBox.ShowDialog("Sorry, you already have a TestForm open!");
}
else
{
myTestForm = new TestForm();
myTestForm.FormClosing += myTestForm_FormClosing;
myTestForm.MdiParent = this;
myTestForm.Show();
}
and in the closing handler, just set it back null.
private void myTestForm_FormClosing(Object sender, FormClosingEventArgs e)
{
myTestForm = 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:
if ((myTestForm != null) && (!myTestForm.IsDisposed())
Thanks you all for your response.
i have found this one
private void ordresToolStripMenuItem_Click(object sender, EventArgs e)
{
// a flag to store if the child form is opened or not
bool found = false;
// get all of the MDI children in an array
Form[] charr = this.MdiChildren;
if (charr.Length == 0) // no child form is opened
{
Lordre myPatients = new Lordre();
myPatients.MdiParent = this;
// The StartPosition property is essential
// for the location property to work
myPatients.StartPosition = FormStartPosition.Manual;
myPatients.Location = new Point(0,0);
myPatients.Show();
}
else // child forms are opened
{
foreach (Form chform in charr)
{
if (chform.Tag.ToString() == "Ordre") // one instance of the form is already opened
{
chform.Activate();
found = true;
break; // exit loop
}
else
found = false; // make sure flag is set to false if the form is not found
}
if (found == false)
{
Lordre myPatients = new Lordre();
myPatients.MdiParent = this;
// The StartPosition property is essential
// for the location property to work
myPatients.StartPosition = FormStartPosition.Manual;
myPatients.Location = new Point(0,0);
myPatients.Show();
}
}
}
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.
if (chform.Tag.ToString() == "Ordre")
if (chform.Tag.ToString() == "Another one")
The way I do it is by first storing the screen in a variable in a class like so:
private Lordre _LordreChildForm = new Lordre(ClientID)
Then make a property for it like so:
public Lordre LordreChildForm { get => _LordreChildForm; set => _LordreChildForm =
value; }
Then on the form's Form Closing event set the value to null like so:
private void Lordre_FormClosing(object sender, FormClosingEventArgs e)
{
ClassName.LordreChildForm= null;
}
Now Finally you can set an if statement in the button click event handler like so:
private void LordreToolStripMenuItem_Click(object sender, EventArgs e)
{
if(ClassName.LordreChildForm== null || ClassName.LordreChildForm.IsDisposed)
{
ClassName.LordreChildForm= new DeregisterClub();
ClassName.LordreChildForm.MdiParent = this;
}
LordreChildForm.Dock = DockStyle.Fill;
LordreChildForm.Show();
LordreChildForm.Focus();
}
精彩评论