In Form1 I'm enabling IsMdiContainer and I added a MenuStrip. In Form1_Load I "new" Form2 and I'm assiging Form2.MdiParent to this which is Form1. I'm also maximizing Form2 and this operation works well.
In Form2 I have a treeView on the left side of the form and on the right side of the form I would like to display a number of different forms with various editing capabilities which will be dependent upon the node or level selected in the treeView.
I would like to create a number of different forms for editing data that would be displayed in Form2 depending on the selection from the treeView. I can't seem to add a form to the MdiChild and I've been seeing some posts where adding a form to a form may create some programming problems which I'm not sure about.
I really don't have any code to paste into this post because nothing seemed to work except for the Mdi Parent and Child relationship which was pretty simple.
Thanks in advance for any help.
There is a lot of information on this subject, but some documentation can be difficult to understand for some new developers. Follow these steps:
Open Visual Studio
Create a Windows Form Application
Click your Form
Go to Properties for that Form
Minimum Size : 1366 pixels by 768 pixels.
Launch Maximized
The important element is IsMdiContainer
Open your Toolbox.
Go to Menus
Drag FileMenu onto your Form
Build your Menu
Then go to Solution Explorer
Right-Click Add Item
Add another Form
I left mine as Form2 (In a real program, not a good name).
So within those fifteen steps, we have all that we need to accomplish our goal. So what we will do to finish our task is:
Go back to our First Form
Go to our FileMenu
Double Click on the menu button you wish to link.
It will load a code view, inside the area put this:
Form2 newFrm = new Form2();
newFrm.MdiParent = this;
newFrm.Show();
What this code is doing is three distinct things:
Line 1: It is actually calling our object, in this case a second form. It is actually building our object for us.
Line 2: Is actually linking our second form to our current form, this is physically turning our second form into a Child Form.
Line 3: This is actually physically showing our second form when the button is clicked.
That is all you need to physically show a Form.
In regards to your second question, I'm not entirely sure what your attempting to accomplish. It sounds like your trying to have a tree, then as a Node is selected the right hand side of the Form changes to specific context.
Now this isn't the nicest example, but do you mean something like this?
TreeNode node = treeView1.SelectedNode;
if (node.Text.Contains("XP"))
{
TextBox one = new TextBox();
Panel i = new Panel();
i.Dock = DockStyle.Right;
i.BackColor = Color.Black;
i.Controls.Add(one);
i.Show();
TreeFrm.ActiveForm.Controls.Add(i);
}
Not sure if that is what you are seeking. Obviously you'd want to implement a FlowLayoutPanel to make the positioning not a pain for you. Keep in mind an MDI Parent, with a Child Form acting as a MDI Parent will not work very well. As most things will default to MDI Parent Forms Docking / Positioning. This example is not pretty, but I'm not entirely sure of what your asking.
Are you trying to dock other forms or components on the same form?
Related
Windows 7 pro 64 bit, VS2015 or VS2019
Hi,
I have a C# Win Form with many various controls.
I defined the main form as MDI parent and built an MDI child form with it's own controls, activated by a menue item in the main.
The child form builds nicely, but it is always displayed under the main form's many different controls.
I have tried many remedies, non of whch solved the problem.
I'v Set the child form as TopMost = true; TopLevel = true; each or all, for no avail.
Have moved from VS2015 to VS2019 community - Same.
I have been wasting hours to solve something that seems to be strightforward.
Can anyone help me out of this?
//In Main Form with menustrip, ComPortSetup is a standard winform class with some controls
private void portSetupToolStripMenuItem_Click(object sender, EventArgs e)
{
ComPortSetup comPortSetup = new ComPortSetup();
comPortSetup.MdiParent = this;
comPortSetup.TopMost = true;
comPortSetup.TopLevel = true; //Can not change programmatically
comPortSetup.Show();
}
That is how MDI works, there is no way around that.
All controls on the MDI Parent form will take away "client" space for the MDI child forms. And thus they will always be shown on top of any MDI Child form.
In other words, the MDI child forms can only use the space that is on the MDI Parent form that is not occupied already by other controls.
What you can do is put a panel on the MDI Parent form, and for example dock it to the left. Then put your "main" controls on that panel. The MDI Child forms will use whatever space is left on your MDI Parent form.
You could put a splitter control next to this panel so you can make it larger or smaller, or make it slidable so the panel comes forward when your mouse is near it, and hides itself again when the mouse is moving away from it.
Another approach you can try, is not making it MDI anymore and set the parent of the "child" forms yourself. But this will most likely cause other problems.
I would try the first approach, the panel on the mainform, docked to the left with a splitter control next to it.
I am not sure, but I believe that when using MDI forms, it is not expected that the parent has its own controls on the main form area, otherwise you will experience this exact problem.
So there are a couple of ways around this.
Firstly you can place a Panel on your parent form, and then your child can be added to the Panel.
This is now "proper" MDI control however, but it might allow you to achieve what you want.
ChildForm child = new ChildForm();
parentPanel.controls.add(child); //ParentPanel needs to already be on main form
Or the other method is to put your Parent Controls either on a MenuStrip (like MS Word) or you can use a Floating Dockable child form (think Visual Studio) which is then always visible.
If you want to do the latter, then I would suggest DockPanelSuite control to help you with this
https://github.com/dockpanelsuite/dockpanelsuite
So I'm trying to learn a thing or two about coding with c# and something i find quite annoying is the way to switch between forms.
Lets say for a game you want to go to the options panel and when you click the button to get there it closes that window(form1) and opens a new window(form2) for my app.
It doesn't look very nice having windows opening and closing like that so I'm wondering what i can do in order to make it switch from form1 to form2 without closing form1 and not open form2 in a new window (Everything switched on the main window(form1).
Might sound a bit confusing but hopefully you understand what i mean.
The code I'm using so far to switch between forms:
ChangeOptions optionchanger = new ChangeOptions ();
this.Hide();
optionchanger.Show();
You could add two panels to a single form, each of which contains the controls you would otherwise have added to one of the two forms. Then switch between the panels by changing their visibility or Z-order. This is slightly tricky in the Windows Forms Designer because you'll have to design the two panels, then position them in the same spot on the containing form.
As #ryanyuyu points out, you can set the Dock property to DockStyle.Fill and switch which panel is on top using Control.BringToFront or Control.SendToBack(). This is also a decent way to interact with the two panels in the designer, as you can switch which is on top from a context menu option.
To truly have two forms, your only option is to show a dialog. Hiding your current window is of course optional.
However, you can:
Group all the controls on a given "form" into a Panel or GroupBox, then show/hide the container control.
Put all the controls into UserControls and have an instance of each UserControl on the main form. You can then show/hide the control.
I prefer the second method as it keeps the encapsulation tighter. Since you already have two forms, its easy to convert to user controls.
lets say i have a main form which have a lot of functionallity.
this form have a tab control in which each tab contain some set of functionality.
what i want to do is when i click on each tab controls button i want to load a form into the client area of the tab control.
so instead of having a lot of controls in the main form , i will only have set of forms and each form will have its control.
i think this is will be better in term of managing the controls so i dont have like 150 control on the main form.
so basically i want to load a form on another form and not show the form in a seperate view.
if its not possible with forms then can i use another control that will group the controls and will be loaded on the main form?
thanks
Alternate 1 :
You can make each of the form as a User Control and then you can load the appropriate user control in a blank panel in your main form whenever required.
You should be able to find a way to communicate between your form and those user controls.
Alternate 2 :
You can show the appropriate Form using ShowModal() method, with the main form as parent, that way user can finish the work with the child form, before coming back to the main form.
Disadvantages here are user wont be able to interact with the main form as long as the child form is closed.
I would recommend looking into User Controls.
User Controls come with a designer, just like forms, and have a rich event model to tap into. Unlike forms, they are easy to embed into other controls and forms. As a matter of fact, user controls will show up in your toolbox to drag-and-drop onto another form.
It's at least worth taking a look at.
Following code adds one Form to a panel in another form.
Add this code in Form1
Form2 ff = new Form2();
ff.TopLevel = false;
ff.Dock = DockStyle.Fill;
ff.ControlBox = false;
ff.Text = "";
panel1.Controls.Add(ff);
ff.Show();
The flip side is your panel should be big enough to accomodate the form...
I have a subform (child) that I want to use in a number of parents. I'm not a professional developer (I'm an architect - I know, you can save all the jokes... :) - working solo at present). I've ended up using an MDI form with the subform as a child. I maximize the subform form and most things are fine except that although I've tried to disable all the various widgets (the subform in the designer shows NO caption/icon/button area), I get TWO icons on the left and TWO sets of buttons on the right - of which ONLY the restore button works. Either of the sets of buttons will work the one child form.
Is there any way around this? I want the subform to be "transparent" the the user - they shouldn't be aware there's a subform in use.
I've done a quick search and I'd already suppressed the actual caption as mentioned in another answer - to get the caption bar suppressed in the designer...
Is MDI the right technology, or is there a better way to have the same subform appear in multiple parent forms?
VS2008, C#, Windows 7
TIA,
Paolo
There's a WF bug that will double the glyphs if you create the MDI child form in the parent's constructor. Here's an example:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
this.IsMdiContainer = true;
var child = new Form();
child.MdiParent = this;
child.WindowState = FormWindowState.Maximized;
child.Show();
}
}
Move the child form creation code to the Load event to avoid this.
Thinking about this for an About dialog but I'm sure it's applicable in other places (say a find box)
Sorry if this is a dupe, but I couldn't find this or how to articulate the last part about it only being on top of the parent. How do you make a form that is always on top of the parent form, but is non-modal, but doesn't cover up other apps?
Try this to open your dialog:
FindDialog fd = new FindDialog();
fd.Show(this);
The key is to assign dialog's owner.
Not sure exactly what you mean; Form.ShowDialog is only modal with respect to the parent, not the application, unless the application is single threaded.
For example, I made an app to test this which was organized like the following:
mainform:
2 buttons, each of which begins a thread that creates a frmDialog1 and calls ShowDialog
frmDialog1:
single button which creates a frmDialog2 and calls ShowDialog on it.
frmDialog2:
does nothing (ie. blank)
when they were all running I could access/drag mainform. I could also do the same with frmDialog1 (both versions) only if I hadn't clicked the button that shows dialog 2.