How to fix the order of items within a ToolStripContainer - winforms

I have a ToolStripContainer with a MenuStrip and a ToolStrip inside, both at the top. They're arranged as usual on Windows with the menu bar above the toolbar. Now, Windows Forms and DPI scaling support has always been a bit iffy. While everything looks fine at 100 %, I'm currently using 110 % DPI scaling and the menu bar and toolbar switch positions in the ToolStripContainer (I'd suspect it's the same with higher scaling factors, though):
My guess as to why this happens is that the designer places both controls at specific locations, even though they are arranged by the container, and with DPI scaling the ToolStripContainer gets locations for its children that would be consistent with placing the toolbar above the menu bar, as if someone dragged the bars around and reordered them (which is possible interactively, after all).
Short of replacing the MenuStrip with a MainMenu, is there a simple(ish) way of ensuring that regardless of DPI scaling the order of both remains consistent? I've got about 50 different windows to change in pretty much the same manner and would also rather avoid putting extra code into the codebehind file¹.
Things I've tried so far:
All changes in the designer have been applied at 100 % scale.
Change the z-order of the toolbar and menu bar in an attempt to control their order. This works with panels and docking, but doesn't apply to ToolStripContainer, apparently.
Docking the MenuStrip at the top. Doesn't work; the designer just removes Dock = None from the code and displays Top as the default value, but with scaling applied, it's back to Dock = None in the designer (and even without touching the Form in the designer, the result at runtime is the same).
¹ These are demo applications for a control library and the main point here is to keep the code clean and still providing a good experience out of the box. So a designer-only solution where the code is hidden away in already-awful code that no one reads would be preferable.

They are very frustrating components to work with. Because they can be dragged and moved it looks like the Location is the key, even though they behave a bit like they're docked. In OnLoad or OnShown have you simply tried resetting the desired location?
menuStrip.Location = new Point();
toolStrip.Location = new Point(0, toolStrip.Height);

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

ImageViewer in WPF

Hi everyone I would like to implement an ImageViewer (like the one in Facebook for example) in a WPF application
I already have a ListBox whith my pictures, it works well. But I would like to add pop "image full size" when the user double click on one of them. (something like in FB, with a fade out of the background etc).
Currently I'm thinking of to use a Window...Do you have a better idea of what I should use ?
i would probably use a window for that as well. Then you can easily put an opacity animation when the window loads to give it the fade in and fade out effect
You could also use a Popup control.
It comes with some some built in (but very limited) animations, like fade, see PopupAnimation.
I'd try that and if it doesn't fit your needs, I second bflosabre91 oppionion and would use a separate opacity animated window.
But bear in mind that with an additional window you could have negative side effects e.g always sync the window positions correctly, handle task switches (ie. correctly hide the window in the taskbar/tasklist)

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.

WPF, any easy way to work with different screen resolutions?

Given a WPF Application running full screen, a fair amount of controls some of which will animate from off screen to center. I was wondering if there are any special ways to save on the amount of time required to optimize an application for different screen resolutions?
For example, using Blend I've setup some text, which is originally off screen to scroll into view. Now in design mode the start positions are static. If resolution changes the start positions will obviously not be correct.
So I guess to correct this, during app startup. I need to
Check resolution
Move the text box to the desired start location
Adjust the storyboard as required, so the frames all have correct co-ordinates depending on the res of the screen.
I don't mind doing all of this, but if there is an easier way please share!
Thanks
In WPF layout of controls should be made in such way, that when size of window or content changes, controls automaticaly resize/reposition themselves to reflect this change.
This is highly affected how your layout is made, especialy by using specific panels, that do layout of their child elements.
This is also made obvious by using device-independent units for sizes, margins and sometimes positions of controls. And also allows different kind of zooming and scaling of whole UI without any need to redesign the whole thing.
If you are trying to position your controls absolutely, either by using Canvas panel or margins, your are doing it totaly wrong.
In WPF, scene is measured in abstract units, not pixels, and controls are freely scaled. There should be no problems to center something, or what?

WinForms Control position in Simplified Chinese

We have a WinForms application that includes controls such as picture boxes that are positioned on a form. The base application is in English.
We've translated this application to a number of different languages (French, Spanish, Danish, Greek, etc.) and most recently to Simplified Chinese. The translated application works perfectly on our operation systems (English).
One of our customers installed the application on their operation system, Windows XP in Simplified Chinese. The layout of our application is broken. Simply put, the elements are pushed to the bottom right by a factor that is proportional to the distance between the element and the top left corner. For example, an element at the top right corner in design view is pushed off screen to the right whereas the items at the bottom of the page are pushed downwards and to the right.
The application supports switching languages while in use. When the locale is en-US, there are no layout issues. When switching to Simplified Chinese, the issue appears, but only on the Simplified Chinese operating system. The screen resolution and DPI are the same.
Do you have any ideas? I'm sure it must be a simple configuration setting somewhere, but I have been unable to solve this issue.
The size of the system base font matters as well. Which is indeed something you can change on XP. This will invoke the form's auto-scaling logic, designed to ensure that the controls grow larger to fit the larger font size.
This is by design, controlled by the form's AutoScaleMode property. Don't change it, rescaling is important. Just make sure the form layout still looks good, use properties like Anchor and Dock, controls like TableLayoutPanel, FlowLayoutPanel. Or the Resize event for tricky ones.
Paste this into your form to test this logic without having to change system settings:
protected override void OnLoad(EventArgs e) {
this.Font = new Font(this.Font.FontFamily, this.Font.SizeInPoints * 125 / 96);
}

Resources