C# Adding tabs at runtime using Form's controls - c#

I have thought of this idea where you could have a tab control on a form, but instead of defining each tabs controls before runtime, inherit or use another form's controls.
Basically a tab control is in place on the main form that has no tabs on it before runtime. When runtime comes along I want to create tabs, but the controls that would be on each tab would be from seperate already created forms.
Each form would be a seperate tab that had been created prior to runtime.
Is this possible? Is so, how?
Thanks in advance
EDIT
I'm using 3.5

first create a User Control and design it as you would a regular form, then add it to your TabControl.TabPages
TabPage page = new TabPage("Title");
page.Controls.Add(new CustomUserControl()); //your user control
this.tabControl1.TabPages.Add(page);

If all you'll be doing is copying controls from a TabControl on one form to a TabControl on another form:
Instantiate the form you want to copy from
Find the TabControl on that form
Iterate through Controls on the TabPages inside that TabControl
Add each Control you find in that collection to the tab(s) in the control you want to copy to
Close and Dispose of the form you created in step 1

Yes, it is possible. You have to add the controls onto the TabPage, then add the TabPage to the TabControl.TabPages

Related

Initialization of tabs in tabControl

I'm using tabControl with many tabs(>10), and each tab has UserContol. But tabs Initialize each control at starting my app. It's making my program too slow. I want to run my userControl only when I click on it. How can I do this?
You should improve your UserControls that they do not do the performance stuff until they get activated/visible. Give them a method Activate which the tabcontrol calls when the tabs becomes the selected tab.
Alternatively you could not add the UserControls to the TabPage content in the designer. Instead create your UserControl when the tab becomes active. But this will make them insivible in the Designer.
You could either have a marker such as IsLoaded and until a tab is selected, not load the controls. When the tab is then selected, if it hasn't already been loaded, you could load the controls and add them at runtime to the tabs Controls.
Or, you could have the controls added but not do anything with them until the tab is selected, and then each tab will do the calculations or whatever and update the correct controls.
It's all about your design.
Add a handler to your UserControl's Load event. Then kick off slow activity in the handler.

can form Contain another form in winforms application?

i want to make one form contain another form ;
For the sake of browsing different sub-forms in the same Parent Form using parent controls .
You can use a TabControl to group different controls together, however if you REALLY need to have a subform, you can use MDI forms.
This is a tabcontrol:
It basically has a groupbox for each individual tab, so you can add and remove as you please.
From the docs for tabcontrol:
A TabControl contains tab pages, which are represented by TabPage
objects that you add through the TabPages property. The order of tab
pages in this collection reflects the order the tabs appear in the
control. The user can change the current TabPage by clicking one of
the tabs in the control.
And MDI:
Multiple-document interface (MDI) applications allow you to display
multiple documents at the same time, with each document displayed in
its own window. MDI applications often have a Window menu item with
submenus for switching between windows or documents.
Solution is to set the parent form property
IsMdiContainer = true ;
and set child forms with
childForm.MdiParent = parentForm;
I believe that you are looking for TabControl in C#. This allows to to browse through different tabs in the same parent form. You may refer to TabControl in C# for more information.
Hope this helps.

Create controls dynamically or create controls in a side form? C# winforms

I have a form full of controls, and there is no room for other controls. On the bottom of the form I have a panel with some controls on it.
My goal is that when a certain button is clicked, the original panel on the bottom will be replaced with another panel that contains controls which could be created before the program starts, meaning these controls in the panel do not need to be created dynamically. The replace action would be executed by setting each panel's visible field to it's matched value.
I have thought of two ways of doing this - either creating the new panel (and it's controls) dynamically and adding it to the form instead of the original, or creating the new panel in another form and when the relevant button is clicked the panel being taken from that form and added to the required form (by creating an instance of the new form and making it's panel's modifier public). The "side form"'s purpose is only to create that panel, it has no functionality of it's own.
The advantages of creating the new panel dynamically:
There is no need to create a zero-functionality form.
The advantages of creating the new panel in a side form:
It's very clear which controls are added to the new panel and their positions.
It's very easy to set the location and other fields of the controls in the new panel.
Which way is better?
Thanks!
Have you considered TabControl? That seems a good fit for your needs. Other controls I can think of are StackPanel (Can be fairly easily done for Windows Forms) or OutlookBar like control (again a user control).
Simplest and quickest way seems to be TabControl.
Edit:
SideForm is a different windows form I suppose. So if you are thinking to make controls public and then change their visibility etc, please don't. Use delegates to handle SideForm's events in MainForm.
As you mentioned, there is no room for more controls, I would suggest more screens rather than just one. Having said that I do not know much about your current UI design and functionality so it's up to you.
I would say having the controls hidden and just playing with the Visibility is fine. This means that you do not have to worry about positioning of controls, anchoring and docking at runtime. The problem could well be loading of form. Having huge number of controls having a lot of data associated with them may slow things down.
IMO the best way would be to utilise user controls for this purpose. Simply create one user control per panel you wish to show/hide and place your controls inside. This way you will have both: the designer and the "extra form" you wanted.

SplitContainer's panel as the MDI parent for other forms

I have got a control with a Splitcontainer added. I want to place
another forms on the second panel (Panel2). However, it is not possible to
set the MDIParent property of a brand new form to Panel2.
Thus, the question is - how can I set the SplitContainer's panel as the MDIParent for another controls?
Thank you in advance for the clues!
cheers
If you want to make Panel-Splitter-MdiClient Form see panel and MDI in c#
An MDIParent can only be another Form. What you need to do is set TopLevel to False on the child Form. Then you can add it to any control just like it was any other control (by adding it to the parent control's Controls collection). However, it won't work like it does in an MDI container (as in you won't be able to minimize or maximize it).
If your intent is to use the splitcontainer to load different subforms this may help. Instead of using WinForms, you could use classes derived from panels containing all the widgets that a normal WinForm would have. To display them, simply add them to your splitcontainer's Panel2 controls collection.
Some events and methods to keep in mind are:
subformPanel.ParentChanged (do some initialization and subscribe to any parent events)
subformPanel.ParentChanged (do some cleaning up and unsubscribe to parent events)
Parent.Controls.Remove (destroy the subformPanel)

How do I make a single control appear across many tab control pages?

I have a WPF tabcontrol with 3 tabs. On the top of the first page is a scrollviewer with a couple buttons which make up a menu of common tasks (save, load etc). I would like this scroll viewer to appear at the top of every tab. Is it possible to do this without simply copying and pasting the code to every tab?
You can make a custom control that contains the UI and logic for the buttons, and then include that control on each tab. The best way to do this is to create a subclass of ScrollViewer, and in the XAML define each of the buttons. On each of your tab pages you can create and create an instance of your new subclass.
This will result in a different instance of your class on each page, but the logic for the buttons will only exist in the code once.
You could implement the scroll viewer and buttons outside and on top of the tabcontrol.

Categories