Setting Panel location on a Form with a Toolbar

P

.pd.

Hello,

I've got a Form with a MainMenu and a Panel. I want the panel to appear
just above the MainMenu bar at the bottom of the form so I set the
Location. This works fine.

If I add a Toolbar to the form, the panel doesn't appear. BUT - if I
stick a button on the Form and have the click event set the location to
the same exact place it then appears.

Can anyone explain this or tell me how to fix it?

Thanks for any help,
..pd.

using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;

namespace MenuHidesControl
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.MenuItem menuItem4;
private System.Windows.Forms.MenuItem menuItem5;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.ToolBar toolBar1;

Panel p;
public Form1()
{
InitializeComponent();

Button b = new Button();
b.Location = new Point(20, 20);
this.Controls.Add(b);
b.Text = "Dock";
b.Click += new EventHandler(b_Click);

/* the red panel appears in the centre just above the menu bar
* but when a toolbar is added, the panel doesn't appear until
* the "Dock" button is clicked. but Dock click event positions
* panel in exactly the same place as the constructor.
*
* huh?
*/

p = new Panel();
p.Size = new Size(128,20);
p.BackColor = Color.Red;

p.Location = new Point((this.Width - p.Width) / 2,
this.Height - p.Height);
this.Controls.Add(p);

}

protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.menuItem4 = new System.Windows.Forms.MenuItem();
this.menuItem5 = new System.Windows.Forms.MenuItem();
this.toolBar1 = new System.Windows.Forms.ToolBar();
this.mainMenu1.MenuItems.Add(this.menuItem1);
this.mainMenu1.MenuItems.Add(this.menuItem2);
this.mainMenu1.MenuItems.Add(this.menuItem3);
this.mainMenu1.MenuItems.Add(this.menuItem4);
this.mainMenu1.MenuItems.Add(this.menuItem5);
this.menuItem1.Text = "one";
this.menuItem2.Text = "two";
this.menuItem3.Text = "three";
this.menuItem4.Text = "four";
this.menuItem5.Text = "five";
this.Controls.Add(this.toolBar1);
this.Menu = this.mainMenu1;
this.Text = "Form1";

}
#endregion
static void Main()
{
Application.Run(new Form1());
}

private void b_Click(object sender, EventArgs e)
{

p.Location = new Point((this.Width - p.Width) / 2,
this.Height - p.Height);

}
}
}
 
G

Geoff Schwab [MSFT]

Hi .pd.

Try moving the location setting to the the load function (Form1_Load) so
that the form has had time to complete its initialization. I have found
that it is typically safer to do this. You should still be able to create
and add the control in the constructor. Simply make Form1_Load do exactly
what b_Click does now and remove the location setting from the constructor.

--
Geoff Schwab
Program Manager
Excell Data Corporation
http://msdn.com/mobility

This posting is provided "AS IS" with no warranties, and confers no rights.
 
P

.pd.

Geoff Schwab [MSFT] wrote on Thu 02 Oct 2003 12:57:21a:
Try moving the location setting to the the load function (Form1_Load)

Thanks Geoff: that worked.

Cheers,
..pd.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top