WPF - Create solid layout without designer - c#

I'm blind and would like to use WPF as it is more accessible in many ways. However, the designer used to position elements onscreen isn't accessible.
In Winforms, I used the size and location attributes of each control to arange them in a way they didn't overlap onscreen. This had to do with logic, not sight, so it went fairly well.
In WPF, things are a bit different, though. I know there are several layout containers like Dockpanel, Stackpanel etc. However I am uncertain whether they would keep the controls from overlapping.
So my question is: If you couldn't see, how would you arrange controls to make sure they don't overlap? Which layout controls would you use?

The easiest way to place controls in WPF without overlapping them is to use <Grid.RowDefinitions>and <Grid.ColumnDefinitions>.
You can either define your width and heights with fixed or relative values (using '*').
For good and simple explanation I can suggest these two sites:
https://www.wpftutorial.net/GridLayout.html
https://www.wpf-tutorial.com/panels/grid-rows-and-columns/
Within the grid you can place again other controls, panels or even other grids with their own row and column definition, allowing for creative designs.

Yes, they would keep the controls from overlapping. This is actually an advantage of WPF: its containers place the controls relative to each other and other controls so they do not overlap and resize or move on window resizing. If the control size will exceed the size of the container, the part of the control will be hidden, not overlapping over it's neighbours. That relates to any container, except for canvas, where the elements are placed by absolute coordinates.

Related

How to handle the size of controls in WinForm application for different font size/localized text?

I am facing one same problem for trying to support any of the followings in a WinForm application:
An option that allow users to change font and font size in all memos/labels/buttons/edits/combos/grids/..etc controls.
Localization where the length/size of the text changes in different languages (and would need a larger font size)
"Windows Color and Appearance" settings which let you change font and font size
The problem is that the size/location/layout of the controls would be all messed up due to the changing size. Either a control can't show all its content, or an autosized control would overlap other controls, or gets clipped by the bound of a parent control, or have problems with wordwrap, or...etc.
The controls are usually some memos/labels/buttons/edits/combos/grids/..etc simply placed on a panel/groupbox/tab/..etc, or combinations of such controls nested in other container controls. I wonder if there are some easy standard less-painful way to handle this problem. (Also, wonder if there are some easy or standard way to make controls pick up the font setting automatically instead of assigning the fonts manually.)
Thanks in advance.
I would suggest placing controls in containers designed for the purpose...such as a TableLayoutPanel. Perhaps even nested panels, if your layout is complex. With that, you can allow most things to AutoSize, and control positions based on percentages of overall width/height.
As far as I could understand, you want to change the font size for all your controls, and fear that those changes would ruin your layout.
First you need to figure out how are you going to handle larger/longer text. You can wrap strings or trim the tails, or you can make your layout larger, while keeping the proportions of your controls the same. Other than that, I don't think there's any easy solution for handling arbitrary changes to font-size/language.

How to make controls stay in a distance of each other when adding them programmatically?

I have a panel, which I add controls programmatically to it. I want each control stay in a far from other controls and not stay on top of them.
for this purpose I can calculate a position for each control based on Panel's size, but it seems a bit odd.
Is there a way to make controls be added in a line and when it ended they be added in another line?
You can use a FlowLayoutPanel to achieve what you're describing. It's under Containers in the ToolBox. Set the direction to horizontal and it will flow from left to right, and wrap when it needs to.
I believe the WrapPanel class does what you're describing in WPF. Or the FlowLayoutPanel in WinForms.
You have a few options. You can use one of the containers such as FlowLayoutPanel or TableLayoutPanel. You can also nest them in each other. And you have to set the Margin property for each control you add to the containers.
Sadly the Windows Forms technology lacks on this part a little, while WPF has a very rich layout system. Even somethings like Margin doesn't always work as expected.

Layout manager in C#

I am trying to design a UI in C# . I come from Java background and am familiar with the different layout managers in Java.
So what I am trying to do is:
I have a Pane. To this pane I wish to add controls one below the other. In Java I would have used a BoxLayout (Y-Axis), and then just added the controls. Also the individual controls/containers could have been further customized by using different layouts for each individual container.
How do I do this in C#?
What I need to do is:
1)Add controls one below the other. Each individual control can be a collection of smaller controls.
So what I can have is something like:
Control 1 here
Control 2 here
Control 3 here
Each control can have its own layout, say BoxLayout on X axis and so on. How do I do all this?
PS: I am using WINFORMS.
I will take a stab that you are building WinForms, and suggest you look at the following controls, which are similar to the Java layout managers;
Panel
FlowLayoutPanel
TableLayoutPanel
A Panel will let you place controls arbitrarily within it, and you use the Dock, Anchor and Location properties to position each child control.
A FlowLayoutPanel will do what you are asking if you set the flow to 'TopDown', but everything will be left-aligned and that can't be changed I believe.
A TableLayoutPanel is going to be the most useful to you I expect. Just create the panel with 1 column, and add each control to a row. The rows can be set to AutoSize to their contents to give you the closest match to a BoxLayout I think.

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

Resizing the datagrid proportional with the form size

hii
I am a fresher in the c # so i want to know how to resize the datagrid(any other control)aith respect to the form size.
I just added one datagrid in the form then what i have to do?please help me...from the very basic please
For a dynamic layout that scales with your form size you have various options (depending on the complexity of your layout).
The first ones are
Anchoring
Docking.
But you can also work with advanced container controls like
TableLayoutPanel
FlowLayoutPanel
SplitContainer.
Some more informations i already post in an older question.
If you have more concrete problems about how to solve a specific layout problem you should post a new question with your exact problem.
But at a first tip i can say that it is never necessary to use the Resize event and do some size changes on yourself. There is always a solution that can be solved with the above elements.
You have to ways of dong it:
Using a Dock. It works fine and is very easy to use but its limit is that you can stick it to a one side of parent container. So if you want to streach control only in width you will fail.
Using Anchor. It require more configuration but you can specify all four(top, right, down, bottom option separably.
Regards
Szymon
Go to the properties window and scroll down to "Docking" and choose to dock the control in the parent container. This will give you various options about how you want the control to dock. You should put some containers in there of some sort, maybe, if you've got buttons or things you'd like to have show up above/below/next to the grid. Also, if your grid goes behind your other controls, select the control and bring it to the front.
edit:
You need some containers in which to put your buttons / drop-downs. You could use a flow-layout panel (which wouldn't resize its child controls), or you could use a plain panel or the table-layout panel. The table one will let you dock your child controls within each cell of the table, and you can set your columns & rows to auto-size to a percentage of the entire table width. That way everything will autosize accordingly.

Categories

Resources