Standard Control Locations on a Windows Forms Form

M

Mythran

I have a groupbox I am creating programmatically ... I add textbox controls
to the groupbox. There is a user-defined number of rows and columns for
these textboxes so the groupbox grows in height and width (as does the form)
depending on the total number of textboxes added to the groupbox. My
question is, when I create the GroupBox and TextBox controls, what location
should I place the groupbox onto the form and the textboxes onto the
groupbox?

I see that the Control class has Padding and Margin properties. Usually, if
I see padding, I would set the left of the left-most controls equal to the
padding within the parent control...but for forms, this doesn't seem quite
right. In my development machine, the default left padding for the Form
class is 0 while the default left margin is 3. When I drag a control onto
the form, for testing, it wants to 'snap' into place at the location "12,12"
instead of "0,0". What does the designer use to determine 12,12? Should I
use 12,12 as a hardcoded set of constants or use a property that I am
currently not aware of?

I know, in the long run, this doesn't really matter much for "most"
applications...but would still be good to know :)

Thanks
Mythran
 
Z

Zhi-Xin Ye [MSFT]

Hi Mythran,

Thank you for using Microsoft Managed Newsgroup, I'm Zhi-Xin Ye, it's my
pleasure to work with you on this issue.

As I understand, you want to create a GroupBox dynamically and place it
somewhere on the form, meanwhile, you also want to create a number of
TextBoxes be arranged in columns and rows on the GroupBox.

I would recommend you use a TableLayoutPanel control in the GroupBox to
hold the TextBoxes, the TableLayoutPanel control will arrange the TextBoxes
in a table style.
And for locating the GroupBox control, you can set the GroupBox.Location
property to any position on the form you want.

I write the following sample code for your information.

private void button1_Click(object sender, EventArgs e)
{
GroupBox gb = new GroupBox();
gb.Text = "groupBox1";

TableLayoutPanel tlp = new TableLayoutPanel();
tlp.ColumnCount = 3;
tlp.RowCount = 5;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 3; j++)
{
TextBox tb = new TextBox();
tlp.Controls.Add(tb, j, i);
}
}

tlp.AutoSize = true;
tlp.Dock = DockStyle.Fill;
gb.AutoSize = true;
gb.Controls.Add(tlp);
gb.Location = new Point(10, 50);
this.Controls.Add(gb);
}

Please try my suggestion and let me know whether it makes sense to you.
Have a nice day, Mythran!

Best Regards,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Mythran

Zhi-Xin Ye said:
Hi Mythran,

Thank you for using Microsoft Managed Newsgroup, I'm Zhi-Xin Ye, it's my
pleasure to work with you on this issue.

As I understand, you want to create a GroupBox dynamically and place it
somewhere on the form, meanwhile, you also want to create a number of
TextBoxes be arranged in columns and rows on the GroupBox.

I would recommend you use a TableLayoutPanel control in the GroupBox to
hold the TextBoxes, the TableLayoutPanel control will arrange the
TextBoxes
in a table style.
And for locating the GroupBox control, you can set the GroupBox.Location
property to any position on the form you want.

Thanks for taking a moment to respond, but I already have the groupbox and
textbox creation and initialization complete. What I'm looking for is what
would be the standard location for these on the form? Meaning, using the
Margin and/or Padding properties, what would the standard Left location for
the groupbox ... 0 is too far left (no space between the border of the form
and the border of the groupbox). I basically want to know if there is a
specific calculation for determining the location of the groupbox (and other
controls) that make it the left-most position of all controls on the form.
What does Microsoft use for all their forms? Currently, Visual Studio
*snaps* the controls onto the form with the left of the control at 12...so
should I use a static value of 12, or is there a calculation where Left
would/could be a different value than 12, for other types of forms
(borderless, modal, etc).

Thanks,
Mythran
 
J

Jeff Johnson

As I understand, you want to create a GroupBox dynamically and place it
somewhere on the form, meanwhile, you also want to create a number of
TextBoxes be arranged in columns and rows on the GroupBox.

Yes, that's what the poster ultimately wants to do, but it's not what he's
asking about. He wants to place controls in the same manner that Visual
Studio does when it snaps controls in the form designer, and he wants to
know where Visual Studio gets its default X and Y distances that it uses to
snap controls. Are they hardcoded into the IDE or can they be accessed
programmatically from another application?
 
Z

Zhi-Xin Ye [MSFT]

Hi Mythran,

Thanks for the clarification.

The recommended distance between control and container border is the sum of
the control's Margin property and the container's Padding property values.
For example, the default Margin property value for a control is (3,3,3,3),
and the default Padding property value for a form is (0,0,0,0), but the
Windows Forms Designer gives the form a shadowed Padding value of
(9,9,9,9), so the default recommended left -most position of a control is
3+9=12, and so does the top-most position. The snaplines' distances follow
the same rule of calculation, that's why you see the snapline appears when
you put a control at 12 pixel away from the left border of the form.

For more information about the layout on Windows Forms, you can refer to
these documents:

Walkthrough: Laying Out Windows Forms Controls with Padding, Margins, and
the AutoSize Property
http://msdn.microsoft.com/en-us/library/3z3f9e8b.aspx

Walkthrough: Arranging Controls on Windows Forms Using Snaplines
http://msdn.microsoft.com/en-us/library/t5b5kc41.aspx

If anything is unclear, please feel free to let me know.

Happy new year, Mythran!

Best Regards,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

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

Mythran

Zhi-Xin Ye said:
Hi Mythran,

Thanks for the clarification.

The recommended distance between control and container border is the sum
of
the control's Margin property and the container's Padding property values.
For example, the default Margin property value for a control is (3,3,3,3),
and the default Padding property value for a form is (0,0,0,0), but the
Windows Forms Designer gives the form a shadowed Padding value of
(9,9,9,9), so the default recommended left -most position of a control is
3+9=12, and so does the top-most position. The snaplines' distances follow
the same rule of calculation, that's why you see the snapline appears when
you put a control at 12 pixel away from the left border of the form.

Ahh, but where do I get this "shadowed padding value" from? Is there a
property on Form that I'm missing, or is there even a way to get this
"9,9,9,9" padding value?

Thanks,
Mythran
 
Z

Zhi-Xin Ye [MSFT]

Hi Mythran,

Thank you for the feedback.

The "shadowed padding value" is used by the Windows Forms designer
internally, there's no a property on Form that represents this "shadowed
padding value". This value is only used when the padding is set to
(0,0,0,0), it you specify another value other than (0,0,0,0) for the
Padding property, the designer will use the Padding value you specified.

However, the snapline distances are the recommended spaces between
controls, you can place the controls at anywhere you like by setting the
location property.

If you have any questions or concerns, please feel free to let me know.

Best Regards,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

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

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