Click here to Skip to main content
15,905,238 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hello,

I want to create a function that accept different form types. Main purpose is to prevent openning multiple forms (same forms) when user open from MdiParent. Instead, I would like to open if it was not opened yet. Else, bring the form alive.
Currently what I have done is:
C#
private FrmUser l_FrmUser;
private FrmRolePermission l_FrmRolePermission;
private void usersToolStripMenuItem_Click(object sender, EventArgs e)
{
    if (l_FrmUser == null || l_FrmUser.IsDisposed == true)
    {
        l_FrmUser = new FrmUser();
    }
    l_FrmUser.MdiParent = this;
    l_FrmUser.Show();
    if (l_FrmUser.WindowState == FormWindowState.Minimized)
        l_FrmUser.WindowState = FormWindowState.Normal;
}
private void rolePermissionToolStripMenuItem1_Click(object sender, EventArgs e)
{
    if (l_FrmRolePermission == null || l_FrmRolePermission.IsDisposed == true)
    {
        l_FrmRolePermission = new FrmRolePermission();
    }
    l_FrmRolePermission.MdiParent = this;
    l_FrmRolePermission.Show();
    if (l_FrmRolePermission.WindowState == FormWindowState.Minimized)
        l_FrmRolePermission.WindowState = FormWindowState.Normal;
}

This is just an example with two forms. I have other forms too and as you can see there are lots of duplicate codes. For this, I would like to create a common function like:

C#
private FrmUser l_FrmUser;
private FrmRolePermission l_FrmRolePermission;
private void rolePermissionToolStripMenuItem1_Click(object sender, EventArgs e)
{
    OpenForm(l_FrmRolePermission);
}
private void usersToolStripMenuItem_Click(object sender, EventArgs e)
{
    OpenForm(l_FrmUser);
}

private void OpenForm(Form aForm)
{
    if (aForm == null || aForm.IsDisposed == true)
    {
        aForm = new Form(); //<-- Problem here! How to initialize to desired form?
    }
    aForm.MdiParent = this;
    aForm.Show();

    if (aForm.WindowState == FormWindowState.Minimized)
        aForm.WindowState = FormWindowState.Normal;
}


Problem is described with the comment inside OpenForm(..) function. I don't know how to convert Form class to FrmUser or FrmRolePermission.

Or is there any other elegant methods to serve this purpose?

Please advice.

Thanks in advanced,
tslin
Posted
Comments
BillWoodruff 28-Nov-14 21:02pm    
Are you satisfied with the code you have now ? Would you like to see a different way of handling this, if so, just ask, and I'll post a code-example.
tslin89 11-Dec-14 20:29pm    
Yes, I would like to see different method.
Thanks

Before I respond to your question with code, I'd like to ask you if you are aware of, or have used, the special 'MdiWindowListItem property of the MenuStrip that causes the Mdi App to automatically keep track of all open Mdi Child-Forms, and brings-to-front whichever Child-Form is mapped to the sub-item you clicked on ?

If you are not familiar with this, see: [^].

That may not have the functionality you want; it may/may-not be easily modified to have the functionality you want.

It would be helpful if you described what you want in contrast with what that "automatic" window management facility provides.

Other information I think is essential to know:

1. will you ever create more than one instance of any of your types of Child-Forms.

2. once an instance of one type of Child-Form is created, do you allow the user to 'Close it, rather than just 'Hide it ?

3. do you always create at least one instance of each type of Child-Form ?

This "jumps out at me" in your code (second example):

1. in 'OpenForm you are setting the MdiParent property of the instance of the Child-Form passed as a parameter whether or not the parameter is 'null or is an existing instance: if it's an existing instance, then you don't need to set its 'MdiParent property again, and you can just make it visible using aForm.Visible = true;
 
Share this answer
 
Comments
tslin89 28-Nov-14 1:39am    
Thanks for the reply. I am not using "MdiWindowListItem" and I am not familiar with it. I will check it out.

1. I won't allow to create another instance of the child form as I am preventing to open the same form multiple times.

2. Once an instance for Child-Form is created, I don't allow them to hide. They can only close the form.

3. Always create? I will create one if they click on the menu item. Otherwise (i.e. user don't open the menu), instance will not be created for those forms.

4. Ok. This is my modified function without setting MdiParent property unnecessarily.
private void OpenForm(Form aForm)
{
if (aForm == null || aForm.IsDisposed == true)
{
aForm = new Form(); //<-- Problem here! How to initialize to desired form?
aForm.MdiParent = this;
}
aForm.Show();

if (aForm.WindowState == FormWindowState.Minimized)
aForm.WindowState = FormWindowState.Normal;
}

The line "aForm = new Form()" is giving me the problem as it create a new Form without any controls on it.

Thanks
tslin89 28-Nov-14 1:45am    
My mistake. I just moved the solution to comment section and deleted the solution. Sorry for your comment that also deleted.
tslin89 28-Nov-14 1:49am    
Yes, user can close the form at any time.

Hide the menu item after the form was shown? Yes, one of the solution. But if I am the user, I would still want to see the menu item and clicking on it will bring the form up (if it was already opened).

Thanks
BillWoodruff 28-Nov-14 3:54am    
If you want the menu item corresponding to the Form to always remain active, and to bring the Form to the front and/or expand it if it's minimized, then setting the men item to disable is not going to work because it won't get a click.

I assume you want the MdiParentForm menu to appear with the Forms' menu showing all the different Form Types available when it is opened, correct ?
tslin89 28-Nov-14 4:12am    
What I am trying to accomplish is -
Let's say I have one parent form and one child form.
Parent Form have a menu strip with 1 menu item for child form.
step 1. I open a child form through menu item (Child form was opened) - Correct
step 2. I click the menu again (Another Child form was opened) - Wrong
step 3. I click the menu again (One more Child form was opened) - Wrong

What I want is after step 1, I don't want that Child Form from openning again by steps 2 and 3.
Are you looking for something like the following:

Public Function FnInitiliseForm(ByVal ChildForm As Form, ByVal title As String) As Form
        For Each ChildForm1 As Form In Me.MdiChildren
            If ChildForm1.Name = ChildForm.Name Then
                ChildForm1.Activate()
                ChildForm1.WindowState = FormWindowState.Normal
                ChildForm.Close()
                FnInitiliseForm = ChildForm1
                Return FnInitiliseForm
            End If
        Next

        ChildForm.FormBorderStyle = Windows.Forms.FormBorderStyle.Fixed3D
        ChildForm.StartPosition = FormStartPosition.Manual
        ChildForm.MaximizeBox = False
        ChildForm.WindowState = FormWindowState.Maximized

        ChildForm.MdiParent = Me
        ChildForm.Text = title

        ChildForm.KeyPreview = True
        ChildForm.Show()
        ChildForm.Left = 0
        ChildForm.Top = 0

        FnInitiliseForm = ChildForm
    End Function
 
Share this answer
 
Comments
tslin89 28-Nov-14 2:28am    
This works! I guess there is no choice to loop through MdiChild forms.

Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900