Adding child components to parent's panel? - c#

m developing a sort of project which I needed a cool customizable interface, so I designed a 'parent-form' from which all childs would get 'stylized', according to XML customization options.
I added a TableLayoutPanel to draw borders and a Panel in the middle, where child forms would supposedly add their components and make their jobs.
The problem I face is, even though I set that 'content panel' to 'public', the designer wont let me add controls to it from the child forms.
Is there any different way I can make designable forms deriving from a 'customizable' superclass?
Edit: The parent class is public, every container containing the Content-Panel are also set to public.

I manually added to child's designer.cs a new Panel inside the parent's content pane, set it to DockStyle.Fill. When I came back to the Designer, it will now let me add components to child's content Panel.
A bit messy and I'm pretty sure there shall be another way around...
But I'll work along like this until i can figure out a better workaround.

I have added a new public Panel from code other than designer in the parent's class scope, Then in the parent constructor I added it to the TableLayoutPanel, configured docking and colspan from constructor code, below InitializeComponents() call and BAM!
public Panel contentPane = new Panel();
public Dialogo()
{
InitializeComponent();
Content.Controls.Add(contentPane);
contentPane.Dock = DockStyle.Fill;
// More code
}
So its a contentPane inside 'Content' which is another panel in the second line of the table ocupying 5 columns (so the table surrounds it and allows me to draw the borders around.
I don't know why, but having added the content-panel in code other than on the designer allowed me to directly add components to the panel from the Designer in child forms.

Related

Recommended way to manipulate User Control panels into a Windows From

I just started working with Visual Studio C# and to be honest I didn't fully understand what happens when we chose to hide a form or a user control.
My intuition tells me this hide/show method is kind of "inefficient" way to get an user through all the functions of my app.
So I am asking you guys if there is another workaround to "load" user control parts in a form.
Right now my main_menu form has all the user control objects placed on the form, but hidden, and I am using buttons to show them.
Is there a better way to achieve the same result? (I was thinking of a workaround like having an empty panel where I can load the User Control - not sure if possible)
Thank you!
You can create the controls on the fly and add them to or remove them from the Controls collection. On the class level, define this field
private Control _currentPanel;
You can use a more specific type here, if you are deriving all your panels from a common base type.
Then change the panel with
// Remove previous one.
if (_currentPanel != null) {
Controls.Remove(_currentPanel);
}
// Add new one
_currentPanel = new MyNewPanel();
//TODO: possibly set the panels Docking property to Fill here.
Controls.Add(_currentPanel);
In the example I am working with the form's Controls collection; however, you might have to use the Controls collection of some container control holding the panel.

How to make a UserControl grow with container (Form)

I have an app which consists of a Form that after loading adds two Controls that are descended from UserControl. The UserControls are sharing the same coordinate space and I alternate which one is visible with BringToFront()/SendToBack(). Basically a poor man's WPF Pages. The UC's each have a panel that takes up the entire area of the UserControl and the panels contain all of the Buttons, TextBoxes, DataGridView's, etc.
Basically, I'm trying to make the UserControls size with the Form containing them, using either the Dock or Anchor properties, but the problem is the UserControl itself doesn't appear to have these properties, so when I resize the window, the panels containing all the content never get bigger. I've tried setting both the Anchor and Dock properties of the Panels inside each UserControl to no avail. I've also tried adding a FlowLayoutPanel to the Form, and then adding the UserControls to that instead of directly to the Form object. Same result. Did I screw myself by going with UserControls containing panels? Any way to fix this? PS, I'm kinda new to C#/.NET. Been doing most of my dev work in Java for a while now.
Code ex:
//Add the panels
FsLookupPanel = new FSLookupPanel(this, this.LdapConn, this.dbConnect);
MakeResPanel = new MakeReservationPanel(this, this.dbConnect);
this.flowLayoutPanel1.Controls.Add(FsLookupPanel);
this.flowLayoutPanel1.Controls.Add(MakeResPanel);
//this.Controls.Add(FsLookupPanel);
//this.Controls.Add(MakeResPanel);
FsLookupPanel.Visible = true;
MakeResPanel.Visible = false;
Have you tryed somthing like this ?
FsLookupPanel.Dock = MakeResPanel.Dock = DockStyle.Fill;
this.flowLayoutPanel1.Controls.Add(FsLookupPanel);
this.flowLayoutPanel1.Controls.Add(MakeResPanel);
this work with framework 3.5 so you should be alright with 4.5
You don't have it when setting the properties via the designer but the property is there. Via code it is working

Indexing into Controls Collection on Tab Control

I have a C# Forms tab application. Each TabPage has a menu on the left (Outlook style navigation panel), and a Panel on the right for content.
If I want the content panel for tab page 0, how would I go about fetching it? I'm a bit stumped because I don't know how to index into the controls collection on a tab page. The following is underlined in red, so I believe its wrong.
Panel panel = tabControl.TabPages[0].Controls["Panel"];
EDIT: remove Window in Panel sub question. It will be moved to a separate question.
Sorry about the beginner questions. I'm a C/C++ guy with lots of MFC time, and C# UI is a bit frustrating at the moment.
foreach (Control control in tabControl1.TabPages[0].Controls)
{
// if (control.Name == "panel1")
}
You can always call this recursively on control.Controls to find a control in any hierarchy. control.Name can be used to find your specific control.
You can't show a Form, inside a Panel. You could create Custom Control where you can add your functionality and add that control to a Panel.
in order to create a new form for example you need to create a variable of what ever form that it is you want to create.
example
Form2 frm2 = new Form2();
frm2.Show();
if you want to show that form in the panel then the panel would be the Owner keep in mind the difference between Owner and Parent
please paste what ever code you have so far and we can suggest the necessary changes
Finally, how does one display a Window in a Panel? - you don't want to do that. If you want a window and a panel to share a piece of UI functionality, create a user control with all the the functionality and then you can place it in a form or in a panel.
A possibility to encapsulate complex UI content is to create a UserControl. This way you can create a reusable piece of complex UI you can basically add as a "blob" inside a form.
The reason why
Panel panel = tabControl.TabPages[0].Controls["Panel"];
is underlined red is because the Controls collection returns a Control which might be a Panel but also might be something else. So you need to cast it:
Panel panel = tabControl.TabPages[0].Controls["Panel"] as Panel;
if (panel != null)
{
// got a panel here so do something
}
Also: MSDN has some good resources - you should make use of it.

User Control that acts like a standard Window, only confined to a pane

I'm making a program to generate code for me, and I'm fashioning the UI after Game Maker due to how easy the interface is. It has a SplitContainer with Panel1 containing a TreeView and Panel2 containing an arbitrary amount of self-contained windows (real windows, not some hacky workaround). I wanted to use user-controls to store the controls I use to modify things, but I can't figure out any way to put it in a window inside the splitContainer's Panel2. Can anyone help me?
Here's a good example:
http://i.stack.imgur.com/CG6kO.png
Those two sprite property windows are what I'm trying to do.
i think what you are looking for is called mdi-container
however the only real mdi container i've seen so far (in .NET) is a form ... sadly no panel or something similar...
but if you just want the "window in a window" effect: simply create your new form, set the TopLevel property of that instance to false, and add the instance to your form/panel/splitcontainer/whatever like any other usual control
You could try using an MDI form and to implement your TreeView control, check out some sort of docking panel. I've used this one in the past (http://sourceforge.net/projects/dockpanelsuite/).
It is very flexible. You set up one of these dockpanel forms, docked to the left of your MDI form. It will always be "on top" and the user can resize it exactly like the splitter control on a form. If you like, it can also has an "autohide" feature which may or may not be desirable in your case.
It can then contain you treeview, which can load all the MDI Child forms you like.
You'll find you're not fighting how "Windows" really want to behave and things will run a lot more smoothly.
Put it into the Panel2's Control collection via the Add() method, apply coordinates, anchor and docking programmaticaly.
I did similar thing once, and for that reason, I have ReplaceControl method, which I paste below:
static public void ReplaceControl(Control ToReplace, Form ReplaceWith) {
ReplaceWith.TopLevel=false;
ReplaceWith.FormBorderStyle=FormBorderStyle.None;
ReplaceWith.Show();
ReplaceWith.Anchor=ToReplace.Anchor;
ReplaceWith.Dock=ToReplace.Dock;
ReplaceWith.Font=ToReplace.Font;
ReplaceWith.Size=ToReplace.Size;
ReplaceWith.Location=ToReplace.Location;
ToReplace.Parent.Controls.Add(ReplaceWith);
ToReplace.Visible=false;
}
Only thing left to do is to create some control manually on the form, as the placeholder for your Form. Use label, for example.
From How to implement a-form-inside-a-form with runtime embedded forms switching?

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