Adding a form into TabControlPage in C# Windows Forms Application - c#

I have two forms that I am trying to display on a master form using a tabControl element in C#.
I have had no luck so far. How does one go about it?

In your "Child" form, put all your control in a Panel (let's call it "movingPanel") with its Dock property set to Fill and its Modifiers property set to internal. After creating, the child form in the master form, simply do:
theChildForm.movingPanel.Parent = tabControl1.TabPages["The_tabPage_Name"] ;

If you want to host your form into a tab page I think this part of code helps :
newForm.TopLevel = false;
newForm.ControlBox = false;
newForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
newForm.Dock = DockStyle.Fill;
var newTab = new TabPage()
newTab.Controls.Add(newForm);
this.MainTabControl.TabPages.Add(newTab);
this.MainTabControl.SelectedTab = newTab;
newForm.Show();
I hope this is what you are looking for.

Related

Multiple opened windows forms & C#

I’m trying to design a feature in my C# application. If the user has multiple forms opened, like for example 3 forms. I want to use the click event to position the 3 forms side to side fitting the screen. I can do that if I determined 3 forms for the incident. But I want to apply the feature towards (any) multiple opened forms.. Any help will be much appreciated. Thank you!
Use MDI container for solving this problem,
for example,
Create main form and set it's IsMdiContainer property value to "true",
for adding child forms you may use like this code:
var frm = new ChildForm();
frm.MdiParent = this;
frm.Show();
For changing child forms position:
var a = 0;
var b = 0;
foreach (var form in this.MdiChildren)
{
form.Location = new Point(a, b);
a = a + form.Width + 5;
}
result after click "Side to side position" button:

Form in a form behind my menu strip

I created a Form with menu strip. When I click on a menu item I want to open a new From in my main From.
It works but a part of the form is behind the menu like this :
my code :
WindowDossierProtection wdPr = new WindowDossierProtection();
wdPr.TopLevel = false;
this.Controls.Add(wdPr);
wdPr.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
wdPr.Dock = DockStyle.Fill;
wdPr.Show();
Can you help me ? thanks !!
Use BringToFront() to get your desired result:
WindowDossierProtection wdPr = new WindowDossierProtection();
wdPr.TopLevel = false;
this.Controls.Add(wdPr);
wdPr.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
wdPr.Dock = DockStyle.Fill;
wdPr.BringToFront();
wdPr.Show();
Do you use Visual Studio or something else?
This can be easily done by the Document Outline.
In Visual Studio go to :
View /
Other Windows /
Document Outline
Or use the shortcut:
Ctrl+W, U
Then, choose your form (in design view).
In the Document Outline window you will see all your controls and the order that exist in your form.
You simply have to drag your menuStrip in the end. (After your new form control)
And voila.
(I use VS2010)

c# create a user control on button press, won't show

counters.Add(new Counter());
foreach (Counter con in counters)
{
con.Show();
con.Top = this.Top;
con.Left = this.Left;
}
counter is a very basic UserControl I made with 3 buttons and textbox. I'm trying create a draggable Counter, I can see the counter on the list (counters) but I can't see it anywhere on screen.
I was wondering if new Counter() is enough to create the UserControl on screen.
(i mean to create this "counter" object dynamically)
Control needs to have a parent that hosts the control. The parent control can be either a form or another container control. When you want to add your control to the hosting control, add the instance into the Controls collection of the hosting control like this:
var counter = new Counter();
var form = new Form();
form.Controls.Add(counter);
form.Show();
I think user Controls need to be in some sort of container, for example a flowLayoutPanel or your instance of the Form class itself to visually exist.
You need to call CONTAINERINSTANCE.Controls.Add(con).

how to display form within another form when i click the menu item

i'm new to windows application.can anybody help me.here is my doubt.im having one parent form and it has four menu items. when i click any of one menu item ,it should display another form within that parent form itself. how to do it?
Thanks in advance
According to details you ve provided it seems that you need to use MDI Forms concept in your app. It s very easy to learn and refer to the following links:
http://www.codeproject.com/KB/cs/mdiformstutorial.aspx
How to open a form within a form?
Just include the code in the 2nd link within your menuitem_Click event...
Hope this helps...
There are several ways you could do it.
One simple way for a newcomer is to add the form to the parent form in the designer. Set the visible Property to false (in the properties) so it will not be shown at first when your program is run.
Then you can set the visible property to true when you handle the menu item clicking.
There are code ways to do it too at runtime etc.
Hers an article with stuff about adding controls (and implicitly child forms) at runtime.
Inside your main form, add a panel and then use the below method to display the child form.
private void InitChildForm(Form childForm, Panel parent)
{
childForm.TopLevel = false;
childForm.Parent = parent;
childForm.Dock = DockStyle.Fill;
childForm.Show();
parent.Parent = this;
parent.Dock = DockStyle.Fill;
}

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