DIGIT-ARC read-only archive · snapshot: July 23, 2026

Req help in c# form

8 posts · originally from https://geek.digit.in/community/threads/req-help-in-c-form.153371/

Y
yo01 Original post #1
Feb 29, 2012 at 6:25 PM
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
RCuber #2
Feb 29, 2012 at 6:29 PM
You are using windows forms right? can you give me a sample code on how you are creating the child windows?
Y
yo01 #3
Mar 1, 2012 at 2:43 PM
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
RCuber #4
Mar 1, 2012 at 3:04 PM
one more question, can you tell me if the parent is a MDI container.
Y
yo01 #5
Mar 1, 2012 at 3:11 PM
Yes, the parent is a MDI container.
RCuber
RCuber #6
Mar 1, 2012 at 4:12 PM
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.
Y
yo01 #7
Mar 1, 2012 at 5:12 PM
Thanks for the help.
This does what i need.
RCuber
RCuber #8
Mar 1, 2012 at 5:15 PM
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.