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

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.

Related

Hide a control without leaving empty space

I have a WinForms project based heavily on the DevExpress Windows Forms suite. I am working on a control that appears as below:
I use the term programme to differentiate between training programmes for people, from program, meaning computer program. In the image there are 3 ProgrammeGroup user controls. Each has a header, with 2 combos, and below that, a ProgrammeDetail user control with programme details.
When the top, left combo shows a value of Yes or blank, the programme details must be visible, otherwise not. When making a ProgrammeDetail control invisible, I also set its Height to zero, and the outer ProgrammeGroup control shrinks on auto-size.
However, if I set the Yes/No combo on the middle ProgrammeGroup to No, that
ProgrammeGroup shrinks, but leaves a gap between the header only middle control and the bottom control. This gap is visible in the following image:
How can I lay my ProgrammeGroup controls out so that if one shrinks in height, all those below it are moved up, meaning no empty gap. I am hoping that one of the many different panels in the DevExpress WinForms suite can help me do this, but I am stumped at finding one.
Solution to this problem is using FlowLayoutPanel which automatically align controls inside it next to each other.
The best way to adjust sizes and locations of child controls is to place them onto LayoutControl. This control is specially designed for such tasks. To hide unnecessary layout items/groups, use the BaseLayoutItem.Visibility property.
I should also note that LayoutControl is flexible and supports different modes including the Flow Layout Mode.
See Also:
Customization Form

Combo box in a MFC application

My question is about combo boxes in Windows MFC applications.
The dropdown part of the combo box contains items composed of a bitmap and a string.
Sometimes, the strings are too long and I have to adjust the width of the dropdown part of the combo box using the CComboBox::SetDroppedWidth() method.
My problem is that when the combo box is near the right edge of the computer screen, the right part of the dropdown is hidden (see image_1 and image_2 below).
I would like it to behave like in Excel (see image_3 below) meaning I would like the dropdown list to be shifted accordingly so that all its items can be seen without being cropped.
How can this be achieved?
image_1: right part of the dropdown is NOT hidden
image_2: near the computer right edge, the right part of the dropdown is hidden
image_3: Excel combo box
=================================================================
EDIT 1
=================================================================
EDIT 2
Ok. I forgot to mention that m_cbXmodels is a CComboBoxEx object. This is why the handles are NULL. I could get the handles via GetComboBoxCtrl()...
Handle the CBN_DROPDOWN notification.
Get the handle for the list control with GetComboBoxInfo.
Now use MoveWindow to adjust the window as needed.
Getting the current screen size is available with MonitorFromWindow. See rcWork member in MONITORINFO. You just need to adjust the left and right coordinates.
EDIT: As you can read in the comments: My Approach with CBN_DROPDOWN is to early Thanks to zett42). It is not possible to resize the combo box list part here.
But it is possible to post a user defined message to the same window and to reposition the window than.

Automatically resize form and move controls when new ones are added or a text in a label grows

Beginner in Visual Studio here.
I'm trying to create an C# Windows Form application that simply displays a form to show some information about the local system.
I have created this form with the help of the VS designer:
As you can see, it consists of a simple panel with a PictureBox at the top, a label below it (both centered), three table layouts next to each other, and a button at the bottom. Never mind the label texts.
The rows of all the table layouts are set to absolute, this is because I want to programatically add rows to the end middle and right table layout if there are more than one local IP address in the computer and setting the rows to auto size or percentage would make alignment difficult if more rows are added.
Now, my problem is that I can't figure out how to make the form resize and move the controls (specifically move the 'OK' button down) when more rows are added.
I have tried anchoring the button to the bottom edge of the form as well as the bottom of the middle table, both produce the same result: the button stays where it is and the new rows overlap it, like this
I would also like to know how to have the form resize to fit the contents of the labels. For example, if the text in one of the labels inside the layout is bigger than the column width, I'd like to have the form grow to accommodate it. Likewise, I'd like the form to shrink to fit the largest text.
I realize that this may not be the most efficient or easiest way to create this particular form, but it is what I've come up with, I of course welcome any suggestions in that regard. It is important to maintain alignment between the rows of all table layouts.
Thanks in advance.
Use a data grid and add controls to it if an add button is pressed. You can scroll through the rows so you won't need to adjust the height of the form

How to scale form with font size in WPF?

How can i scale a Form with font in WPF?
i.e. What is the WPF equivalent of
this.Font = SystemFonts.IconTitleFont;
In WinForms, if you're a good developer, you honor the user's font preferences. A WinForm that starts out as:
You then apply the user's font preferences:
this.Font = new Font("Segoe Print", 11, GraphicsUnit.Point);
and elements on the form scale to accommodate the new size:
Notice:
the form is wider and taller
the label is positioned further down, and to the right
the label is wider and taller
the text of the label is not cut off on the right or on the bottom edge
the button is wider and taller
but button is positioned further down, and to the right
Note: In WinForms you can also use the line:
this.Font = SystemFonts.IconTitleFont;
WPF doesn't support Font, which is why i provided the clearer alternative. For the example below.
A similar WPF form starts out as:
You then apply the user's font preferences with:
this.FontFamily = new FontFamily("Segoe Print");
this.FontSize = 14.666; //11pt = 14.66
and elements on the form don't scale to accommodate the new size:
Notice:
the label's position has not changed
the button's position has not changed
the form is not wider or taller (text is cut off)
the label is not any wider (text is cut off on the right)
the label is not any taller (cutting off text along the bottom edge)
the button is not any wider (text is cut off)
Here is another example of two buttons that are the same size:
WinForms:
Windows Presentation Foundation:
Bonus Reading
WPF: How to specify units in Dialog Units?
How to prevent WPF from scaling with the Windows font size options?
WPF version of .ScaleControl?
WPF doesn't do primitive font-based scaling because it's... well, primitive. You can see it in your own screenshots.
Here's your "WinForms, before changing font" screenshot. Take a look at how much space there is between "sat on a log." and the right edge of the form.
And here's your "WinForms, after changing font" screenshot. Notice how much less margin you have after "scaling".
If you hadn't left all that extra space, then your label would be cut off with the new font. And with some fonts, it would be cut off even though you did leave all that extra space. That's what I mean when I say WinForms' scaling is "primitive". WinForms picks a single scale to apply to everything, and that scale is not chosen with any awareness of your content; it's based on average statistics for the font, which can and will fall apart once you start talking about specifics.
WPF doesn't hobble you with something that primitive. It gives you an amazingly powerful layout system, where it would be trivial to make a window that scales beautifully. But instead, you're choosing to cripple that layout system by using hard-coded sizes. Stop it.
Hard-coded sizes have two huge problems:
They don't adapt to different fonts. You've noticed this already.
They don't adapt to different content. (What happens when you want to make a German version of your app, and the German text doesn't fit into your hard-coded button size?)
Hard-coded sizes just don't adapt. To anything. You had to use them in WinForms because that's all WinForms supported. But WPF gives you a proper layout system, so you don't have to (and shouldn't) use anything that crude.
All you need is this:
A Window with SizeToContent="WidthAndHeight". That way, the window will be exactly the right size to accommodate the text and button, no matter what font or language you use.
Since you only have two UI elements, and one is above the other, you would put a StackPanel inside your Window.
Inside the StackPanel, you need:
A Label or TextBlock to show your text, with the text in Content (Label) or Text (TextBlock); and
A Button with HorizontalAlignment="Right", and the text in Content.
Set some Margins on the StackPanel, TextBlock, and Button to space things out to your liking.
That's it. Don't set any other properties on anything -- especially not Width or Height.
Now, if you change your font, the window and the button will still be exactly the right size, and won't cut off your text. If you localize your app into a different language, the window and the button will be exactly the right size, and won't cut off your text. Stop fighting WPF, and it will give you great results.
If you later want to make your layout more advanced, you could consider things like:
If you want the button to be a little wider (to have more breathing room before and after the text), try playing with the Padding, or set a MinWidth and MinHeight. (Don't use Width or Height if your button contains text. You might consider using them if your button only contains an image, but maybe not even then.)
If you're worried that the font might make the window so large that it no longer fits on the user's screen, and want to enable word-wrapping, then play around with MaxWidth and TextWrapping.
WPF's layout system is amazingly powerful. Learn it. Don't fight it by using hard-coded layouts and then complaining that your hard-coded layouts suck.

Control snapping in Visual Studio 2010

I'm sort-of just nit picking here, but maybe there's a simple solution which will save me some time.
When I'm drawing my Winforms GUI in the designer, controls snap to certain points. I can align the baseline of the text of one control to that of another, I can align the left and/or top of one control to another, etc. This is all great.
What's great too, is that the controls snap to other controls spaced with their margins. This means that if I'm making a vertical array of TextBoxes, then I can have them equidistant in my GUI - it looks less messy.
However not everything snaps correctly, or at all. Say I have put in my TextBoxes and now I wish to reduce the width of my form so that there is no white space between the edges of the TextBoxes and the edge of the form. Additionally, I want the distance between the edge of the TextBoxes and the edge of the Form to be the same on the left as it is on the right. If I drag the right edge of the form to the left, towards the TextBoxes it will not snap. I'm left with either calculations to work out what the width should be, or a juggling act to gradually reduce the width until the TextBox is snapping to both the left of the right.
I'm not saying this process is particularly difficult or time consuming. It's just that if it were to snap, the whole process would be infinitely easier.
Is there a built-in option in VS2010, or perhaps an extension? Or maybe I'm just doing it wrong in the first place?
The snap lines in the designer work perfectly when moving or resizing any of the built-in controls. The only time that they don't work is when you are resizing the form itself.
I agree that it would be extremely convenient to have snap lines here, as well. I wish I knew of a way to enable this. But unfortunately, I don't believe that there is one.
The workarounds are either to calculate the proper size mathematically, or guess at resizing the form then check your work by dragging one of the controls (and using the snap lines that appear). I go through the same "juggling act" that you describe on an unfortunately regular basis.
Whenever a control needs to be positioned so that it "snaps" to a form edge I usually move (or resize) the controls to the correct size first, and then change the controls "Anchor" property of those controls to be anchored to the corresponding form edge (even if the form itself won't be allowed to resize).
That way whenever I resize the form, the controls position relative to that form remains the same, simply resizing or moving the control as required (depending on the Anchor property chosen).
I completely agree that the ability to "snap" the form to controls when resizing the form would be extremely useful, but its normally possible to work-around using the anchor property in this way - the times when its doesn't work (such as when a form consists entirely of a column of text-boxes is a fixed height), I'm afraid you need to resort to calculations, but I find that most of my dialogs are resizable.

Resources