Is it possible to add a form to a TreeView? - c#

I want to create a TreeView that contains complex forms of EmployeeNode as nodes. When you add the node I will call InitializeComponent(). So that the expected result will be something like
Or codewise:
EmployeeNode EMPND = new EmployeeNode(new Employee());
EMPND.Draw();
VisualTree.Nodes.Add(EMPND);
//Draw (at EmployeeNode form class)
public void Draw()
{
InitializeComponent();
}
Is this possible?
edit
If this cant be done using TreeView, I am open to hear about other ways to store forms, other then TreeView, that offers similar hierarchy - based WinForm visual solutions (maybe a generic version?)

Set the TopLevel property of the form you want to add as false.
var form = new Form { TopLevel = false };
treeView.Controls.Add(form);
form.Show();

Related

Show top most Child Form c#

How can I show topmost a button clicked child form in my parent form (with a tabcontrol which docked as fill)?
It always shows the form at the back of the tabcontrol of the parent form, I've even used:
frm.TopMost = true;
frm.BringToFront();
Still shows at the back.
What you want is not possible. MDI children of a control get shown on a control (which you can't directly select) called MdiClient, which is not transparent (and can't be) and by default, goes always to the back of other controls in the parent form.
So the only way to do this, would be getting the MdiClient over the controls in the parent form: this would do what you expect, but it would also hide the parent controls when there are no child forms displayed (since again, the MdiClient is not, and can't be transparent).
So the only reasonable way would be having a maximized child form with the TabControl, instead of having that TabControl directly on the parent.
Or you could have your TabControl only shown when there are no child windows. For that, make a timer in the parent form, and check this at every interval:
if(MdiChildren.Length > 0)
myTabControl.SendToBack();
else
myTabControl.SendToFront();
This will only work if the MDI children are always maximized: your TabControl will not be visible when there are any children (no matter if they cover it or not)
Update
As remarked in the comments, you can have "your own MDI", by having a host control (a Panel, for example) in the parent form and loading the child forms in that control:
var form = new ChildForm();
form.TopLevel = false;
form.Parent = myHostPanel;
form.Show();
This would show the form inside the panel (which you can locate and zorder where you want)... you lose all the MDI management though, and you'll have to keep track of your children (and take care of the forms' events if needed) yourself.
I'd not use this solution, it's pretty hacky and can get messy for big applications (unless you do a correct system)
As a summary
Since we're discussing these methods in the comments
You can hack your way to do what you want, but you'll come into all sorts of problems with any approach.
If I was you, I'd redesign my application so that what you want to achieve is not needed. If you can't do that, the only sane way would be just not having those controls in the parent form, have an always-maximized, non-closable MDI child form with those controls, and skip that window everytime you need to work in the MDI children collection.
Please explain what components of what frameworks you are using and what you have done so far. Without that information i suggest the following solution (untested).
In the "ButtonClick" event of your ParentForm do this:
ChildForm cf = new ChildForm();
cf.MdiParent = this;
cf.Show();
If this doesn't work you may add a
cf.Focus();
This question make me uncomfortable :). after a lot of testing, I can't really find a solution. neither BringToFront() Function nor SendToBack() Work Properly. maybe the following approach can help you. I use IntersectWith Function of Rectangle Class and test if form intersect with tabControl or not. if so change the tab control visibility to false otherwise true. take a look at the following code:
first make form declaration public at the mdi parent form:
public partial class MdiParentForm : Form
{
Form frm = new Form();
}
After that when you initialize your child form, add some handler to its locationChanged event, like this:
frm.MdiParent = this;
frm.LocationChanged += Frm_LocationChanged;
frm.Show();
And at the end, This is the handler:
private void Frm_LocationChanged(object sender, EventArgs e)
{
Rectangle tabControlRectangle = new Rectangle(tabControl1.Location, tabControl1.Size);
Rectangle childFormRectangle = new Rectangle(frm.Location, frm.Size);
if (tabControlRectangle.IntersectsWith(childFormRectangle))
{
tabControl1.Visible = false;
}
else
{
tabControl1.Visible = true;
}
}
Thanks to #Jcl, Problem with this is that the tab control will hide and show as long as any point of the child form touches its rectangle. Moving the child form around would be horrible :-)

Child form implement in C# Winform

Well, I'm writing a simple application which will have multiple forms inside it. Now say one form will be the base window i.e. the parent and all other will be child of it. So for this I'm trying to opening the child window by below method.
//Inside class FormBaseWindow
private void linkLabelReservation_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Point childLocation = new Point(this.Location.X + 100, this.Location.Y + 120);
FormReservation formReserve = new FormReservation();
formReserve.Location = childLocation;
formReserve.MdiParent = this.MdiParent;
formReserve.Show();
}
And also set the IsMdiContainer attribute of FormBaseWindow class to true. Now the things is, I want to have feel like dragging the FormBaseWindow window will drag the whole thing including the child window inside it. Currently once I click on the FormBaseWindow window its coming foreground and the child is going behind it.
In MFC I was able to do it by setting the window style Child and set it as a child window, but here the story is bit different.
I'm using C# Winforms with VS 2012.
Change :
formReserve.MdiParent = this.MdiParent;
to
formReserve.MdiParent = this;
NB: You should use
formReserve.MdiParent = this.MdiParent;
when you open a child form from another child form to make it under the same MdiParent.

how to display form within another form when i click the menu item

i'm new to windows application.can anybody help me.here is my doubt.im having one parent form and it has four menu items. when i click any of one menu item ,it should display another form within that parent form itself. how to do it?
Thanks in advance
According to details you ve provided it seems that you need to use MDI Forms concept in your app. It s very easy to learn and refer to the following links:
http://www.codeproject.com/KB/cs/mdiformstutorial.aspx
How to open a form within a form?
Just include the code in the 2nd link within your menuitem_Click event...
Hope this helps...
There are several ways you could do it.
One simple way for a newcomer is to add the form to the parent form in the designer. Set the visible Property to false (in the properties) so it will not be shown at first when your program is run.
Then you can set the visible property to true when you handle the menu item clicking.
There are code ways to do it too at runtime etc.
Hers an article with stuff about adding controls (and implicitly child forms) at runtime.
Inside your main form, add a panel and then use the below method to display the child form.
private void InitChildForm(Form childForm, Panel parent)
{
childForm.TopLevel = false;
childForm.Parent = parent;
childForm.Dock = DockStyle.Fill;
childForm.Show();
parent.Parent = this;
parent.Dock = DockStyle.Fill;
}

C# call MDI child from child form

I have these forms:
MainScreen - MDI container
DataBaseOutput - child
NewAnime - child
DataBaseOutput has a tab control that holds datagrids, each for different tables. I use an access database.
In those tabs, there is a menustrip, where the functions "New", "Edit", "Delete" etc. will be called from. Now when I'm on the first tab's menustrip and click on "New" I want to open the form "NewAnime", inside the MDI container. This however, is not working as I planned. At first I tried to just call it from the childform (DataBaseOutput). This resulted in opening a new form instead of a child. when I made it a child it was not showing up.
Then I triend lots of things, but I still haven't figured it out.
This is the current code for calling the form. It calls the form with a method in the main form:
private void NewAnime_Click(object sender, EventArgs e)
{
MainScreen main = new MainScreen();
main.mShowForm(2);
this.Close();
}
Method in the main form:
// Forms for MDI Parent
DataBaseOutput OutputForm = new DataBaseOutput();
NewAnime AddAnime = new NewAnime();
// How i made them childs (this is at the InitializeComponent(); part)
OutputForm.MdiParent = this;
AddAnime.MdiParent = this;
public void mShowForm(int formnumber)
{
switch (formnumber)
{
case 1: OutputForm.Show(); break;
case 2: AddAnime.Show(); break;
}
}
Does anyone have a clue of what i'm doing wrong and maybe has a better idea? This might be a bit too much working around, but as I said, it's my first time using MDI forms and i'm just trying to get it to work.
Have you set the MainForm to be an MDIContainer? To do this set its IsMdiContainer property to true; Also check it has File and Window top-level menu items and New and Close menu items. (The tutorial suggests this, I know it should have a Window menu item at least).
Have a look at this tutorial for more guidance: Creating MDI Child Forms (MSDN)
EDIT: Looking at it more closely, it seems you are creating a new instance of MainForm, and trying to show the form as a child of that instance, as opposed to showing it in the existing MainForm. I assume you already have an instance of MainForm open at this point? And assuming the OutputForm and AddAnime forms are children of MainForm you could call the existing instance's method like this:
private void NewAnime_Click(object sender, EventArgs e)
{
this.ParentForm.mShowForm(2);
this.Close();
}
but ideally you should have an event on DataBaseOutput that MainForm listens to, and shows the new Form when the event is raised. See here for more info (it talks about user controls and not child forms, but the principle is the same):
Calling parent form functions from a user control

winforms panels vs java swing panels

In java swing I can insert panels into panels and so on, and not have to build a brand new window for every view of my applicaiton, or mess around removing and adding controls.
Theres a panel clas sin C# however I cant see any way of creating a 'panel form' or basically just a form in form designer thats a panel and its contents.
How do I do this then and work the way I did with java swing?
Usually i just dock different forms within eachother setting the IsMdiContainer Property to true on the parent window. Then i create subforms that i dock using the following function:
static class FormUtil
{
static public void showForm(Form sender, Control reciever)
{
sender.ControlBox = false;
sender.FormBorderStyle = FormBorderStyle.None;
sender.ShowInTaskbar = false;
sender.TopLevel = false;
sender.Visible = true;
sender.Dock = DockStyle.Fill;
reciever.Controls.Clear(); //clear panel first
reciever.Controls.Add(sender);
}
}
then whenever i need to dock a form inside a panel on the parents form i just do:
FormUtil.showForm(new SomeForm(), this.splitContainer1.Panel1);
This allows me to delegate some of the form creation to different designers. Works like a charm for me, love to hear if theres a better way of doing it.
Actually, you can use the panel control and set it's Dock property to Fill. That way, your panel will be the entire canvas of the form. Then, you can add child panels as needed either through code behind or through forms designer.
There's the concept of user controls which basicly provides you with a panel like designer surface , not to mention that you can create atomic forms (which can be reused) and register them as inheritable, that way you can provide inheritance too.

Categories

Resources