Embed a form onto a tabcontrol in windows forms - c#

I have a tab control in a windows form and I want to be able to click on a tab and in the body area of the tab I want it to display another form as an embedded component. Is this possible? If so, can someone please provide an example or a link to an example of how to accomplish this?

You are probably looking for Tabbed MDI Child Forms

You can embed a Form but it's not the best choice.
Better place the contents on UserControls and add that to the TabPage.

Set your MainForm (Parent) as IsMDIContainer = true;
Create an instance of the ChildForm and call this function:
FormChild frmChild = new FormChild();
AddNewTab(frmChild);
Copy this Function to your code:
private void AddNewTab(Form frm)
{
TabPage tab = new TabPage(frm.Text);
frm.TopLevel = false;
frm.Parent = tab;
frm.Visible = true;
tabControl.TabPages.Add(tab);
frm.Location = new Point((tab.Width - frm.Width) / 2, (tab.Height - frm.Height) / 2);
tabControl.SelectedTab = tab;
}

I think the other answer has the right idea; Tabbed MDI is probably what you want.
There is an approach where you create a UserControl that has the same content as the form and use that on the TabPage.
TabPage myTabPage = new TabPage(sometext);
myUserControl = new myUserControlType();
myUserControl.Dock = DockStyle.Fill;
myTabPage.Controls.Add(myUserControl);
myTabControl.Add(myTabPage);
http://bytes.com/topic/c-sharp/answers/270457-can-i-add-form-tabpage goes into more detail; but I'd look at the MDI stuff first.

If you do not want to use MDI, you can try to put everything from desired form to user control and add this user control in both form and tab.

Related

Open ChildForm with owner permission

I am trying to insert a form inside another form. I used .Controls.Add(form) and it works. My problem is that I have to declare the owner and to do that I have to set the toplevel to true but if I set the toplevel to true I get stuck with the control.add bacause telling me that it is impossible to add a top level control to a control.
How can I do?
public void openChildForm(Form childForm)
{
if (activeForm != null) activeForm.Close();
activeForm = childForm;
childForm.TopLevel = true;
childForm.TopMost = true;
childForm.FormBorderStyle = FormBorderStyle.None;
childForm.Dock = DockStyle.Fill;
this.Controls.Add(childForm);
panelChildForm.Tag = childForm;
childForm.Owner = this;
childForm.BringToFront();
childForm.Show();
}
thankyou in advance,
Davide.
For me it's a bit strange what you are trying to do. A form is kind of window. Why would you add a window in another window? Don't you want to create a user control and to display that user control in your existing form?
There isn't enough information to go on from the original question you've posted, but it looks like you're trying to create the kind of functionality that a sidebar page menu might offer. In that situation, clicking on a button on the sidebar changes the window that's currently being displayed. Perhaps this question about sidebar navigation would relate to what you're saying? There's some information also available if you try searching "winforms sidebar navigation," such as this post about creating navigation in winforms, and there are a lot more linked from this question.
I'm just taking a guess at what you're trying to do here. Perhaps if you want to refine the answers you're getting, the original question should include a link to a github sample project, or at least a bit more of an explanation of what the target is.
It looks like you are working with WinForms.
Look at MDI Forms (Multiple-Document Interface Forms)
You can handle this by setting the parent form property IsMdiContainer = true
and then the child form childForm.MdiParent = parentForm;.
Here is a small example:
Add a menuStrip to the parentForm and set the parent form MainMenuStrip = menuStrip. Add a menu item and add some code to the menuStrip_ItemClicked event.
private void menuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
if (e.ClickedItem.Text == "New Child")
ShowNewForm(sender, e);
}
private void ShowNewForm(object sender, EventArgs e)
{
Form childForm = new Form();
childForm.MdiParent = this;
childForm.Text = "Window " + childFormNumber++;
childForm.Show();
}
You should end up with this behaviour.
If you want to access other forms within the same application you can use a loop and find a form using Application.OpenForms or find a form using either Application.OpenForms["form1"] or Application.OpenForms[0]
If you use MdiForms you can find child forms using parentMdiForm.MdiChildren

How to add a form inside a specific layout?

I am not that PRO using WindowsForms because I do not use them too much but right now I am developing a Inventory system so this is my approach(design):
I have used 3 FlowLayoutPanels, I am trying to show various Forms inside FlowLayoutPanel3 when clicking specific button in the left panel the flowlayoutpanel3 will show/trigger the specific form.
What I have tried?, well I have read that I can use MDI container and stuff like that but I noticed that it is a different approach because I do not want to show a separate window, just just inside the flowlayoutpanel3 and maximized.
I have been googling solutions but could not find a solution. * OR is there any item in the toolbox to perform this kind of stuff?*
PD: I am using VS2019 and C#.
Create the new form, set it's TopLevel to false, add it to the panel controls and maximize it:
var frm = new Form(); //Or whatever form type you want to use.
frm.TopLevel = false;
frm.FormBorderStyle = FormBorderStyle.None;
flowLayoutPanel3.Controls.Add(frm);
frm.WindowState = FormWindowState.Maximized;
You may need also to make it visible
frm.Visible = true;

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();
}

open many forms in a panel by link labels in desktop application in c#

My problem is ..I am able to add form in panel BY.
Form2 fm2 = new Form2();
fm2.TopLevel = false;
fm2.Dock = DockStyle.Fill;
panel1.Controls.Add(fm2);
fm2.Show();
but when i try to remove form from panel. By
panel1.Controls.Remove(fm2);
Nothing workss.This code note remove FORM from panel..
Actually your code should work even without closing form explicitely. Make sure fm2 points to form which you have added:
Form2 fm2 = panel1.Controls.OfType<Form2>().First();
panel1.Controls.Remove(fm2);
UPDATE: Again, you should remove exactly same instance of Form2 which you have added to panel. Creating new instance and removing it from panel does nothing, because that new instance was not added to panel and original instance still exist in panel.
I suggest you to remove all controls from panel, if you just want to use it as host to your forms. Just call panel1.Controls.Clear().
You could try to close form before remove it from Panel:
fm2.Close(); //Or fm2.Visible = false;
panel1.Controls.Remove(fm2);

Displaying a form within another form

I am building a web browser application using c# and the awesomium web framework . I have a form containing a dock panel within which I would like to display another form that holds the awesomium web-control . Basically the parent form facilitates creating tabs and the one with the webControl has the browsing engine and is rendered within the tabs .
Is this possible ? If yes , can you give me some tips on how to.
You can embed a form in another control if set TopLevel = false;
private void EmbedForm()
{
Form f = new Form();
f.TopLevel = false;
f.BackColor = Color.White;
f.FormBorderStyle = FormBorderStyle.None;
f.Dock = DockStyle.Fill;
f.Visible = true;
panel1.Controls.Add(f);
}
Move common UI content to UserControl and use it in a both form.
It is a most common practice.

Categories