2

I am designing a dotnet Window application which has the resolution 1366x768.I am not able to resize the form controls when the resolution of the screen goes below the given screen resolution.Is there any solution in which I could resize the form controls for lesser resolution also. So far I have tried the following code. It works well when the resolution is above the given resolution.

 private void masterform_Resize(object sender, EventArgs e)
        {

            double RW = (this.Width - CW) / CW;
            double RH = (this.Height - CH) / CH;
            foreach (Control Ctrl in Controls)
            {
                Ctrl.Width += Convert.ToInt32(Ctrl.Width * RW);
                Ctrl.Height += Convert.ToInt32(Ctrl.Height * RH);
                Ctrl.Left += Convert.ToInt32(Ctrl.Left * RW);
                Ctrl.Top += Convert.ToInt32(Ctrl.Top * RH);
            }
            CW = this.Width;
            CH = this.Height;
        }


  private void masterform_Load(object sender, EventArgs e)
        {
            IW = this.Width;
            IH = this.Height;`enter code here`

        }   

Let me know if any solution exits.

Lucifer
  • 29,392
  • 25
  • 90
  • 143
user3541403
  • 247
  • 1
  • 3
  • 18
  • The trivial way to do this is to simply re-assign the form's Font property with a different font size. Font is an "ambient" property, controls normally use their parent's font. Everything gets resized automagically. Actually creating UI like this is rarely appropriate. Check how other programs that run on your machine behave when you resize their main window. – Hans Passant Apr 29 '14 at 11:53

2 Answers2

2

You can use Table Layout Panel+Anchor to let form handle the size of controls in every resolution.

for doing this you can follow this instruction:

Create a form put a table layout panel, set rows and cols as desired, Do not forget to set the cols width as Percent then put your controls in the cells (or first put a panel into a cell then put your controls on the panel), set the anchors to left+right, and that is it.

See below pictures:

Pic1

Pic2

Reza
  • 18,865
  • 13
  • 88
  • 163
  • If u have any sample application pls provide. – user3541403 Apr 29 '14 at 11:52
  • Tried this .It is also not working.Am not able to resize the controls still – user3541403 Apr 29 '14 at 12:12
  • @user3541403 If do you mean in design time you have two choice, 1. set Dock of control to Fill 2. Add a panel set it's Dock to Fill then put your controls on Panel and you can change the size of control. – Reza Apr 29 '14 at 12:15
  • If i set dock property of a control eg.if suppose for a button ,i am seting dock as fill it is occupying whole form space.I am not able to place any other controls – user3541403 Apr 29 '14 at 12:44
  • @user3541403 First put a panel instead of button then put your button on panel. – Reza Apr 29 '14 at 12:51
  • @user3541403 See pictures in answer, also you can put another Table layout panel into a cell.(like button) – Reza Apr 29 '14 at 12:58
  • working fine for me after setting dock to panel as well. – user3136884 Nov 21 '16 at 05:53
0

try this

private Size oldSize;
private void Form1_Load(System.Object sender, System.EventArgs e)
{
    oldSize = base.Size;
}
protected override void OnResize(System.EventArgs e)
{
    base.OnResize(e);
    foreach (Control cnt in this.Controls) {
        ResizeAll(cnt, base.Size);
    }
    oldSize = base.Size;
}
private void ResizeAll(Control cnt, Size newSize)
{
    int iWidth = newSize.Width - oldSize.Width;
    cnt.Left += (cnt.Left * iWidth) / oldSize.Width;
    cnt.Width += (cnt.Width * iWidth) / oldSize.Width;

    int iHeight = newSize.Height - oldSize.Height;
    cnt.Top += (cnt.Top * iHeight) / oldSize.Height;
    cnt.Height += (cnt.Height * iHeight) / oldSize.Height;
}

description here

Community
  • 1
  • 1
Tanmay Nehete
  • 2,138
  • 4
  • 31
  • 42