Add tabcontrol to dynamically created tab control in winforms - c#

I've a tabcontrol on my page with 2 tabs. Now I want to create another tabcontrol dynamically and want to add the existing tab control to dynamically created tab control tabs.
Is is possible? I'm not able to add this.
Here is my code:
TabControl tbdynamic = new TabControl();
TabPage tbpaagedynamic = new TabPage();
tbpaagedynamic.Controls.Add(statictabcontrol);
tbdynamic.TabPages.Add(tbpaagedynamic);
Any idea?

Yes, it is posiible.
Add dynamic tab to Form :
this.Controls.Add(tbdynamic);
example
TabControl tbdynamic = new TabControl();
tbdynamic.Height = 200;
tbdynamic.Width = 200;
TabPage mPage = new TabPage();
mPage.Text = "Test Page";
tbdynamic.TabPages.Add(mPage);
mPage.Controls.Add(statictabcontrol);
statictabcontrol.Top = 0;
statictabcontrol.Left = 0;
this.Controls.Add(tbdynamic);

Just add the bringToFrontMethod() at the end of adding it to your Window.
tbdynamic.BringToFRont();

Related

How to make a Dynamic FlowControlPanel visible in C# Winforms

I'm trying to create a dynamic flowcontrolPanel and make it visible after adding controls to it. However the problem now is, the controls are added successfully but the panel is not visible on the screen. The below piece of code is a sample which I have tried form my side:
List<object> lstChild = lstFromChild;
//creating an instance for the flowlayoutpanel
FlowLayoutPanel objflowParent = new FlowLayoutPanel();
objflowParent.Name = "flowLayoutParent";
objflowParent.Location = new Point(380,155);
objflowParent.Size = new Size(800, 800);
objflowParent.BackColor = Color.DarkCyan;
objflowParent.BorderStyle = BorderStyle.Fixed3D;
objflowParent.Dock = DockStyle.Fill;
objflowParent.AutoSizeMode = AutoSizeMode.GrowAndShrink;
objflowParent.Size = new Size(300, 30);
objflowParent.SuspendLayout();
//Adding Controls to the flowlayoutpanel
foreach (Control item in lstChild)
{
objflowParent.Controls.Add(item);
}
objflowParent.ResumeLayout(false);
//Tried to hide a datagrid available in the screen to check if it is hiding the panel
dgMainGrid.Hide();
this.Show();
Note: lstChild is the list carried form another form with controls loaded in it.

c# adding groupbox when create a new tabpages

i just created an application using C# in Visual Studio. I am wondering is it possible to create a new instance of group-box with text-box and label as shown in the picture when user click add a new tab button(+). For example when the user opens a google chrome, whenever the user opens the new tab, it will show the same search box on each page.
As for now I am able to create new tab page when the user clicks the add button but the new tab page will empty.
You need to use "using System.Reflection;"
And is basically this:
TabPage tpOld = tabControl1.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);
}
tpNew.Controls.Add(cNew);
}
tabControl1.TabPages.Add(tpNew);
This code will copy all the controls of the old tabPage and put them into a new tabpage with the exact controls, positions, sizes of the old one.
Credits here: here.

How do I add UI Children to an empty WPF-Page?

I'm having a MainWindow which is a NavigationWindow. Inside this MainWindow I want to switch in several Pages. One of the Pages has to be dynamically generated by Code, not by XAML. When I had a normal window before, I could add UI components like this:
Button b = new Button();
b.Content = "Hello";
this.AddChild(b);
Or if I added (for example) a StackPanel with this method first, I could add Children to this StackPanel with:
myPanel.Children.Add(b);
However, the Page Class doesn't have a Children Attribute or a AddChild Method.
The only method I found so far is:
AddVisualChild(b);
The page shows but I don't see any components which I added with this method.
So how do I add Children to a WPF-Page correctly?
First of all, the Window.AddChild will throw an exception when you add more than one object to it, because Window is a ContentControl. Page allowes only 1 child .So you set the child using the Page.Content property. So you want to add a container to your Page and then add children to the container.
For example:
Button b = new Button();
b.Content = "Hello";
StackPanel myPanel = new StackPanel();
myPanel.Children.Add(b);
this.Content = myPanel;
I'm not really sure if this works for you but you could try to set the content of a page to a user control. E.g. use a StackPanel and add all the children to it. After that you set the content of the Page to the Stackpanel.
Here is an lazy example in the constructor of a MainWindow.
public MainWindow()
{
InitializeComponent();
StackPanel panel = new StackPanel();
Button b1 = new Button {Content = "Hello"};
Button b2 = new Button {Content = "Hi"};
panel.Children.Add(b1);
panel.Children.Add(b2);
Page page = new Page {Content = panel};
this.Content = page;
}

add a Tab to Tab control with its content

I am working on an ERP project. it is a button on treeView box and when it is clicking on a button in treeView it must create a Tab with its content (content which is defined-designed before).
I can add a tab programically but how can I design its content?
Adding this to your click event of your treeview should do what you are after:
var contentControl = new ContentControl (); //This is what we will put all your content in
contentControl.Dock = DockStyle.Fill;
var page = new TabPage("Tab Text"); //the title of your new tab
page.Controls.Add(contentControl); //add the content to the tab
TabControl1.TabPages.Add(page); //add the tab to the tabControl
To your project, add a new UserControl called ContentControl (or whatever you need, just using this in my example), and fill it with all the contents you want to appear in your tab.
You have few solutions, the simplest one is to create TabPage, create desired Controls, set up their properties (i.e. Size, Location, Text etc.), add them to the TabPage and then add TabPage to the TabControl.
TabPage tp = new TabPage();
//create controls and set their properties
Button btn1 = new Button();
btn1.Location = new Point(10,10);
btn1.Size = new System.Drawing.Size(30,15);
//add control to the TabPage
tp.Controls.Add(btn1);
//add TabPage to the TabControl
tabControl1.TabPages.Add(tp);
the second solution is to override TabPage in your class, for instance CustomTabPage where you will set up controls in the constructor of the class. Then, when you want to add new TabPage, create your CustomTabPage instance and add it to the TabControl.
public class CustomTabPage : TabPage
{
public CustomTabPage()
{
//create your Controls and setup their properties
Button btn1 = new Button();
btn1.Location = new Point(20, 20);
btn1.Size = new System.Drawing.Size(40, 20);
//add controls to the CustomTabPage
this.Controls.Add(btn1);
}
}
//Create CustomTabPage
CustomTabPage ctp = new CustomTabPage();
tabControl1.TabPages.Add(ctp);
the third solution (the best but the most complicated) is to create your desired UserControl with everything you want on it (you can use Designer help), then create an instance of your UserControl, Create a TabPage and add UserControl on the TabPage. Then add TabPage to the TabControl.
public partial class CustomControlForTabPage : UserControl
{
public CustomControlForTabPage()
{
InitializeComponent();
}
}
//Create CustomControl
TabPage tp = new TabPage();
CustomControlForTabPage ccftp = new CustomControlForTabPage();
//set properties you like for your custom control
tp.Controls.Add(ccftp);
tabControl1.TabPages.Add(ctp);
Add a new user control to the project then use the designer to do controls/layout, then when you click all you do is add a new instance of the user control to the tab - probably docked to fill the tab unless your form's size is fixed.

Adding items to PopupMenu

I am trying to find how I can add items to devExpress PopupMenu. I have tried the following:
manager = new BarManager();
listBoxMenu = new PopupMenu(manager);
listBoxMenu.ItemLinks.Add(manager.Items["Remove item"]);
listBoxMenu.ItemLinks.Add(manager.Items["Clear items"]);
As shown here http://documentation.devexpress.com/#WindowsForms/CustomDocument5472 (at the bottom), but it gives me an error saying the item is not initialized.
What is the proper way to add items? I can't find it anywhere.
Edit, here is how I did it:
//Creates the popup menu to be used for the keywords listbox
manager = new BarManager();
listBoxMenu = new PopupMenu(manager);
item = new BarButtonItem(manager, "Copy");
item2 = new BarButtonItem(manager, "Clear Item");
item3 = new BarButtonItem(manager, "Clear All Items");
listBoxMenu.ItemLinks.Add(item);
listBoxMenu.ItemLinks.Add(item2);
listBoxMenu.ItemLinks.Add(item3);
//Adds the seperator on the second item
item2.Links[0].BeginGroup = true;
manager.ItemClick += manager_ItemClick;
Check this code snippet and implement using the same way.
//create popup and manage objects
private DevExpress.XtraBars.BarManager barManager1;
private DevExpress.XtraBars.PopupMenu buttonContextMenu;
DevExpress.XtraBars.BarButtonItem menuButtonExport = new DevExpress.XtraBars.BarButtonItem();
DevExpress.XtraBars.BarButtonItem menuButtonSave = new DevExpress.XtraBars.BarButtonItem();
public TestForm8()
{
InitializeComponent();
barManager1 = new BarManager();
this.barManager1.Form = this;
buttonContextMenu = new DevExpress.XtraBars.PopupMenu(barManager1);
this.buttonContextMenu.Name = "subViewContextMenu";
menuButtonExport.Caption = "E&xport";
menuButtonExport.Id = 1;
menuButtonExport.Name = "menuButtonExport";
menuButtonExport.ItemClick += new ItemClickEventHandler(menuButtonExport_ItemClick);
menuButtonSave.Caption = "S&ave";
menuButtonSave.Id = 2;
menuButtonSave.Name = "menuButtonSave";
menuButtonSave.ItemClick += new ItemClickEventHandler(menuButtonSave_ItemClick);
//add items to barmanager
this.barManager1.Items.AddRange(new DevExpress.XtraBars.BarItem[] {
menuButtonExport,
menuButtonSave
});
//create links between bar items and popup
buttonContextMenu.ItemLinks.Add(barManager1.Items["menuButtonExport"]);
buttonContextMenu.ItemLinks.Add(barManager1.Items["menuButtonSave"]);
//finally set the context menu to the control or use the showpopup method on right click of control
barManager1.SetPopupContextMenu(btnInsert, buttonContextMenu);
}
Ref by step to include popup:
How to: Create a popup menu
How to: Add items to a container bar item (menu)
Populating Popup Menus
BarManager.SetPopupContextMenu Method
Your manager is empty:
manager = new BarManager();
The example you linked to is using a BarManager that was already created: barManager1, which I assume was created in the designer and populated with items.
From their BarManager help page:
After a BarManager has been added to a form/user control, you can create bars and bar commands using context menus right on the form, using the bar manager's Customization Window or its Designer. Please see the Toolbars Customization section, to learn more.

Categories