Req help in c# form

yo01

Broken In
I am having 12 form in my application in which one is the parent and other 14 are the child.

My query is.

When the user tries to open a child form from strip menu then a msgbox must be display asking the user to close the existing opened child form.
 

RCuber

The Mighty Unkel!!!
Staff member
You are using windows forms right? can you give me a sample code on how you are creating the child windows?
 
OP
Y

yo01

Broken In
i m just calling the form in parent by creating its object.
the code that i m using is,

private void salaryDetailToolStripMenuItem_Click(object sender, EventArgs e)
{
saldetail sd = new saldetail();
sd.MdiParent = this;
sd.Show();
}

where, saldetail is my child form.

i had created the child windows in the form application in visual studio by placing the controls in the form.
 

RCuber

The Mighty Unkel!!!
Staff member
ok do the following.

create a global Form variable

Code:
Form CurrentForm;

when you are create/opening new forms the menu do this
Code:
private void salaryDetailToolStripMenuItem_Click(object sender, EventArgs e)
{
saldetail sd = new saldetail();

[B]if(CurrentForm!=null)
{

MessageBox.show("Please close the other window");
//CurrentForm.close(); //uncomment this line if you want to close the other window.
CurrentForm=sd;
}
else
{
CurrentForm=sd;
}
[/B]
sd.MdiParent = this;
sd.Show();
}

use this logic for all the forms. There is a better way of doing this, but I cannot recall the methods used for it.

EDIT: oops , I saw that you require a message box, edited the source for displaying messagebox.
 
Last edited:

RCuber

The Mighty Unkel!!!
Staff member
Thanks for the help.
This does what i need.

you are welcome :)

EDIT: BTW, if you are using the message box, then you need to use a return statement after the message box, else the program will continue to execute.
 
Top Bottom