How to fit c# application to monitor with different resolution? - c#

I usually work with two monitors and two differents resolutions. 1366x768 and 1920x1080.
My prob is that my WPF application doesn't fit the monitor with biggest resolution when it runs on monitor 1366x768.
I found differents solutions but none of them work for me.
My last attempt is :
Screen actualScreen = Screen.FromHandle(new WindowInteropHelper(App.Current.MainWindow).Handle);
App.Current.MainWindow.Height = actualScreen.Bounds.Height;
App.Current.MainWindow.Width = actualScreen.Bounds.Width;
Any idea?

Rather than trying to layout your screen using a fixed resolution size, use other properties of the GUI controls to:
Dock along side another control (beside, above, or below).
Add minimum padding between the control and the control it docks next to.
Set a minimum size for the control. That can force scroll bars on the Application window if the user resizes it too small or the monitor's resolution is not large enough to fit your applications layout.
Those are some of the "responsive design" properties of a control in WPF.
My advice when laying out a Panel: start with placing the primary control and then fit the other controls on that Panel in relation to that primary Control (using the Alignment/Margin properties).

Related

Responsive Applications

I am developing a c# application using windows forms and I wonder if it is possible to make the application responsive somehow, I've searched a lot and found that the anchor could be used but it doesn't resize things so it doesn't look responsive, is there a way that I can make c# responsive desktop applications?
For Example:
Here when I maximize the pannels they fit the window but the text will just look bad.
Before Maximization
After Maximization
If you want to simply distribute the components in the maximized window, without scale the components up, I suggest you use the TableLayoutPanel, instead of the Panel.
Using a TableLayoutPanel, you can divide the screen, for instance in 4 parts, 2 columns and 2 rows with 25% in each. And then distribute the components. Don't forget to set the Dock property of TableLayoutPanel to fill, or set the anchor property appropriately.
You can Stil put a FlowLayoutPanel inside the TableLayoutPanle row, doing that will provide a dynamic layout for the child controls that can be arranged horizontally or vertically.
Search for TableLayoutPanel to get some samples.
Some articles about it:
https://www.codeproject.com/Articles/8845/FlowLayoutPanel-TableLayoutPanel-controls-Visual-S
https://www.developer.com/net/net/article.php/11087_3671986_3/Changing-Layout-Dynamically-in-Windows-Forms.htm

How do I fit/re-size Windows Form to any screen resolution?

I know this is duplicated question but I checked all other related question and their answers did not help ( the result still the same as shown in screenshot 2)
I am new to c# windows forms. As shown in screenshot1, I have Form1 with some controls and each group of controls were put in a panel. I designed the application in PC1 as shown in Screenshot1 which is fit the screen resolution and worked well.
My application was developed in 1366 x 768 screen resolution (as shown in Screenshot1) but when I run this application in different computer (PC2) with different monitor size and different screen resolution the form appeared too big and part of the application is missing or is out of the screen.
However I solved this issue using Anchors but another issues came up which is: the user control does not re-size itself ( as shown in screenshot2) and part of it is cut or went under panel1 . I do not know if the problem is related to user control or related to all controls in Form1 (they should resize themselves)
I even tried the following code but the result still the same:
this.WindowState = FormWindowState.Maximized;
this.Location = new Point(0, 0);
this.Size = Screen.PrimaryScreen.WorkingArea.Size;
Screen.PrimaryScreen.WorkingArea
I have been searching to solve this issue the whole day yesterday but I failed, Please help me with any idea/suggestion it might work. Thank you
If you are working with Windows Forms and you cannot switch to WPF then you will prefer to do all the design in the lowest resolution at which you must run.
In WinForms you are setting the Size of every element so they will not re size according to the app size. What they will do is to be distributed along the empty space (if you program them to do so) increasing the free space between them, that's all.
Another option are LayoutPanels as Sinatr said as they try to offer the WPF panel functionality.
By default in WinForms, all controls that you place on the form while designing it have a fixed size. If you don't do anything special, whatever size the controls are when you place them are the size that they are always going to have, no matter what machine you're running on.
As you've noticed, that isn't always going to give good results. The way you work around it is to use a fluid layout, with liberal use of the TableLayoutPanel and/or FlowLayoutPanel container controls, as well as the Anchor and Dock properties for individual child controls. If you take special care to lay out the controls on your form correctly, they can be dynamically resized and rearranged to fit the available screen space.
This code
this.WindowState = FormWindowState.Maximized;
this.Location = new Point(0, 0);
this.Size = Screen.PrimaryScreen.WorkingArea.Size;
Screen.PrimaryScreen.WorkingArea
doesn't do anything. The only thing you need there is the very first line. Once you make the form maximized, it will fill the entire screen. You don't need to force it by setting its Size or Location properties. In fact, those have no effect on a maximized form.
The problem is presumably that the controls placed on your form do not resize themselves automatically (as discussed above). If you had a big enough screen, you'd see empty space where the form was filling the screen but had no controls on it. You have the opposite problem: on a smaller screen, controls don't fit and therefore overlap each other.
It isn't a perfect situation. Even if you code up a perfect dynamic layout, if you try to run the application on a system with a screen that is significantly smaller than your design intended, you're going to end up with buttons that are too small to poke. That's why applications are not, in general, designed this way. A screenful of buttons is terrible UI. The only time this kind of design is acceptable is when you're designing for a touch screen UI, like a restaurant POS. And in that case, you already have a pretty good idea what size screens your clients are going to be using, since it's all specialty hardware.

How to run or adjust Windows application efficiently in any screen resolution in C# 4.0?

How to run or adjust Windows application efficiently in any screen resolution in C# 4.0?
I have done it in 1600 x 1200 resolution but if I try in lower resolutions only some part of the forms are visible.
How to solve this problem? I have searched a lot and got to know about Anchor & Dock will be useful but also to came to know that these should be used from the beginning of designing but I have completed my application while this resolution is now causing the problem when I install the application in any system
The quick and dirty method of making sure your controls remain visible would be to place a Panel on the form, set Dock = Fill and AutoScroll = True, then place every other control in your form inside it. Your controls won't get cut off, but your program won't win any awards for asthetics. If the program is brand new, that's really not the way you want to start things off.
You could redesign it to make use of Anchors and Docking, as you mentioned. You could also place items such as a row of Button controls or a series of TextBox controls in a FlowLayoutPanel, which will take care of repositioning them as you resize the form.
But if adjusting for screen resolutions is important to you, a better way would have been to use WPF from the start. Controls are automatically resized and repositioned as needed, based on their container control.
The real question should be:
How do I want the controls to resize themselves with their parent?
The answer is that you need to specify anchors. Anchors are used to tell your controls how they should react on resize, and what the concerns should be.
Lets say you have a form with two [Cancel] and [OK] buttons. They are usually seen fit at the bottom right of your window. But the default Anchor property is set to Top, Left, so on your form's resize, they stick to the Top, Left corner where they belong according to the default settings. This won't be any trouble if your maximize your form, thus you'll have your button probably in the middle of the screen. But at least, you will see every controls adequately.
But what if resize your form smaller and smaller? Do you still want them to stick at the Top, Left, or Bottom, Right would be more useful? My guess is that you should set the Anchor property to Bottom, Right, for those two.
This might come in handy to have different Anchor property settings depending on how you want your control to react to your form resize. Let's take three TextBox controls aligned horizontally with each other. Perhaps your longer field will be your object Description property located on horizontal-center of your form. Then, when you risize, you have to think what would make more sense on resize. If it is to make it longer on resize in order to fill your form width with all your control, then perhaps you want the DescriptionTextBox to get wider and wider, and the contraray should also be true, on form's resize, you probably want this field to be resized smaller too. Then, to make this happen, you have to set the Anchor property to Left, Right, so that the edge of your DescriptionTextBox control remains at the same very distance of your form's edge at any time.
Another thing is of concern in case of resizing to smaller window, is its MinimumSize property. One shall agree that there is use to have a form of size 34x34 pixels. So, setting your MinimumSize property to a certain size which makes sens for the form to exist, you will avoid display glitches of controls getting one over another.
For more details on the Anchor property: Control.Anchor Property
For more details on the MinimumSize property: Control.MinimumSize Property

User Interface Layout in c#

Kinda new in Microsoft visual C#. I have made a simple program. Everything is working perfectly fine, unfortunately the tricky part for me is i do not know how to layout my objects properly. I would like to copy the layout of the Microsoft visual C# interface, wherein the panels adjust to their predefined ratio and proportion whenever the main form is re-sized and the user may adjust the width and height of each panel. Any readings or code would be a lot of Help. THANKS A LOT!
You could do a number of things:
Allow automatic layout using something like a FlowLayoutPanel
Allow resizing of controls using a Splitter
Look at custom implementations to provide more advanced functionality Collapsible Splitter
Well follow these tutorial links to know about resizing in windows
using Dock and Anchor property. Along this the layout control will
help you to manage the layout - FlowLayoutPanel and
TableLayoutPanel, Panel, GroupBox etc.
Designing Resizable Windows Forms in Visual Studio .NET
Manage WinForm controls using the Anchor and Dock properties
This one is much better to understand.
For a simple start the anchor property is what you want. so for instance if you set all four anchors for that left hand control, and the parent window changes size it will will resize proportionally.
After that it starts getting complicated. Adding panels and then putting your controls inside them. Setting Dock to left, or top or fill. Grow and shink on scrollable controls. Splitter bars.
And last ditch handling resize events and calculating positions and sizes.
Sit down and have a think about what you want to happen, play around with minimum and maximum height and widths, ie no point in working out waht your form is going to look like when it's postage stamp sized...
PS Don't forget VS allows floating panels, and persists (well some times sort of) user choices in the layout, that's a bigger job.
Place a TableLayoutPanel as the base control, anchor it to all sides, define as many columns and rows as you like with "percentage" sizes.
Then place different sections of your form in different table cells. Properly dock your controls in each cell.
Can also use a split container above table if needed.
The ideal order should be like below
SplitContainer
TableLayoutPanel
Panel
Controls

Monitor Screen Independent Forms size & Control Size?

in various case i have seen that when we run apps in various pc with different monitor size then win form behave differently. sometime the form get bigger and as a result few control on that form will not visible.so please tell me how to design win apps in such a way that what ever the monitor size would be the form size and control position will behave same way in all the pc monitor size.please guide me.thanks.
Use Anchor Property of the Control. Default Anchor is Top,Left. If you want Control Width to increase with Form, Set it to Top, Left, Right. Adjust the Anchors according to your requirement.
You have many options in terms of layout of controls. Anchoring is the most common way, but you should also investigate docking and table layout panels.

Categories

Resources