I am working on Winform application and want to open modal form in center of parent form. In Winform application there is :
MDI Form (open as startup form and act as container for all)
on click of one of the Menu item of MDI Form - opens a MDI Child form
on click of one of the button on MDI Child opened in step 2 - opens a modal form - which we need to open in center of MDI Child form (opened in step 2)
So for opening modal form in center 1st obvious solution i had done is
TestModalForm obj = new TestModalForm()
obj.StartPosition = FormStartPosition.CenterParent;
obj.showdialog(this);
but above solution didn't work as modal form always considers MDI Form as its Parent. So I workout for 2nd solution: in that i wrote method in Form Load of Modal window to position it in center as below:
private void MakeWinInCenter()
{
if (this.Owner != null)
{
Form objParent = null;
int TopbarHeight = 0;
if (this.Owner.IsMdiContainer && this.Owner.ActiveMdiChild != null)
{
objParent = this.Owner.ActiveMdiChild;
TopbarHeight = GetTopbarHeight(this.Owner);
}
else
objParent = this.Owner;
Point p = new Point((objParent.Width - this.Width) / 2, (objParent.Height - this.Height) / 2);
p.X += objParent.Location.X;
p.Y += TopbarHeight + objParent.Location.Y;
this.Location = p;
}
else
{
//If owner is Null then, we have reference of MDIForm in Startup Class - use that ref and opens win in center of MDI
if (Startup.MDIObj != null)
{
this.Left = Convert.ToInt32((Startup.MDIObj.Width - this.Width) / 2);
this.Top = Convert.ToInt32((Startup.MDIObj.Height - this.Height) / 2);
}
}
}
private int GetTopbarHeight(Form MDIForm)
{
int TopbarHeight = 0;
MdiClient objMDIClient = null;
foreach (Control ctl in MDIForm.Controls)
{
if (ctl is MdiClient)
{
objMDIClient = ctl as MdiClient;
break;
}
}
if (objMDIClient != null)
{
TopbarHeight = MDIForm.Height - objMDIClient.Size.Height;
}
return TopbarHeight;
}
Above solution works perfect when MDI form is opened in Maximized form. But when we checked by resizing the MDI form (i.e. not in maximized form) or moving MDI Form to other screen - in case of multiple screens, above solution is not working and doesn't opens the modal form in center of MDI Child form
Also looked at this Question but its not helpful to my problem.
Can anyone have any suggestions or solution to solve the problem.
Thanks
Try this:
TestModalForm.showdialog(this.MdiParent);
To show form using ShowDialog in center of its parent when the parent is MDI Child, you can use following code
The code is supposed to run from a button in a MDI Child form and show another form as modal dialog at center of the MDI Child form:
var dialog = new Form(); //The form which you want to show as dialog
//this.Parent point to the MdiClient control which is the container of this
var p = this.Parent.PointToScreen(this.Location);
p.Offset((this.Width - dialog.Width)/2 , (this.Height - dialog.Height)/2);
dialog.StartPosition = FormStartPosition.Manual;
dialog.DesktopLocation = p;
dialog.ShowDialog();
In above code, since the current form is a MDI Child, then this.Parent point to the MdiClient control which is the container of MDI Child forms, so we can use its PointToScreen method passing location of MDI child (this) to get the screen location of MDI child. Then if you offset that location using the half of difference between MDI children and dialog width and height and set the StartPosition of dialog to FormStartPosition.Manual and show it using ShowDialog.
Your method seems overly complicated. Why not just do it this way?
// All code goes in your MDI Child form
// Create the modal form
TestModalForm obj = new TestModalForm();
// Find center point of MDI Parent form
Point centerPoint = new Point(this.MdiParent.Top + this.MdiParent.Height / 2,
this.MdiParent.Left + this.MdiParent.Width / 2);
// Set the location and show the modal form
obj.StartPosition = FormStartPosition.Manual
obj.Location = centerPoint;
obj.ShowDialog(this);
I think you should do this
//for modal form set its strat position to centerparent like this
**this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;**
// this will open the modalform in center of parent form
In MDI forms center to parent doesn't work as expected see the documentation MSDN ,it is always recommended to use center to screen, but I use a work-around to this, after all it's about location positioning so I created an override method for center to parent and used it on the child form load event so that every time the child is loaded it is centered to parent:
private void CenterToParentOverride()
{
this.Location = new Point(
this.MdiParent.ClientSize.Width / 2 - this.Width / 2,
this.MdiParent.ClientSize.Height / 2 - this.Height / 2);
}
Related
i am new in c# windows application learning
i have 3 form.
main form, form menu, child form
in main form when i click on button1, form menu is show in 1st panel.
this code in main form.....this work properly for me... if anyone have better then this then suggest me.
private void button1_Click(object sender, EventArgs e)
{
bool IsOpen = false;
foreach (Form frmchild in Application.OpenForms)
{
if (frmchild.Text == "Child Form")
{
IsOpen = true;
frmchild.Close();
break;
}
}
if (IsOpen == false)
{
child form cf = new child form();
cf.Owner = this; //main form is owenr
cf.Show();
cf.Location
= new Point(button1.Left + 37, button1.Height + button1.Top + 4);
}
}
main form have 2 panel, 1st is small and dock in top. 2nd is main panel (dock = fill) where i want to dock all child form.
in form menu i have button and when i click on i want to open child form in main form 2nd panel.
please help me to open child form in main form's panel when i click button in another child form in c# language.
please help me.
i think you have to use USER CONTROL instead of a form. Add a usercontrol at your prject and here is the code :
for (int i = panel2.Controls.Count; i > 0; i--)
{
panel2.Controls[i-1].Dispose();
}
//add user control you want
UserControl1 uc = new UserControl1();
panel2.Controls.Add(uc);
I have been working on project for the past one month but I am really stucked. I have a form called MainForm which has 2 panels. One is called MainContainer and other called SubContainer. I have two other forms, one called Login and other SQLSettings. SubContainer is found inside MainContainer. SubContainer is suppose to hold all open forms. When MainForm loads at startup it opens Login form inside SubContainer. Login contains a linklabel which is also suppose to open SQLSettings form in SubContainer but nothing happens when i click on the linklabel in the Login form.
What I have tried:
this is the code for the linklabel which is suppose to open SQLSettings form:
this.Close();
MainForm f = new MainForm();
Form cur = new SQLSettings();
f.SubContainer.Dock = DockStyle.None;
f.SubContainer.Anchor = AnchorStyles.None;
f.SubContainer.Size = cur.Size;
f.SubContainer.Location = new Point(f.MainContainer.Width / 2 - f.SubContainer.Width / 2,
f.MainContainer.Height / 2 - f.SubContainer.Height / 2);
cur.TopLevel = false;
f.SubContainer.Controls.Remove(f.currentForm);
f.SubContainer.Tag = cur;
f.SubContainer.Controls.Add(cur);
f.SubContainer.Tag = cur;
cur.BringToFront();
cur.Show();
Child form can be opened inside the parent form with setting property MdiParent.
According to your MainForm as parent and SQLSettings form as child:
MainForm f = new MainForm();
Form cur = new SQLSettings();
cur.MdiParent = f;
cur.BringToFront();
cur.Show();
I prepared the form with using "SplitContainer tool".I added Treeview into left side of that SplitContainer. next i added to that treeview two Node such as hide and show and also i prepared a "child form". I need to do, Chile form SplitContainer load to right side when I click on the Node show and hide child form when click on the hide node.i can show chile form but can not hide it.please help me to do this.below i attached code which i used to "Show"
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
UserControll.UscCreateUser UscPerobjForm = new UserControll.UscCreateUser();
string Tree = (string)e.Node.Tag;
if (Tree == "1")
{
UscPerobjForm.TopLevel = false;
splitContainer1.Panel2.Controls.Add(UscPerobjForm);
UscPerobjForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
UscPerobjForm.Dock = DockStyle.Fill;
UscPerobjForm.Show();
//Show part
}
else if (Tree == "2")
{
// Hide part
}
}
I need to Hide part.
Try using http://dockpanelsuite.com/ where you can have the tree view implement in a Form class docked on Left while the Child Form dock the center.
I have a main application form, I've set the IsMdiContainer property to TRUE
I have a panel in the main application form on the top but when I open the ChildForm it opens behind the panel. How do I set location relative to the bottom of the panel + maximize the window?
As Hans said in comments, Set the Dock property of Panel to Top for example. And add your Mdi Childs this way:
Example:
private void MdiTest_Load(object sender, EventArgs e)
{
//Codes for test only
var f = new Form();
f.Controls.Add(new TextBox());
f.MdiParent = this;
f.Show();
}
Note:
You can access to MdiClient control this way:
var mdiClient = this.Controls.OfType<System.Windows.Forms.MdiClient>().First();
Then you can do anything with it if you need. It is a Control like other controls.
Normal State:
Maximized State:
I have a mainform that contains a panel in which different MDI child forms are displayed. The controls in the MDI child got Anchor = Left, Right, Top, Bottom to resize.
The problem is the resizing of the controls in the MDI child when the main form is resized. I got it working with the following code:
private void MainForm_Resize(object sender, EventArgs e)
{
foreach (Form f in panel.Controls.OfType<Form>())
{
f.WindowState = FormWindowState.Minimized;
f.WindowState = FormWindowState.Maximized;
}
}
The problem is that the controls in the MDI child permanantly change their location when you are resizing. Is there some way to call a Resize method?
For those who got the same issue:
You need to set the Dock property to DockStyle.Fill for the MDI child. Else it just doesn't resize the controls.