Hi,
How can I setBounds of a component in percentage of screen size so that the interface looks same on different resolutions.
I am using Visual Studio 2008. When I click on a button in designer view it shows me button's properties on right side. There I can set Location (x and y) of the button. But it allows me to enter only numbers. I want to add some formula like:
For x: SystemInformation.PrimaryMonitorSize.Width / 2;
For Y: SystemInformation.PrimaryMonitorSize.Height / 2;

Could anyone please help me fix this ??

Dani AI

Generated

A short, practical follow‑up to the thread: ’s anchor/dock suggestion (confirmed by and by ) is the simplest built‑in fix for many cases because it preserves a control’s offset from form edges as the window resizes. That said, anchoring keeps constant offsets — it does not express positions or sizes as percentages of the form. ’s programmatic approach also works, but gets tedious for many controls. Two designer-friendly alternatives that give true percent-based layout are (1) TableLayoutPanel with percent Column/Row styles, and (2) computing SetBounds in the form’s Resize/layout event.

TableLayoutPanel approach (design-time or runtime): create a grid, set ColumnStyle/RowStyle to SizeType.Percent, then add controls to the desired cell. The center cell will always be at the same percentage of the form.

var tlp = new TableLayoutPanel {
    Dock = DockStyle.Fill,
    ColumnCount = 3,
    RowCount = 3
};
tlp.ColumnStyles.Clear();
tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 25F));
tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F));
tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 25F));
tlp.RowStyles.Clear();
tlp.RowStyles.Add(new RowStyle(SizeType.Percent, 25F));
tlp.RowStyles.Add(new RowStyle(SizeType.Percent, 50F));
tlp.RowStyles.Add(new RowStyle(SizeType.Percent, 25F));

tlp.Controls.Add(myButton, 1, 1); // put control in center cell
this.Controls.Add(tlp);

Code-driven percent layout: handle the form’s Resize (or Layout) and call SetBounds using ClientSize fractions so placement/size follow percentages:

private void Form1_Resize(object sender, EventArgs e)
{
    int w = this.ClientSize.Width;
    int h = this.ClientSize.Height;
    int bw = (int)(w * 0.20); // 20% width
    int bh = (int)(h * 0.08);  // 8% height
    int bx = (w - bw) / 2;     // center horizontally
    int by = (h - bh) / 2;     // center vertically
    myButton.SetBounds(bx, by, bw, bh);
}

Extra tips: wrap many changes between SuspendLayout/ResumeLayout to avoid flicker; test on different DPI/scaling settings and set AutoScaleMode appropriately; use margins and Dock/Anchor inside TableLayoutPanel cells for fine tuning. TableLayoutPanel usually gives the most maintainable “percentage” behavior without hard‑coding positions.

Recommended Answers

All 6 Replies

You can set a control's location and many other properties from within the code. What you want to do would be done like so, and other properties can be set in a similar way.

ButtonName.Location = new Point(X, Y);

I'd put this in the constructor, after InitializeComponent();, so that it runs when the program is started.

WildBamaBoy,
Thanks for your suggestion. Yes I am doing this right now, but it's really hectic to set location of each component manually. I thing it nullifies the convenience of drag-drop thing provided by Visual Studio. What do you think?
I was expecting some way that we drag a component on form in design time and the component automatically adjusts it's location according to screen width and height.


Adil Shoukat

why dont you use Docking and Anchoring properties..???

this will get you going: http://www.codeproject.com/KB/miscctrl/anchordock.aspx

Thanks for help. Anchor property seems to be doing what I wanted. Resizing is very smooth now. I've set the Anchor property of a button to 'Top'. Will it be positioned on same relative location on different sizes of screens ??

yes it does the same.... anchor your control to right and top corner.... or top and left corner

commented: really helpful +0

yes it does the same.... anchor your control to right and top corner.... or top and left corner

Awesome ..
It worked :)
Thanks everyone for help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.