|
When the tabpage added the form in it, and the tagpage resize, the form can't fill the tabpage. i haved setup the form.dock = dockstype.fill, but seem that is no usefull. any one can gave me clue?
Examples.
Form2 form = new Form2();
TabPage tbPage = new TabPage();
tbPage.Text = "test";
tbPage.Name = "page1";
tbPage.BackColor = SystemColors.ControlText;
Panel panel = new Panel();
panel.Dock = DockStyle.Fill;
tbPage.Controls.Add(panel);
tabControl.TabPages.Add(tbPage);
form.TopLevel = false;
form.Parent = panel;
form.Show();
tabControl.SelectedTab = tbPage;
|
|
|
|
|
Well, you can achieve this, and here's an example:
public partial class Form1 : Form
{
public Form1(){ InitializeComponent();}
private Form form2 = new Form();
private Panel pnl1 = new Panel();
private Button btn1 = new Button();
private TabPage tbPg1 = new TabPage();
private void Form1_Load(object sender, EventArgs e)
{
pnl1.BackColor = Color.SlateBlue;
pnl1.Padding = new Padding(32);
form2.TopLevel = false;
form2.FormBorderStyle = FormBorderStyle.FixedSingle;
form2.ControlBox = false;
form2.MaximizeBox = false;
form2.MinimizeBox = false;
form2.Text = "";
form2.BackColor = Color.Azure
form2.Resize += (ob, ea) =>
{
btn1.Location = new Point((form2.Width/2) -(btn1.Width/2),
(form2.Height/2) - (btn1.Height/2));
};
form2.Parent = pnl1;
form2.Dock = DockStyle.Fill;
form2.Visible = true;
btn1.AutoSize = true;
btn1.BackColor = Color.WhiteSmoke;
btn1.Text = "button on the Form";
form2.Controls.Add(btn1);
btn1.Click += (ob, ea) => { MessageBox.Show("Button on Form Clicked"); };
tbPg1.BackColor = Color.LightSteelBlue;
tbPg1.Text = "Test Form in Panel in TabPage";
tbPg1.Padding = new Padding(32);
tbPg1.Controls.Add(pnl1);
pnl1.Dock = DockStyle.Fill;
tabControl1.Controls.Add(tbPg1);
}
}
But, I wonder: why are you inserting a Form in a Panel which is then inserted in a TabPage ?
In general, you should not be using a Form inside any other Container Control, including another Form: Forms are "heavy-weight" .NET objects, and really are not meant to be used this way.
Yes, .NET WinForms will let you "get away with it," but that does not mean it is good practice. If you really need to design the contents of TabPages in their own designer, outside of the TabControl itself, why not use a UserControl, a "lighter-weight" object ?
To change from using a Form, to using a UserControl in the above example, is as simple as making these changes to the code:
1. private UserControl form2 = new UserControl();
... of course, being a good programmer, you will change form2's name to something more mnemonic !
2. comment out the following four lines in the Form_Load EventHandler, like this:
//form2.FormBorderStyle = FormBorderStyle.FixedSingle;
//form2.ControlBox = false;
//form2.MaximizeBox = false;
//form2.MinimizeBox = false;
If you share something about your design goals here, I'd be happy to respond with suggestions, and I am sure that other CP Members will also respond.
yrs, Bill
|
|
|
|