How to set text-positions of different controls inside a FlowLayoutPanel, so that they are on a straight line? - winforms

In this WinForms-App (not my choice, they made me use WinForms) there is a FlowLayoutPanel filled with a NumericTextBox, a Label and a CheckBox.
Each control has a text which is displayed. The text of the Label is about 3 pt lower than the other texts (image).
Changing the margin of the Label is not an option, because the outcome after changing the font could be unwanted.
Has anyone an idea of how to fix this without changing the margin?
I tried to change the anchoring and docking for the controls in the FlowLayoutPanel, and the outcome that came closest to my expectations was the one in the image.

Related

How can I neatly line up right-aligned check boxes with with other controls?

The Visual Studio WinForms designer lets you snap together text boxes, list boxes, buttons, and so forth reasonably well. However, a check box with CheckAlign=MiddleRight does not work quite like the others, because it has two components: the built-in label, and the check box itself. If you line up the text with other labels, the check box is left floating and will not automatically line up. You can turn off AutoSize and attempt to size the CheckBox control so the check box lines up, but this is touchy and will be thrown off by custom font sizes.
In this example, I've almost lined up the check box with the other controls, but not quite:
If I had several check boxes it would be a real headache, as with AutoSize turned off the position of the check box is relative to the end of the text, so each control has to be individually sized pixel by pixel.
What's the best way to line up check boxes with other controls?
One workaround is to not include text with the CheckBox and instead have a separate Label control. However, by doing so you lose the ability to click on the label to toggle the check box. On a high-resolution screen, clicking a 10x10 pixel is finicky, a poor UX experience. You could add an OnClick event to the label, of course, but that seems like solving the wrong problem.
Here's an example of someone else with the same issue: in the preferences panel in Oracle SQL Developer, when there's a mix of properties the check boxes are handled with separate labels and boxes:
Personally, i avoid that issue by not having the CheckBoxes aligned to the right:
rather than having the caption on the left.

Telerik theme is defeating right-alignment

I have a Windows form control that was built using Telerik RadTools for Windows Forms. The form is styled nicely using the "Office 2010 Silver" theme. However, at runtime, the theme seems to throw-off the alignment of some of my labels.
I have several rows of labels and textboxes on my form. I want the labels' right sides to all line up. This is easily done in the Visual Studio designer. However, I believe my Telerik theme is throwing things off because it slightly changes the font, which alters the labels' sizes as well. Since the designer only stores the x,y coordinates of my controls, the labels' right edges no longer line-up.
Is there a way to code around this?
I fixed this by doing the following on all labels:
Set AutoSize to false.
Set x-location to that of the widest label.
Set size equal to that of the widest label.
Set TextAlignment to right.
Since all labels will have the same width and the same starting (left) location, their positions will now be fixed. Setting the alignment to the right takes care of the problem.

Winforms - can't resize when adding textbox

I'm putting together a simple winforms app, which consists on 1 form and a textbox.
I want to have the textbox scale in width when the form is scaled by the user.
By default the textbox is a fixed width and anchored at the top & left. The form scales ok, but not the textbox.
If I apply left, top & right anchor to the textbox it only scales the textbox & form up and I can't shrink it.
Do I need to dynamically set the width of the textbox to that of the form?
I've been trying to recreate this for 5 minutes and it resizes fine for me, and I've only been able to find one set of combinations that reproduces the behavior you describe:
Is the textbox inside a panel with Autosize set to "true" and AutoSizeMode set to "grow only" perhaps? That's the only way I've been able to duplicate the behavior.
Here's a screenshot of the properties set this way. My textbox is also set to anchor at top, left, right. Again, this is the ONLY way I've been able to duplicate the behavior you're describing, so hopefully this helps.

How to resize multiple WPF controls when the text size changes?

What's the correct pattern to resize controls when a font size changes?
For example, I used the WPF designer to make a form, and placed UI elements from the toolbox. Late in the game I got the requirement that the font size of every UI element needs to be configurable, so now I'm thinking there has to be a better way to resize controls when the font size changes. Right now I'm doing alot of code behind calculations on Margin properties.
For such cases I usually place my control inside Grids and StackPanels, this way font size won't affect the layout, everything will be stretchable.
Never place controls on the Window using absolute coordinates.
Make sure your Height and Width on the controls are set to Auto.
When you use the designer/toolbox to add controls it usually provides a static height/width. If you type the tag in the XAML editor you get the control default which is usually Auto.
You'll notice the black diamond in the property window next to the attributes that are changed from their default value. You can right click and choose reset value to clear the attribute from your XAML and see what the default value is.

WPF: Changing the look of a Surface Listbox "click-effect"

I'm changing the look of some Controls that I use in my Microsoft Surface Application at the moment. And today I'm working on a SurfaceListBox.
I have the template for it and already changed background and borders and other stuff.
But I really cannot find where I can change the color of the rectangle that appears on the ListBoxItem when you touch the ListBox. At the moment it's just white and I want to change that.
It's not something like the effect that you can see when the item is selected. I already found that and changed that so my selected item now appears in a different color. But the effect in the second when you touch the item stays white.
So where can I change this effect?
If you mean the bright, white highlight that happens when you press your finger against an item you should look into the SurfaceShadowChrome. It's used by (almost) all surface controls to give feedback when the user is pressing on a control.
It does have a Color property that you can modify to suit your needs. Apart from the color, and the tweaking of corner radius and shadow offset, it's not a very flexible decorator. But hopefully, you'll be able to make it do what you need.

Resources