I created a .NET Framework 4.0 winforms project and added a "tab" as you can see in the following picture:
From the View Designer both tabs (Profile and Features) looks like they fit the rectangle window, but when I run the program, the "Profile" tab looks the same but the "Features" tab suddenly does not fit the rectangle anymore and looks like that:
I don't want to give the user the option to enlarge the window.
I am wondering why it looks like it fit the rectangle window but when I run the application, it does not.
This is the "Features" tab from the MainForm:
this.TabPage2.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.TabPage2.Controls.Add(this.listBoxShowFriendsBirthday);
this.TabPage2.Controls.Add(this.buttonShowFriendsBirthday);
this.TabPage2.Controls.Add(this.label1);
this.TabPage2.Controls.Add(this.monthCalendar1);
this.TabPage2.Controls.Add(this.postTextBox);
this.TabPage2.Controls.Add(this.pictureBoxEvent);
this.TabPage2.Controls.Add(this.listBoxFetchEvents);
this.TabPage2.Controls.Add(this.buttonFetchEvents);
this.TabPage2.Controls.Add(this.postButton);
this.TabPage2.Location = new System.Drawing.Point(8, 39);
this.TabPage2.Name = "tabPage2";
this.TabPage2.Padding = new System.Windows.Forms.Padding(3);
this.TabPage2.Size = new System.Drawing.Size(1976, 806);
this.TabPage2.TabIndex = 1;
this.TabPage2.Text = "Features";
this.TabPage2.UseVisualStyleBackColor = true;
I suggest you to use Dock and Anchor properties of controls. This way you can also allow to user resizing the window.
Dock:
Anchor:
See - How to: Anchor and Dock Child Controls in a FlowLayoutPanel Control
To prevent the user from rescaling their window:
c# how to prevent user from resizing my application window?
but aside from that, I'd suggest, (just like ikram said) to use docking, that way you're certain your lay-out will remain, no matter the resolution/screensize the user has.
Related
In the Form Load event I'm adding some controls to the Form dynamically.
I use this code to resize the Form to adapt its Size to the its new content:
this.MaximumSize = new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
this.AutoSize = true;
this.AutoSizeMode = AutoSizeMode.GrowAndShrink;
It works fine but StartPosition = FormStartPosition.CenterScreen doesn't work as expected:
the Form center position is calculated based on the original size.
My suggestion is to resize the Form using it's PreferredSize property. When you have added new Controls to the Form and these Controls don't fit in client area, the PreferredSize is updated to reflect the new size needed to contain them all, but limited to the Form's MaximumSize you have set.
Then use the CenterToScreen() method to center the Form to the current Screen.
CenterToParent(), suggested by Hans Passant in comments, has the same effect if the Form is not parented to any other Form: i.e., if it's not shown using the .Show(this) method overload; otherwise, it's centered to the Owner Form.
Setting AutoSizeMode = AutoSizeMode.GrowAndShrink (or calling the SetAutoSizeMode() method) and then setting AutoSize = true can have unwanted results. The more visible is that you cannot resize your Form with normal means anymore. The area occupied by a StatusStrip may not be considered etc.
As a note, if your application is not DpiAware, the Screen measures and your Form's Size may be different from what is expected. In case it's not, read about it here:
High DPI support in Windows Forms
These notes about the Screen and VirtualScreen may also be useful:
So, add your controls to the Form (maybe in the Load handler) then:
var screen = Screen.FromHandle(this.Handle);
this.MaximumSize = new Size(screen.WorkingArea.Width, screen.WorkingArea.Height);
this.ClientSize = this.PreferredSize;
this.CenterToScreen();
Today I'm trying to use c# to fit a number of windows controls onto a panel sequentially.
I would like them to dock to the top so that I can then use BringToFront() to stack them up.
HOWEVER I would also like them to be centred. Currently, docking behaviour forces the controls to the left of the screen (however much I resize and change the location property)
I then tried to anchor my controls to the top of the panel instead. This enabled the controls to be centred and also let me resize them but anchoring has no stacking behaviour and each control overwrites the previous one.
I have researched this extensively for hours and have found no answers to this question. Is it possible to use either or both of these properties to stack my controls in the centre of the panel?
My code currently stands as so:
//Docking
userControl.Dock = DockStyle.Top;
userControl.Width = 633;
userControl.Left = (pnlRules.Width - userControl.Width) / 2; //doesn't work
Point location = new Point(((pnlRules.Width - userControl.Width) / 2), 0);
userControl.Location = location; //doesn't work
userControl.BringToFront();
OR
//Anchoring
userControl.Anchor = AnchorStyles.Top;
Point location = new Point(((pnlRules.Width - userControl.Width) / 2), 0);
userControl.Location = location;
userControl.BringToFront(); //doesn't work
My outputs are either stacked controls bound to the left panel edge (docking) or overlapping controls beautifully resized and centred (anchoring)
Thanks :)
Anya
Edit:
This captions my problem quite nicely:
http://www.techrepublic.com/article/manage-winform-controls-using-the-anchor-and-dock-properties/
This explains that using docking, the controls can be stacked next to each other. I would just like the docked, stacked controls to not be bound to the left edge of the panel.
There is no way to use a combination of docking and anchoring. A TableLayoutPanel may have worked here but I was tied to a simple Panel.
The fix was to use padding to force the control to centre:
userControl.Dock = DockStyle.Top;
pnlParent.Padding = new Padding((pnlParent.Width - userControl.Width) / 2, 0, 0, 0);
userControl.BringToFront();
When I put a System.Windows.Forms.ListView on a form in a WinForm application (using C#/.NET 4.5), it gets rendered as a flat rectangle, even though the BorderStyle property is set to Fixed3D. This is also true for TextBox and a number of other controls.
This is the code generated by the Form Designer for the ListView. The form is just a plain Form and has no other controls at the moment.
...
this.listView1.Location = new System.Drawing.Point(10, 115);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(250, 97);
this.listView1.TabIndex = 2;
this.listView1.UseCompatibleStateImageBehavior = false;
this.listView1.View = System.Windows.Forms.View.Details;
...
If I open older programs (done with VB6 or .NET 2), control borders are rendered with 3D look (that is, controls are rendered with a proper 3D border) instead of being flat.
Is there anything I can do to force the 3D look for these controls under .NET 4.5?
This surely works however I think it also applies to all your controls on your forms, just remove the line Application.EnableVisualStyles() in the Main() method in Program.cs file.
I have a homework, where I need to create a winforms game using C#. I have the following components:
Panel subclass with custom paint event
Panel with default windows UI elements.
I want them to arrange like this:
Because I draw on the center panel manually, I want to set it's Width, and Height fixed, so the Form subclass, what will contain it, would show the whole panel.
I tried setting the size manually in the panel subclass:
Width = someFixedWidth;
Height = someFixedHeight;
Then adding it to the containing Form:
GamePanel panel = new GamePanel(...);
panel.Dock = DockStyle.Center;
this.AutoSize = true;
this.AutoSizeMode = AutoSizeMode.GrowAndShrink;
this.Controlls.Add(panel);
Using this, I thought, that the form will respect the size of the Panel, but it just shrinks the window to so small, that nothing is visible, only the title.
So my question is, how would I be able to set the size of the GamePanel manually, and then dock it in the center of the form, so that the Form will respect the size I set, and doesn't makes it smaller/bigger?
The Dock property is used to define the behavior of the component during resizing Container (Form) The way you did the screen is not centralized but is resized according to the screen changes, the ideal is to use a method to reposition the control and set its size. See this:
SuspendLayout();
Width = someFixedWidth;
Height = someFixedHeight;
panel.Size = new Size(panelWidth, panelHeight);
panel.Location = new Point( ClientSize.Width / 2 - panelWidth / 2, ClientSize.Height / 2 - panelHeight / 2);
panel.Anchor = AnchorStyles.None;
panel.Dock = DockStyle.None;
ResumeLayout();
In my case, I edited minimum height/width of the panel and it worked.
I tried to edit the code which related to design but it was not recommended to rewrite auto-generated code.
Thank you.
I assumed that the C# margin property had a meaning like in CSS - the spacing around the outside of the control. But Margin values seem to be ignored to matter what values I enter.
Then I read on the SDK:
Setting the Margin property on a
docked control has no effect on the
distance of the control from the the
edges of its container.
Given that I'm placing controls on forms, and perhaps docking them, what does the Margin property get me?
Like Philip Rieck said, the margin property is only respected by container controls that perform layout. Here's an example that makes it fairly clear how the TableLayoutPanel respects the Margin property:
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
TableLayoutPanel pnl = new TableLayoutPanel();
pnl.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50));
pnl.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50));
pnl.Dock = DockStyle.Fill;
this.Controls.Add(pnl);
Button btn1 = new Button();
btn1.Text = "No margin";
btn1.Dock = DockStyle.Fill;
Button btn2 = new Button();
btn2.Margin = new Padding(25);
btn2.Text = "Margin";
btn2.Dock = DockStyle.Fill;
pnl.Controls.Add(btn1, 0, 0);
pnl.Controls.Add(btn2, 1, 0);
}
}
}
I believe the only .NET 2.0 built-in controls that respect this property are FlowLayoutPanel and TableLayoutPanel; hopefully third-party components respect it as well. It has basically no effect in other scenarios.
The margin property is used by whatever layout engine your control host (Panel, for example) is using, in whatever way that layout engine sees fit. However, it is best used for spacing just as you assume. Just read the documentation for that specific layout engine.
It can be very handy when using a FlowLayoutPanel or TableLayoutPanel, for example - to either reduce the default padding or space things out a bit. Obviously, if you write a custom layout provider, you can use Margin however you see fit.
Control.Margin property could be also useful at design time if you do not use layout container, but rather place controls manually.
It affects the distance between manually dragged controls at which snaplines appear.
E.g. for default margin value of 3 for text box you would have this snaplines:
And for margin of 10 - these (label has margin of 3 in both cases):
So if you have some strict guidelines for you UI then you just set the margins as you need and drag controls to the snaplines.