HI all,
I have this code in which the window property of making child window load at the center of mdiparent.
Form2 f = new Form2();
f.MdiParent = this;
//center screen is working.
//f.StartPosition = FormStartPosition.CenterScreen;
f.StartPosition = FormStartPosition.CenterParent;
but instead of making the child window pop at the center it loads at the left side.
Can anyone help me on this.
Please refer the screenshot below.
I have even tried doing the same in vb. Even there i get the same error. I think the property of FormStartPosition.CenterParent is dummy.
alt text http://img13.imageshack.us/img13/7003/errorprb.jpg
I experimented a bit with this, and first came to the same solution as Patrick. But I was bugged by the following statement in the documentation of the StartPosition property:
You can also position the form to
display in the center of the screen or
in the center of its parent form for
forms such as multiple-document
interface (MDI) child forms.
So, I decided that there must be a way. And there is, though I don't find it all intuitive: set the StartPosition to CenterScreen (not CenterParent):
MdiChildUI form = new MdiChildUI();
form.MdiParent = this;
form.StartPosition = FormStartPosition.CenterScreen;
form.Show();
Of course, you can also set it in the designer instead of in code.
Setting start position as center screen works perfect for me.
I tried showing the child with the MDI container form as owner, but caused an exception for me. You could manually set the location before showing the child as follows:
Form2 f = new Form2();
f.MdiParent = this;
f.StartPosition = FormStartPosition.Manual;
f.Location = new Point((this.ClientSize.Width - f.Width) / 2,
(this.ClientSize.Height - f.Height) / 2);
f.Show();
EDIT:
f.StartPosition = FormStartPosition.CenterScreen;
is the correct way to center an mdichild on its parent form.
A form has an MdiParent property and a Parent property.
Please notice that an MdiParent IS NOT the parent of MdiChild form.
It is, its MdiParent;
So CenterToParent Method is meaningless for these types of forms.
You can see the functionality of CenterToParent Method when you program for multiple monitors application, or when you host a form inside another form by setting its Toplevel property to false, and setting its Parent property to another form.
Good Luck,
Aram Afsar
Related
My problem is ..I am able to add form in panel BY.
Form2 fm2 = new Form2();
fm2.TopLevel = false;
fm2.Dock = DockStyle.Fill;
panel1.Controls.Add(fm2);
fm2.Show();
but when i try to remove form from panel. By
panel1.Controls.Remove(fm2);
Nothing workss.This code note remove FORM from panel..
Actually your code should work even without closing form explicitely. Make sure fm2 points to form which you have added:
Form2 fm2 = panel1.Controls.OfType<Form2>().First();
panel1.Controls.Remove(fm2);
UPDATE: Again, you should remove exactly same instance of Form2 which you have added to panel. Creating new instance and removing it from panel does nothing, because that new instance was not added to panel and original instance still exist in panel.
I suggest you to remove all controls from panel, if you just want to use it as host to your forms. Just call panel1.Controls.Clear().
You could try to close form before remove it from Panel:
fm2.Close(); //Or fm2.Visible = false;
panel1.Controls.Remove(fm2);
I have a little problem with my MDI Parent Window and the MDI Childs window. The problem is that i need 3 child window but only the first it'll be maximize, so i use this code:
UserAdmin usrWindow = new UserAdmin();
usrWindow.MdiParent = this;
usrWindow.WindowState = FormWindowState.Normal;
usrWindow.Show();
For the others 2 windows i use this code:
TaskAdmin tskWindow = new TaskAdmin ();
tskWindow.MdiParent = this;
tskWindow.Show();
I only need that the first windows is maximized, but when i open the others they open maximized too.
How can i do to open one maximized and others in the default size over the first?
Thanks
That is not possible, yet can be achieved with very very tricky (using WndProc override, custom event loops) and ugly code which will not work in different operation systems in the same manner (i.e WinXP/WinXPSP1/WinXPSP3/Vista/Win7)
Your TaskAdmin forms cannot have an MDIParent in this situation. You need to either float these forms over the MDIParent or place them in panels in the MDIParent, docked to a side.
I have a tab control in a windows form and I want to be able to click on a tab and in the body area of the tab I want it to display another form as an embedded component. Is this possible? If so, can someone please provide an example or a link to an example of how to accomplish this?
You are probably looking for Tabbed MDI Child Forms
You can embed a Form but it's not the best choice.
Better place the contents on UserControls and add that to the TabPage.
Set your MainForm (Parent) as IsMDIContainer = true;
Create an instance of the ChildForm and call this function:
FormChild frmChild = new FormChild();
AddNewTab(frmChild);
Copy this Function to your code:
private void AddNewTab(Form frm)
{
TabPage tab = new TabPage(frm.Text);
frm.TopLevel = false;
frm.Parent = tab;
frm.Visible = true;
tabControl.TabPages.Add(tab);
frm.Location = new Point((tab.Width - frm.Width) / 2, (tab.Height - frm.Height) / 2);
tabControl.SelectedTab = tab;
}
I think the other answer has the right idea; Tabbed MDI is probably what you want.
There is an approach where you create a UserControl that has the same content as the form and use that on the TabPage.
TabPage myTabPage = new TabPage(sometext);
myUserControl = new myUserControlType();
myUserControl.Dock = DockStyle.Fill;
myTabPage.Controls.Add(myUserControl);
myTabControl.Add(myTabPage);
http://bytes.com/topic/c-sharp/answers/270457-can-i-add-form-tabpage goes into more detail; but I'd look at the MDI stuff first.
If you do not want to use MDI, you can try to put everything from desired form to user control and add this user control in both form and tab.
I asked this question previously and thought I had it figured out but it still doesn't work.
Form.Show() moves form position slightly
So I have a parent form that opens a bunch of children with show() and then when one is needed I use bringToFront() to display it. The problem is when show() is called the child form is aligned perfectly but when I use bringToFront it moves left and down 1 px which screws with my borders. I have set all the child forms startPosition properties to Manual before show()-ing them. I have set frm.location = new Point(x,y) when bringing to front. I've also tried explicity setting frm.location when show()-ing also. It still moves it left and down 1 px when I bringToFront(). Is there something with bringToFront() that doesn't let me change the location property of the form? Here is my code:
if (myNewForm != null)
{
myNewForm.MdiParent = this;
bool isFormOpen = false;
foreach (Form frm in Application.OpenForms)
{
if (frm.GetType() == myNewForm.GetType())
{
frm.WindowState = FormWindowState.Maximized;
frm.BringToFront();
frm.Location = new Point(-4, -30);
isFormOpen = true;
break;
}
}
if (!isFormOpen)
{
myNewForm.StartPosition = FormStartPosition.Manual;
myNewForm.Show();
}
}
EDIT: Ok so apparently Microsoft has a bug that lets StartPosition only work for ShowDialog() and not Show() but refuses to fix it: http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=107589
But my application needs to keep all the different forms open and just bring them to the front when needed...so ShowDialog() could not be appropriately used in this instance correct? So what options do I have? Any?
If you wish to set the form location, you have to set the form's WindowState to Normal (on any other setting, the location is set for you according to the rules of that setting, so your value is ignored), and its StartPosition to Manual.
frm.WindowState = FormWindowState.Normal;
frm.StartPosition = FormStartPosition.Manual;
frm.BringToFront();
frm.Location = new Point(-4, -30);
I suppose the forms are moved by the code that handles the Show() (and BringToFront) requests, so you really cannot set the form's location - neither before nor after calling the method - because the form location will be updated after the code in your main window has executed (and left control back to the window message pump, in Win32 terms, basically).
I would use a subclass of Form for each of your forms and then add an explicit Point property that indicates the fixed position where that particular form is expected to be. Inside this class, override the OnShown virtual method (or perhaps the OnActivated method too) and simply update this.Location with the correct location.
That should force the forms to the correct position, even if some code inside windows forms changes it at some time.
Have you tried:
this.Location
or
Form.ActiveForm.Location ?
What about using a p/Invoke to MoveWindow? The link provided includes a C# example.
Have you tried showing the form, and then adjusting the Location?
Edit: Or, have you tried adjusting the Left and Top properties?
I have a Windows Forms application that opens MDI child forms. When I select those forms, I need to set or render its windowstate to Maximized. The problem is, when I navigate between the open forms, it reverts back to the normal window state, and when I set the window state to maximized again, it shows the transition from normal to maximized state and it doesn't look nice.
How can a Windows application be created that have an MDI parent form that opens many MDI childs in maximized window state?
Here's an answer based on using the MDI "Parent Form and Child Form paradigm," with the following assumptions :
you have a MenuStrip control 'Dock = 'Top on your MDIParentForm, and you've implemented the automatic MDI &Window menu handler as described in : How to: Create an MDI Window List with MenuStrip
you are creating new child forms that :
a. do not have a MaximizeBox, MinimizeBox, etc., but may have ControlBox (for closing them)
b. these child forms may be resizable or not : we won't consider the implications of that here.
You want these MDIChildForms to display maximized in the MDIParent Form, but not to obscure the MDIParentForm's menu.
Okay : assuming you have all your child Forms fully designed, "waiting in the wings" : we might see some code like this in your MDIParentForm code :
// create instances of your child forms
Form2 f2 = new Form2();
Form3 f3 = new Form3();
Form4 f4 = new Form4();
Form5 f5 = new Form5();
private void MDIParentForm1_Load(object sender, EventArgs e)
{
f2.Text = "subForm1";
f3.Text = "subForm2";
f4.Text = "subForm3";
f5.Text = "subForm4";
f2.MdiParent = this;
f3.MdiParent = this;
f4.MdiParent = this;
f5.MdiParent = this;
f2.Dock = DockStyle.Fill;
f3.Dock = DockStyle.Fill;
f4.Dock = DockStyle.Fill;
f5.Dock = DockStyle.Fill;
f2.Show();
f3.Show();
f4.Show();
f5.Show();
}
At this point, the dock style 'Fill applied to the child forms will make them full-screen, and keep them from obscuring the MDIParentForm menu : and the menu will allow you to auto-select which one is frontmost.
Now, if you want to do fancier stuff : like resizing the child Forms, tiling them, cascading them. You are going to have to change the 'Dock property of these child windows : and then you can make use of the built-in MDI paradigm window arranging facilities as described here : How to: Arrange MDI Child Forms
And if you want to create multiple instances of one type of pre-defined child form : How to Create MDI Child Forms ... see the example on how to use a 'New menu entry : may prove useful.
If you want the window state to always be maximized, I'd recommend switching away from an MDI Form. A TabControl may work better, in that case.
MDI forms have quite a few usability issues, which is why they are not commonly used anymore, and tend to be replaced with other controls/options.
After reading Reeds answer and especially your comment:
problem with tabcontrol is, i have a
lot of controls used per child form
Maybe this will help:
Don't put your controls into a Winform. Instead encapsulate them into a UserControl (maybe it already works by changing your inheritance from Form to UserControl).
Now put every UserControl on it's own TabPage and set its Dock property to Fill. Now you are able to change each UserControl on it's own, without any interference to another control on another TabPage (as far as you don't built in any connection).
If you intend to give up on MDI, you could have a look at docking frameworks like WeifenLuo or DigitalRune. These are free, for other options you can have a look here: http://windowsclient.net/downloads/folders/controlgallery/tags/Windows+Forms+Docking+Windows/default.aspx
EDIT:
If I remember well, DigitalRune allows the usage of windows forms as containers for docked content so the migration effort would be smaller.