Alex said:
Every control has ClientRectangle property. YOu need one returned by
TabPage
And therein lies the problem. The ClientRectangle returned by TabPage is
exactly the same as its Bounds. I have included test code below. It
creates the main window, puts a TabControl in it, a TabPage in that, and a
green control in the tab page. The green control is made 20 pixels less
high and wide - you'll only see it get narrower. Even more obvious,
uncomment the MessageBox lines - you see the TabPage's Bounds and
ClientRectangle are equal!!! (that's gotta be a bug - right???) If I
resize the main window by dragging with the mouse (we can't do this in the
CF), then the client rectangle gets set correctly.
If I am doing something wrong in the code, please let me know, however I
believe that the Bounds and ClientRectangle should never be equal (in this
case).
Is there a workaround?
Hilton
Here's the code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace TestTabRect
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
TabControl tabControl = null;
TabPage tabPage = null;
Control control = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// Create the controls
//
this.tabControl = new TabControl ();
this.tabPage = new TabPage ("Test");
this.control = new Control ();
//
// Create the controls
//
this .Controls.Add (this.tabControl);
this.tabControl.Controls.Add (this.tabPage);
this.tabPage .Controls.Add (this.control);
// Make the control green so that we can see it
this.control.BackColor = Color.Green;
this.SetSize ();
}
protected override void OnResize (EventArgs e)
{
base.OnResize (e);
this.SetSize ();
}
protected void SetSize ()
{
// Make the tabcontrol fill the window
this.tabControl.Bounds = this.ClientRectangle;
// Make the tab page fill the tab control's client rectangle
this.tabPage.Bounds = this.tabControl.ClientRectangle;
// Uncomment these - you'll see the numbers are the same
//MessageBox.Show (this.tabPage.Bounds .ToString (),
"SetSize");
//MessageBox.Show (this.tabPage.ClientRectangle.ToString (),
"SetSize");
// This clearly shows the bug - the green should move in
equally from bottom and right
this.control.Bounds = new Rectangle
(this.tabPage.ClientRectangle.X,
this.tabPage.ClientRectangle.Y,
this.tabPage.ClientRectangle.Width - 20,
this.tabPage.ClientRectangle.Height - 20);
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Text = "Form1";
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
}