Copy TabControl Tab - c#

I searched the internet for this but i couldn't find how to do it with C#
What i am trying to do is make it so that when i click on my NewTab button, a new tab appears with the same controls that were on the first tab. I saw some information on how to add a UserControl to your form, but C# doesn't have anything like that.
And for everyone who would say "Post your code", i don't have any, so don't bother saying that, the only code i have is the code for the program and that wouldn't help anyone.

EDIT
I have rewritten my solution to use reflection.
using System.Reflection;
// your TabControl will be defined in your designer
TabControl tc;
// as will your original TabPage
TabPage tpOld = tc.SelectedTab;
TabPage tpNew = new TabPage();
foreach(Control c in tpOld.Controls)
{
Control cNew = (Control) Activator.CreateInstance(c.GetType());
PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(c);
foreach (PropertyDescriptor entry in pdc)
{
object val = entry.GetValue(c);
entry.SetValue(cNew, val);
}
// add control to new TabPage
tpNew.Controls.Add(cNew);
}
tc.TabPages.Add(tpNew);
Some information can be found here.
http://www.codeproject.com/Articles/12976/How-to-Clone-Serialize-Copy-Paste-a-Windows-Forms

Your best bet would be to look at this article:
Code Project
Then apply the following code to add the cloned control (this would be in your button click handler code (based on article):
private void button1_Click(object sender, EventArgs e)
{
// create new tab
TabPage tp = new TabPage();
// iterate through each control and clone it
foreach (Control c in this.tabControl1.TabPages[0].Controls)
{
// clone control (this references the code project download ControlFactory.cs)
Control ctrl = CtrlCloneTst.ControlFactory.CloneCtrl(c);
// now add it to the new tab
tp.Controls.Add(ctrl);
// set bounds to size and position
ctrl.SetBounds(c.Bounds.X, c.Bounds.Y, c.Bounds.Width, c.Bounds.Height);
}
// now add tab page
this.tabControl1.TabPages.Add(tp);
}
Then you would need to hook the event handlers up. Will have to think about this.

I know it's an old thread but I just figured out a way for myself and thought I should share it. It's really simple and tested in .Net 4.6.
Please note that this solution does not actually create new controls, just re-assigns them all to new TabPage, so you have to use AddRange each time you change tabs. New tab will show the exact same controls, content and values included.
// Create an array and copy controls from first tab to it.
Array tabLayout = new Control [numberOfControls];
YourTabControl.TabPages[0].Controls.CopyTo(tabLayout, 0);
// AddRange each time you change a tab.
YourTabControl.TabPages[newTabIndex].Controls.AddRange((Control[])tabLayout);

Related

Attempting to get child control of currently selected tab

I'm attempting to get the TextEditor control off of the currently selected tab in my tab control. The tabs and text editors are created dynamically so simply referencing the text editor isn't an option. I've searched far and wide and so far, no answer has helped me.
The following code works for Winforms, but not WPF:
var currentTextEdit = tabControl.SelectedTab.Controls.OfType<TextEditor>().First();
Is there something along these lines that perhaps I'm missing?
This is how I'm creating each tab and adding a TextEditor control to each tab created:
TabControl itemsTab = (TabControl)this.FindName("tabControl");
TextEditor textEdit = new TextEditor();
Then to create the new tab and add the text editor:
TabItem newTab = new TabItem();
newTab.Content = textEdit;
itemsTab.Items.Add(newTab);
Further down in the code I get the currently selected tab like so:
TabItem ti = tabControl.SelectedItems as TabItem;
And using the GetChildOfType extension method, I'm attempting to get the current text editor like so:
var currentTextEditor = ti.GetChildOfType<TextEditor>();
This code returns the NullReferenceException:
File.WriteAllText(saveF.FileName, currentTextEditor.Text);
Indeed I wrote a not right thing in my comment. TabControl works in a little different way in comparing with other controls. It has a collection of TabItems. TabControl can show every header of each TabItem which belongs to its collection.
At the same time TabControl "grabs" the selected TabItem's content and add it to its ContentPresenter (it is called PART_SelectedContentHost - just use ILSpy).
So, returning to your issue, you have to search for your TextEditor directly in the TabControl. Then you can use this code:
TabControl itemsTab = (TabControl)FindName("tabControl");
TextEditor currentTextEditor = itemsTab.GetChildOfType<TextEditor>();
if (currentTextEditor != null)
{
File.WriteAllText(saveF.FileName, currentTextEditor.Text);
}
You should always check if the object you obtain from GetChildOfType<T> method is not null, since if GetChildOfType<T> cannot find a control whose type is T, it returns null.
As I told in my previous comment you can find here the code of GetChildOfType<T>.
I hope this answer can help you.
When working with WPF I usually use the method
var currentTextEdit = tabControl.SelectedTab.Children.OfType<TextEditor>().First();

Add tooltip to a dynamically created tabpage

I'm having a difficult time trying to work with tooltips.
I'm trying to add a tooltip with different text to each dynamically created tab in a tabcontrol.
It's important to note that this tab is created from a form that contains a docked form where the tabcontrol is in.
This is, a main Form with a docking area, in where I have docked a results form, which contains an - initially empty - tabcontrol.
When you start the application this results form doesnt exists, I also create it dynamically whenever the user press certains parts of the main form, each one created as a new tab in the results form tabcontrol.
This is how I generate the tabs:
generateResultForm();
TabPage newtp = new TabPage("Nuevo paciente")
_result.TabControl.TabPages.Add(newtp);
newtp.Name = setTabName("np");
Now, I've tried putting a tooltip in the results form, then tried to first generate the tooltip by adding below something like _result.ResultsTooltip.SetToolTip(newtp, "Creación de un nuevo paciente.");, which didn't work. Then, since once the tab is created, it becomes selected, I tried to add it in the results class by something like WorkareaTooltip.SetToolTip(tabControl.SelectedTab, "Cosas"); in the selectedindexchange event from the tabcontrol.
I don't think it would have been a great solution, but I don't know what else to try.
Of course the tabcontrol has its ShowToolTips property set to true.
If anyone could help me that'll be great.
Thanks for reading, and sorry if there are any language mistakes :)
//EDITED
This is the code i'm actually using (and doesn't work)
TabPage newtp = new TabPage("Nuevo paciente");
_workareaform.TabControl.TabPages.Add(newtp);
newtp.Name = "np";
var tooltip = new ToolTip();
tooltip.SetToolTip(newtp, "Creación de un nuevo paciente.");
Now, it doesn't work, might be because of the whole configuration.
Just to be clear, this tab is in a TabControl which is in a Form docked into a dockContainer in another Form.
Here is an image if it.
http://i.imgur.com/fVz6e06.png
As you can see, no tooltip at all.
Have you tried setting ToolTipText property as shown below?. It worked for me.
_result.TabControl.ShowToolTips = true;
TabPage newtp = new TabPage("Nuevo paciente");
_result.TabControl.TabPages.Add(newtp);
newtp.ToolTipText = "this is tooltip";
If you're working with another form you need to reference the other form TabControl
In this sample I create a Form1 instance (with TabControl in it) from my Form2 and then add page and tooltip.
private void Form2_Load(object sender, EventArgs e)
{
//instantiate another form
var f1 = new Form1();
//find tabcontrol on new form
var tc = (TabControl) f1.Controls["tabControl1"];
//create page
TabPage newtp = new TabPage("Nuevo paciente");
newtp.Name = "Paciente1";
tc.TabPages.Add(newtp);
//add tooltip
var tt1 = new ToolTip();
tt1.SetToolTip(newtp, "paciente 1 tooltip");
//show other form
f1.Show();
}

Is it possible to add more than one panel in same location

i'm using telerik control.
So i want to ask,
In winforms application ,Is it possible to add more than one panel in same location and display one at a time just like show/hide property.
Make sure you have placed all panel control in same container or form. then you can use Visible property to show and hide panel. BringFront and SendToBack function will be used to bring panel on top or send it to back. If you have placed any panel in another panel then that will be disappeared when you Hide parent panel. So, Make sure all panels' parent control must be same. To determine the parent control simply select that panel and press escape key to select their parent.
private void LoadPanels()
{
panel1.Location = new Point(10,10);
panel2.Location = new Point(10,10);
panel3.Location = new Point(10,10);
panel4.Location = new Point(10,10);
panel5.Location = new Point(10,10);
VisiblePanel("panel1");
}
private void VisiblePanel(string panelName)
{
string[] panels = new string[]{"panel1","panel2","panel3","panel4","panel5"};
for (int i=0;i<panels.Length;i++)
this.Controls[panels[i]].Visible = (panels[i] == panelName);
this.Controls[panelName].BringToFront(); //Not required you can remove this line.
}
Here's a slightly different approach you might want to consider...
Are you wanting to be able to programmatically select the contents of a rectangular area at runtime, selecting among various controls to display? If so, you could use a custom TabControl which has its tabs (not the pages) hidden.
Then you can select which page is displayed by programmatically changing its SelectedIndex property at runtime.
Doing it like this means that your form editor will show a normal tab control, which allows you to much more easily add the content to each page - but at runtime the tabs will be hidden from the user; they will just see the contents of the currently selected page.
See Hans Passant's answer here for how to create such a custom tab control.
(However, you might also want to override the OnKeyDown for the custom tab control in order to ignore Ctrl-Tab.)

Tab control like chrome browser in Silverlight Application

I have seen this one before. We want a dedicated tab item with a '+' symbol on it. Like chrome browser in Silver light Application.
There is always a tab item appears at the end half visible and once you click on it, it turns into a complete tab item.
That was a little though to answer :P
Here it goes:
Create a class called LinqToVisualTree. You can find it at the end of this post, along with an explanation of what it does. Basically, it lets you query your Visual Tree through LINQ.
To add anything to the tabs row in a TabControl, you need to manipulate the TabPanel, which holds the "buttons" of tabs. TabPanel is in the System.Windows.Controls.Primitives namespace, so refer to it.
The easiest way to get the TabPanel I've found is to name at least one of your TabItems and do this:
using System.Windows.Controls.Primitives; // Contains TabPanel
using LinqToVisualTree;
void AddPlusButton() {
// Creates a button beside the tabs
var button = new Button()
{
Content = "+",
IsTabStop = false // To prevent keyboard press
};
// Links the Click with the "new tab" function
button.Click += new RoutedEventHandler(btPlus_Click);
// *** HERE IS THE TRICK ***
// Gets the parent TabPanel in the Visual Tree and cast it
var tabpn = tabItem1.Ancestors<TabPanel>().FirstOrDefault() as TabPanel;
// Links the button created
tabpn.Children.Add(button);
}
Here's the method for the plus button:
void btPlus_Click(object sender, RoutedEventArgs e)
{
// Creates a new TabItem
var ti = new TabItem();
ti.Header = "TabAdded";
ti.Content = new TextBlock() { Text = "Tab content!" };
// Links it
tabControl.Items.Add(ti);
}
That's it! Tip: I've just find out about the TabPanel class using Silverlight Spy. Searching on Google, I could just find methods of doing this by changing the Template Style from the TabControl.
Best regards!

Programmatically change the tab order

How do I programmatically reorder the tabs in a TabControl? I need to sort the tabs depending on some conditions.
If it's possible to do the reordering through the designer, i guess we must be able to do it through code at runtime too.
Create a new Form.
Create a new TabControl.
Notice it has two TabPage controls, and TabPage1 is the first tab.
In form's Load event, add
this.TabControl1.TabPages.Remove(this.TabPage2)
this.TabControl1.TabPages.Insert(0, this.TabPage2)
Run the form.
Notice TabPage2 is now the first tab.
Note that if you fail to remove the tab page, it will still show at its old location. In other words, you will have two tabs of the same tab page.
You have to redefine your tab page collection, in order to change the index of your tab pages.
The following lines of code can also do the trick, this kind of solution also works for other kind of data that has no direct way of sorting:
- convert to a list
- sort the list
- put it back
public static void Sort(TabControl tabControl)
{
var tabList = tabControl.TabPages.Cast<TabPage>().ToList();
tabList.Sort(new TabPageComparer());
tabControl.TabPages.Clear();
tabControl.TabPages.AddRange(tabList.ToArray());
}
public class TabPageComparer : IComparer<TabPage>
{
public int Compare(TabPage x, TabPage y)
{
return string.Compare(x.Text, y.Text);
}
}
thelost is right. Below is a quick sample code.
I have a tab control with 2 tabs (tabpage1, tabpag2)
Then I declare two tabpages and store the existing tabs in the tabcontrol in it.
abPage tbp1 = new TabPage();
TabPage tbp2 = new TabPage();
tbp1 = tabControl1.TabPages[0];
tbp2 = tabControl1.TabPages[1];
Then on a button click I removed the tabs using
tabControl1.TabPages.Remove(tabControl1.TabPages[0]);
Now if you want to change the order then you will have top add it to the tab in that order
//Order changed
tabControl1.TabPages.Add(tbp2);
tabControl1.TabPages.Add(tbp1);
Note: This is untested quick code.
Go inside Designer.cs file
There you will find
/// [Your TabControl Name]
yourTabControl.Controls.Add(yourPage1);
yourTabControl.Controls.Add(yourPage2);
yourTabControl.Controls.Add(yourPage3);
The adding order is your tabpages' order in the tabcontrol. Change the order how you wish. Remove and Add functions of TabControl.Controls will help you as Shoban answered.
try this after Initilizacomponent(). this code will give you freedom to change it programmatically in .cs file.
this.tabReceive.Controls.Remove(this.metroTabPage4);
this.tabReceive.Controls.Remove(this.metroTabPage5);
this.tabReceive.Controls.Add(this.metroTabPage4);
this.tabReceive.Controls.Add(this.metroTabPage5);
Sometimes I have tabControls with several tabPages. At runtime tabPages are made invisible (by removing them) an added later again.
After this the tabPages may be in wrong order. I use this code to redorder them again:
public void ReorderTabPages()
{
// Demo code to reorder tabControl with tabPages
// where some tabPages may be unwanted at the moment
// caution: events like "SelectedIndexChanged" does fire!
// List of wanted tab pages
List<TabPage> wantedTabPages = new List<TabPage>();
// remember the current selected tab
TabPage currentTabPage = this.tabControl.SelectedTab;
// check if all possibly active tabs are currently visible
// check it in the order they should be displayed
// after that they are in the correct order in "wantedTabPages"
if (this.tabControl.TabPages.IndexOf(this.tabPage_01) >= 0)
wantedTabPages.Add(this.tabPage_01);
if (this.tabControl.TabPages.IndexOf(this.tabPage_02) >= 0)
wantedTabPages.Add(this.tabPage_02);
if (this.tabControl.TabPages.IndexOf(this.tabPage_03) >= 0)
wantedTabPages.Add(this.tabPage_03);
if (this.tabControl.TabPages.IndexOf(this.tabPage_04) >= 0)
wantedTabPages.Add(this.tabPage_04);
this.tabControl.SuspendLayout();
// remove all currently visible tab pages
for (int i = this.tabControl.TabPages.Count - 1; i >= 0; i--)
this.tabControl.TabPages.RemoveAt(i);
// add the tabPages in the correct order
foreach (var wantedPage in wantedTabPages)
this.tabControl.TabPages.Add(wantedPage);
// restore the currently selected tabPage
this.tabControl.SelectedTab = currentTabPage;
this.tabControl.ResumeLayout();
}

Categories