illep
illep

Reputation: 175

C# Screen resolution and Form display

I have a C# WinForms application and when I give the executable to different users the application displays in different sizes (based on their screen resolution). Some of the parts of the application can't be seen.

Is there anyway to auto-size the window based on the screen resolution, or is there another approach?

EDIT : furthermore it appears in different styles under different Operating systems, is there away to standardize its design ?

Upvotes: 3

Views: 12912

Answers (3)

Sherif Hamdy
Sherif Hamdy

Reputation: 593

Try this

private void MainForm_Load( object sender, EventArgs e ) 
  { 
     this.Size = Screen.PrimaryScreen.WorkingArea.Size 
  }

Upvotes: 0

Navid Rahmani
Navid Rahmani

Reputation: 7958

You can use Control.ScaleControl and Control.Scale

private void MainForm_Load( object sender, EventArgs e )
{
    float width_ratio = (Screen.PrimaryScreen.Bounds.Width / 1280);
    float heigh_ratio = (Screen.PrimaryScreen.Bounds.Height / 800f);

    SizeF scale = new SizeF(width_ratio, heigh_ratio);

    this.Scale(scale);

   //And for font size
   foreach (Control control in this.Controls)
   {
      control.Font = new Font("Microsoft Sans Serif", c.Font.SizeInPoints * heigh_ratio * width_ratio);
   }
}

In the case when the development platform resolution is 1280x800

According to @sixlettervariables 's answer Docking and anchoring will help of course.

Upvotes: 4

user7116
user7116

Reputation: 64068

It sounds like you have specified your controls with absolute positioning and other layout defaults. In order to make a WinForms application that looks and feels the same and behaves correctly in various resizing scenarios, you need to utilize the Anchor and Dock properties. Arranging controls in WinForms can be a tiring process, but MSDN includes some nice How To's on the subject.

I would also suggest following along with this TechRepublic article, which covers the difference between Anchoring and Docking, and shows you visually what each property accomplishes:

Anchoring example from the TechRepublic Article

Upvotes: 5

Related Questions