Can't see controls in GroupBox

G

Guest

Hello everyone,
I have a tab control in which I've created a tab page. I add a GroupBox to
the page and add two buttons to the GroupBox. So far so good, everything
works. Then, I add a second GroupBox below the first, and try to add some
controls to that, but only the GroupBox appears, never the controls. The
first group box continues to display just fine. What could be the problem?:

Code:

// Add the first tab page ("Axes"):
TabPage axesPage = new TabPage("Axes");
tabControl1.TabPages.Add(axesPage);


// Add the controls to the tab page.
// Add the group box to frame the font buttons:
GroupBox fontFrame = new GroupBox();
fontFrame.Location = new Point(axesPage.Left+15, axesPage.Top+10);
fontFrame.Width = 300;
fontFrame.Height = 75;

// Add the fontFrame to the page:
axesPage.Controls.Add(fontFrame);

// Add the "X-axis font" button to the group box:
Button xAxisFontButton = new Button();
xAxisFontButton.Text = "X-axis font...";
xAxisFontButton.Location = new Point(fontFrame.Left + 10, fontFrame.Top+12);
xAxisFontButton.Width = 120;
xAxisFontButton.Height = 35;

// Add the button to the group box:
fontFrame.Controls.Add(xAxisFontButton);

// Add the "Y-axis font" button:
Button yAxisFontButton = new Button();
yAxisFontButton.Text = "Y-axis font...";
yAxisFontButton.Location = new Point(fontFrame.Left + 140, fontFrame.Top+12);
yAxisFontButton.Width = 120;
yAxisFontButton.Height = 35;

// Add the button to the page:
fontFrame.Controls.Add(yAxisFontButton);

// Add the group box to frame the axes labels:
GroupBox labelFrame = new GroupBox();
labelFrame.Location = new Point(axesPage.Left+15, axesPage.Top+105);
labelFrame.Width = 300;
labelFrame.Height = 75;

// Add the labelFrame to the page:
axesPage.Controls.Add(labelFrame);

// Add the axes title labels:
Label xAxisLabel = new Label();
xAxisLabel.Text = "X-axis label:";
xAxisLabel.Location = new Point(labelFrame.Left+25, labelFrame.Top+20);
xAxisLabel.Width = 80;

Label yAxisLabel = new Label();
yAxisLabel.Text = "Y-axis label:";
yAxisLabel.Location = new Point(labelFrame.Left+25, labelFrame.Top+50);
yAxisLabel.Width = 80;

// Add the labels to the group box:
labelFrame.Controls.Add(xAxisLabel);
labelFrame.Controls.Add(yAxisLabel);


- "labelFrame" is visible and properly placed, but the controls that were
added to it are not visible. What is going on here? Thanks!
 
R

Roger Rabbit

I suspect its all to do with relative positioning. At the time this staement
runs what is the value of labelFrame.Top + 50?

yAxisLabel.Location = new Point(labelFrame.Left+25, labelFrame.Top+50);

Remeber that controls are positioned relative to their container.
I think you'll find it equal to

axesPage.Top+105 + 50;

Given label frame only has a height of 75 the labels you input, although
contained, are not visible because they are "y" positioned too deep. You
dont want to position relative to the containing control "axesPage" for
controls contained in "labelFrame".

Also for the sake of readability try using the With keyword. Such that this:
// Add the "Y-axis font" button:
Button yAxisFontButton = new Button();
yAxisFontButton.Text = "Y-axis font...";
yAxisFontButton.Location = new Point(fontFrame.Left + 140, fontFrame.Top+12);
yAxisFontButton.Width = 120;
yAxisFontButton.Height = 35;

becomes this.
// Add the "Y-axis font" button:
Button yAxisFontButton = new Button();
With yAxisFontButton
.Text = "Y-axis font...";
.Location = new Point(fontFrame.Left + 140, fontFrame.Top+12);
.Width = 120;
.Height = 35;
End With

RR
 
G

Guest

Thanks, that's probably it. The "With" keyword looks very handy...but I
don't see any mention of it in my reference (Schildt, "The Complete C#
Reference"). Is it a new feature?
 
D

Diggers

With doesn't exist in C# (although I've yet to find a good argument for
why not). I develop in both VB.NET and C# and I must say I miss it from
C#.

Simon

Oh .. unless its been added to a beta version of C# (ie shipping with
VS 2005)
 

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