Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have windows form application in which there's only one parent form and many child forms but the child forms open and quickly close. See below code which executes on menustrip item click event

TimeTaskManager objTimeTaskManager = new TimeTaskManager();
objTimeTaskManager.Show();
objTimeTaskManager.MdiParent = this;
objTimeTaskManager.BringToFront();
objTimeTaskManager.StartPosition = FormStartPosition.CenterParent;
objTimeTaskManager.Focus();

Can someone show me how to fix this issue?

What I have tried:

I tried the below code

TimeTaskManager objTimeTaskManager = new TimeTaskManager();
objTimeTaskManager.MdiParent = this;
objTimeTaskManager.Show();
objTimeTaskManager.BringToFront();
objTimeTaskManager.StartPosition = FormStartPosition.CenterParent;
objTimeTaskManager.Focus();
Posted
Updated 4-Nov-20 8:13am

1 solution

I think you have not set the MdiParent property of your newly created Forms.

For the new child Form to be centered in its MdiParent Form, you need to show it after the Main Form has been shown, and, use CenterScreen, not CenterParent:
C#
using System;
using System.Windows.Forms;

namespace MdiTest2020
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private Form2 f2 = new Form2();

        private void ShowNewMdiChildFormToolStripMenuItem_Click(object sender, EventArgs e)
        {
            f2.MdiParent = this;
            f2.StartPosition = FormStartPosition.CenterScreen;
            f2.Show();
        }
    }
}
These weird work-arounds are necessary because the MDI WinForm architecture is an outdated mess, and MS suggests you don't use it.
 
Share this answer
 
v2
Comments
Christopher Fernandes 5-Nov-20 1:45am    
I have set the parent as IsMDIContainer true and yet it does the same thing
BillWoodruff 5-Nov-20 2:49am    
Your Main Form must have IsMdiContainer = true; each child Form must have its MdiParent property set to the instance of the Mai Form. Study the working code example I posted, and figure out what you are doing differently.
Christopher Fernandes 5-Nov-20 5:05am    
I am doing everything as you have shown and yet it opens and closes quickly
BillWoodruff 5-Nov-20 15:58pm    
I wish I could help further: it is obvious something else is happening in your code that prevents your using the working example I showed you.

You need to set breakpoints before and after the 'Show command on your child forms and single step (F11) to identify what happens.
Christopher Fernandes 17-Nov-20 8:40am    
It is fixed. I dont know how. Thanks so much

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