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.Fo开发者_JS百科rms.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?
the way i usually do it if i am only supposed to have one open is something like:
//class member for the only formWeblead
frmWebLeads formWebLead = null;
private void leadsToolStripMenuItem_Click(object sender, EventArgs e)
{
if (formWebLead == null)
{
formWeblead = new frmWebLeads();
formWeblead.MdiParent = this;
}
formWeblead.WindowState = System.Windows.Forms.FormWindowState.Maximized;
formWeblead.Show();
}
Set this in your form main() function
InitializeComponent();
this.WindowState = FormWindowState.Maximized;
this.ShowInTaskbar = true;
from_login login = new from_login();
login.MdiParent=this;
login.Show();
pmsnrr.pmsmain = this;
and this is the code the goes inside your menu strip slick event
if (this.ActiveMdiChild != null)
this.ActiveMdiChild.Close();
frm_companymaster company = new frm_companymaster();
company.MdiParent = this;
company.WindowState = FormWindowState.Normal;
company.Show();
I personally prefer a generic implementation:
private void ShowOrActivateForm<T>() where T : Form
{
var k = MdiChildren.Where(c => c.GetType() == typeof(T)).FirstOrDefault();
if (k == null)
{
k = (Form)Activator.CreateInstance(typeof(T));
k.MdiParent = this;
k.Show();
}
else
{
k.Activate();
}
}
Then you can just use it like this:
ShowOrActivateForm<myForm>();
Where myForm is the TYPE of the form you want to create
private void leadsToolStripMenuItem_Click(object sender, EventArgs e)
{
formWeblead formWeblead = null;
if ((formWeblead = IsFormAlreadyOpen(typeof(frmWebLeads)) == null)
{
formWeblead = new frmWebLeads();
formWeblead.MdiParent = this;
formWeblead.WindowState = System.Windows.Forms.FormWindowState.Maximized;
formWeblead.Show();
}
}
public static Form IsFormAlreadyOpen(Type FormType)
{
foreach (Form OpenForm in Application.OpenForms)
{
if (OpenForm.GetType() == FormType)
return OpenForm;
}
return null;
}
frmWebLeads formWeblead;
bool isformWebleadOpen =false;
private void leadsToolStripMenuItem_Click(object sender, EventArgs e)
{
if(isformWebleadOpen == false)
{
formWeblead = new frmWebLeads();
isformWebleadOpen =true;
formWeblead.Closed += formWeblead_Closed;
formWeblead.Show();
}
}
void formWeblead_Closed(object sender, EventArgs e)
{
isformWebleadOpen = false;
}
Fists time when you open the form from menu, the variable frmRep is null
frmReportes frmRep = 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()
private void rToolStripMenuItem_Click(object sender, EventArgs e)
{
if (frmRep != null)
{
if (frmRep.Visible == false)
{
frmRep = new frmReportes();
frmRep.MdiParent = this; frmRep.Show();
}
else
{
frmRep.Activate();
return;
}
}
else
{
frmRep = new frmReportes();
frmRep.MdiParent = this;
frmRep.Show();
}
}
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:
class ParentForm : Form {
frmWebLeads formWeblead = null;
//...
private void leadsToolStripMenuItem_Click(object sender, EventArgs e)
{
if(formWeblead != null) return;
formWeblead = new frmWebLeads();
formWeblead.MdiParent = this;
formWeblead.WindowState = System.Windows.Forms.FormWindowState.Maximized;
formWeblead.Show();
}
}
You also need code to set formWeblead to null when you close it, but I'm sure you can figure that part out :)
// Implementation of the below class in your MDI Parent
private void openToolStripMenuItem_Click(object sender, EventArgs e) {
if (SingletonForm.Exists) {
return;
} else {
m_openToolStripMenuItem.Enabled = false;
SingletonForm form = new SingletonForm();
form.FormClosed += new FormClosedEventHandler(
delegate(object _sender, FormClosedEventArgs _e) {
m_openToolStripMenuItem.Enabled = true;
});
form.MdiParent = this;
form.Show();
}
}
// SingletonForm Class
using ...
using System.Threading;
namespace SingletonForm {
public partial class SingletonForm : Form, IDisposable {
static private readonly string m_mutexName = "SingletonForm.SingletonForm";
private Mutex m_mutex;
private bool m_disposed;
public SingletonForm() {
m_disposed = false;
// Check to see if there is already a running instance...
bool owned;
m_mutex = new Mutex(true, m_mutexName, out owned);
if (!owned) {
// Already running, get out...
Close();
return;
}
InitializeComponent();
}
~SingletonForm() {
Dispose(false);
}
static public bool Exists {
get {
bool owned;
using (new Mutex(false, m_mutexName, out owned)) {
return !owned;
}
}
}
// IDisposable Members
// --------------------------------------------------------------------------
#region IDisposable Members
new public void Dispose() {
Dispose(true);
// Use SupressFinalize in case a subclass of this type implements a finalizer.
GC.SuppressFinalize(this);
}
#endregion // IDisposable Members
/// <summary>
/// Note: Comment out the Dispose(bool disposing) implementation in your
/// SingletonForm.Designer.cs
/// </summary>
/// <param name="disposing">true if we are disposing.</param>
protected override void Dispose(bool disposing) {
if (disposing && (components != null)) {
components.Dispose();
}
base.Dispose(disposing);
// If you need thread safety, use a lock around these
// operations, as well as in your methods that use the resource.
if (!m_disposed) {
if (disposing) {
// Code to dispose the managed resources held by the class here...
if (m_mutex != null) {
m_mutex.Dispose();
m_mutex = null;
}
}
// Indicate that the instance has been disposed.
m_disposed = true;
}
}
};
};
Would have been nice to use semaphores instead in order to allow 1-n instances to exist.
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.
Form1 frm1 = new Form1();
CheckActiveChildForm(frm1,"myForm");
//myForm is the Text of Form1
private void CheckActiveChildForm(Form FormControl, string FormExists)
{
int h = 0;
if (MdiChildren.Count() == 0)
{
//Form2 childF = new Form2();
FormControl.MdiParent = this;
FormControl.Show();
}
if (MdiChildren.Count() > 0)
{
for (int i = 0; i < MdiChildren.Count(); i++)
{
if (MdiChildren.ElementAt(i).Text == FormExists)
{
h = 1;
}
}
}
if (h == 0)
{
FormControl.MdiParent = this;
FormControl.Show();
}
}
You could check current open forms to achieve that:
if (Application.OpenForms.Count == 1) {
ReportProductDetails Report9 = new ReportProductDetails();
Report9.MdiParent = this;
Report9.Show();
Report9.Activate();
}
else {
MessageBox.Show("Sorry!Close the All the Exist Form,Before open this Form", "Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
I use this solution, with a little trick
frmWebLeads formWebLead = null;
private void leadsToolStripMenuItem_Click(object sender, EventArgs e)
{
if (formWebLead == null || formWebLead.isAccessible == false)
{
formWeblead = new frmWebLeads();
formWeblead.MdiParent = this;
}
formWeblead.WindowState = System.Windows.Forms.FormWindowState.Maximized;
formWeblead.Show();
}
private void mnuMyForm_Click(object sender, EventArgs e) // click to open MyForm
{
foreach (Form item in this.MdiChildren) // check all opened forms
{
if (item.Name == "MyFormName") // check by form name if it's opened
{
item.BringToFront(); // bring it front
return; //exit
}
}
// if MyForm is not opened
// you know what it is
MyForm frm = new MyForm();
frm.MdiParent = this;
frm.Show();
}
The code which prevents the same child form in an MDI form
private void materialToolStripMenuItem_Click(object sender, EventArgs e)
{
frmMaterial newMDIChild = new frmMaterial();
bool opened = false;
foreach (Form OpenForm in Application.OpenForms)
{
if (OpenForm.GetType() == typeof(frmMaterial))
{
OpenForm.Activate();//to bring the activated form to the front
opened = true;
}
}
if (opened==false)
{
// Set the Parent Form of the Child window.
newMDIChild.MdiParent = this;
//newMDIChild.WindowState = System.Windows.Forms.FormWindowState.Maximized;
// Display the new form.
newMDIChild.Show();
}
}
The simplest ways of preventing multiple instance of child:
private void Showforms(Form frm)
{
if (this.ActiveMdiChild==null)
{
frm.MdiParent = this;
frm.Show();
}
}
Then call it like this:
Form1 frm = new Form1();
Showforms(frm);
the simpleist ways of preventing multiple instance of child
private void Showforms(Form frm)
{
if (this.ActiveMdiChild==null)
{
frm.MdiParent = this;
frm.Show();
}
}
精彩评论