create dynamic tabs and use control of tabs in C#.net - c#

I created one emulator for one device, so each time the user clicks on the button I am creating one new form, and each form represents new devices.
now I am looking for another option by which inside my main form I can create multiple tabs (instead of multiple forms) which has controls like button, dropdown, text box, grid view.
and the user can navigate between tabs, create a new tab dynamically, use controls inside of the tab.
any suggestion ??

Just use the TabControl on my 'main form'. and added tab pages at runtime.
tabControl1.TabPages.Add("New Tab");
placed an entire form inside a tab control ...this code will create a new instance of a form and place it in the last tab of a tab control:
frmDevice dev = new frmDevice();
dev.TopLevel = false;
dev.FormBorderStyle = FormBorderStyle.None;
dev.Parent = tabControl1.TabPages[tabControl1.TabCount - 1];
dev.Dock = DockStyle.Fill;
dev.Show();
Reference :https://www.codeproject.com/Answers/5246227/Create-dynamic-tabs-and-use-control-of-tabs-in-csh#answer1

Related

Display Different Windows Within a Panel in a Form (Windows Forms Application)

I am working through an Algorithm book (C#) and am consolidating all of the examples and algorithms into a windows form app. The idea is to have a side menu with buttons to choose which "program" you want to run and to the right of the menu with the buttons the Panel will display the form or user control created for that specific algorithm. For example one of the menu options will be "Area of Parallelogram" when clicked the User Control will show to the right of the selection menu (within a Panel) with the input boxes for height and width, a button to calculate it and an output Textbox for the results. I am kind of stuck on which approach I should use, I tried using multiple Panels layered on top of each other and hide them initially then do a Show() on-click but this proved extremely problematic. I read that using User Controls would allow for better control but am unsure how to proceed. I guess my question is how do I have the User Control display inside the Panel within my Form which displays the programs? I would upload a pic but I do not have the rep.
Rather than instantiate the controls for each application, hide them all, and just show the ones from the selected "application", consider this: design your main selector panel as a placeholder. When the user selects an application, assign the application panel instance in place of the selector panel instance and allow the application to execute to completion. After it exits, restore the placeholder instance.
Bind all the button click event to button1_Click.
Place your panels on top of each other and change the visible properties to false.
Then, use switch-case as below.
Button btn = sender as Button;
switch (btn.Text)
{
case "Triangle":
pnlCircle.Visible = false;
pnlSquare.Visible = false;
pnlTriangle.Visible = true;
break;
case "Square":
pnlCircle.Visible = false;
pnlSquare.Visible = true;
pnlTriangle.Visible = false;
break;
case "Circle":
pnlCircle.Visible = true;
pnlSquare.Visible = false;
pnlTriangle.Visible = false;
break;
}

How to move a control to another tab programmatically in C#?

I have a TabControl which contains some tabs. each tab includes a Word component control which loads Microsoft office word. Every time User opens a new tab, a new Word component control has to be add to it which takes a little time.
Is there a way to move the current Word component control to the new tab programmatically when adding new tabs, so it doesn't have to create a new component class?
Something like this (But Tabs[1] has no Controls)
stcWordTab.Tabs[1].Controls.Add(stcWordTab.Tabs[0].Controls[0])
EDIT
I'm using DotNetBar's SuperTabItem control.
While Tab itself does not has Controls property, it has TabItem.AttachedControl property which is TabControlPanel connected to the tab and this panel hosts your controls.
So your code could looks like
(stcWordTab.Tabs[1].AttachedControl as TabControlPanel).Controls
.Add((stcWordTab.Tabs[0].AttachedControl as TabControlPanel).Controls[0]);
See knowledge base for reference.

TabControl - Loading Default UI Elements in New Tab

I have a TabControl in my application but I want to allow the user to create a new tab, but when the new tab is created it contains some default form controls. All new tabs created will contain the same basic form controls per tab (think NotePad++ where each new tab has a text editor within it).
Is there some sort of design pattern or template I can use so that when a new tab is created it will contain the necessary form controls required by my application?
Just to convey some meaning a little more:
User clicks "New Tab"
New tab is loaded with a text editor and a tree view
User clicks "New Tab" again
New tab is loaded with a text editor and a tree view
... and the processes repeats for any number of tabs that the user requires.
Like the comments suggest you should create a UserControl, lets say you call it TextEditorControl. Then you can add tab pages on a button click using the following method
private void AddTabPage()
{
// Add new tab page and dock fill.
TabPage tab = new TabPage(Path.GetFileName(fullPath));
TextEditorControl editor = new TextEditorControl();
editor.Dock = System.Windows.Forms.DockStyle.Fill;
// Add the new tab to the tab control.
tab.Controls.Add(editor);
fileTabs.Controls.Add(tab);
fileTabs.SelectedTab = tab;
}
I hope this helps.

Can I add already created pages to tab control?

I have a tab control with tabs on it in a windows application. I have already created some pages. Can I make these pages display on tabs of the tab control?
Thanks in advance.
Make Usercontrols of your pages and add them as TabPages
edit:
To add forms to a TabControl, you will first want to set a Panel inside of a page in the TabControl. Then, do the following in the constructor of the form with the TabControl:
FormToLoad formToLoad = new FormToLoad();
formToLoad.TopLevel = false;
panelInTabPage.Controls.Add(formToLoad);
formToLoad.Show();
You may also want to individually style your forms so that they don't show window chrome.

C# making a tab, adding controls to the tab, and then having multiple tabs of that type

TabPage newpage = new TabPage();
Tabs.TabPages.Add(newpage);
newpage.Controls.Add(this.tableLayoutPanel41);
newpage.Location = new System.Drawing.Point(4, 26);
newpage.Name = "AddMaintAgreement" + offset;
newpage.Size = new System.Drawing.Size(736, 523);
newpage.TabIndex = 10;
newpage.Text = "Add Maintenance Agreement";
newpage.UseVisualStyleBackColor = true;
offset++;
Basically thats what i have at the moment, I added the offset in there because i thought it might affect my problem.
Basically, this code here works okay for adding one "addmaintagreement" tab. After that only the latest tab has any controls on it!
Basically I'm stumped. Any help would be appreciated. Thanks.
I think short example should stay here:
TextBox tmpLog = new TextBox(); // create new control of textbox type
tmpLog.Text = "some text here";
TabPage tb = new TabPage("my brand new tab"); //create tab
tabControl.TabPages.Add(tb); // add tab to existed TabControl
tb.Controls.Add(tmpLog); // add textBox to new tab
tabControl.SelectedTab = tb; // activate tab
Derive from TabPage and add the controls you want in that derived class. Then use your derived class instead of TabPage.
Controls can only be parented to one control, but it looks like you are trying to parent your tableLayoutPanel41 in every TabPage instance. You need to create new copies of the controls for each instance of the tab. There are various ways to fix this.
Programmatically create your tab page and its contents multiple times.
Have the contents of your TabPage implemented as a user control that you dock fill on a tab page. Then recreate one of those for each page duplicate your require.
Create a class derived from TabPage that implements your tab page and create new instances of that for each use.

Categories

Resources