How do I know when the current system scrollbar width changed? - c#

I need to move controls around when the scrollbar's size change (System.Windows.Forms.SystemInformation.VerticalScrollBarWidth).
I'm creating a control with custom scrollbars that go over the normal ones. This implies creating a new UserControl (not inheriting a built-in control) and playing around with panels so as to hide the normal scrollbars.
The custom control must have one "outer" panel at the right size, this one containing an "inner" panel larger than the outer panel, so the scrollbars do not appear. How much larger depends on System.Windows.Forms.SystemInformation.VerticalScrollBarWidth and HorizontalScrollBarHeight as was already answered. But then I must know if that changes when my app is running, as improbable as it seems.
This question is related to:
How do I know the current width of system scrollbar?

I'm currently trying to achieve something similar.
I'm running Windows XP SP3, "Classic" style, and upon changing just the scrollbar width of the current design, my override of OnSystemColorsChanged() (in a class derived from Control) gets called four times.
Why it's four times I don't really know, I suspected it might be because there's four properties in there that seem to depend on that setting:
SystemInformation.HorizontalScrollBarArrowWidth
SystemInformation.HorizontalScrollBarHeight
SystemInformation.VerticalScrollBarArrowHeight
SystemInformation.VerticalScrollBarWidth
But all of these already hold the new value at time of the first call. So I'm not 100% sure what's going on here. But it looks like OnSystemColourChanged() should rather be called OnSystemInformationChanged().
Hope this helps ...

One must listen to Microsoft.Win32.SystemEvents.UserPreferenceChanged.
As takrl mentioned, OnSystemColorsChanged gets called, but only once for me (Windows7, Framework 3.5)

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.

FixedPanel in SplitContainer causes ignorance of size

I'm having the following code to set the width of the first panel to the width I need (for some reason I need to divide the expectedWith by two to get the actual width to be my expectedWith - don't know why).
splitContainer1.SplitterDistance = expectedWith / 2;
The next thing I need is that the first panel is fixed, means that if you resize the window, only the second panel gets larger and the first stays in size. To achieve this I use the following line:
splitContainer1.FixedPanel = FixedPanel.Panel1;
Problem now: it seems that the FixedPanel-property completly ignores the size of the first panel. It doesn't matter which value I set the SplitterDistance-property to if I used the FixedPanel-line. It is always the same. It doesn't even matter if I set SplitterDistance in the form designer.
Is there a solution to this?
I can answer my own question. This only happens if the content of the appropriate panel is using DockStyle.Fill in its Dock-property. The solution is to set the DockStyle.Fill value right after setting FixedPanel. This was also responsible for the "divide by two"-behaviour explained in the question.
Thanks to John Willemse.
I ran into this same problem, and was tempted to use the original poster's advice and just divide the desired SplitterDistance by two, as it always seemed to be (nearly but not exactly) twice the width that I asked for.
I tried changing the DockStyle of the children of the two panels to None before setting the SplitterDistance, but in my case it did not have any impact on the problem.
I solved the problem by changing when the SplitterDistance gets set. Originally I was setting the SplitterDistance on the panel before calling Form.Show(). Setting the SplitterDistance after the form is shown seemed to cure it.
Since you only want a fixed first panel and a dynamic second one, couldn't you use a TableLayoutPanel instead of a SplitContainer?
The DockStyle.None is a step in the right direction. But it needs 3-6 iterations (shaking the Splitter), until the Panel-Window has the expected dimensions. Strange!
I've figured this out. I needed to have a page open and load the splitter positions from the registry but was very frustrated with the way the watch I set up showed that it kept going back to the design time setting of the control.
To fix it all you do is change the property of "FixedPanel" from "None" to panel1 or panel2. Which is up to you. it only really comes into play when the minimum setting is not small enough and/ or when you have a splitcontainer within another sizable container. the fixed panel stays fixed then. Both panels can be resized as you choose using a mouse though so the word fixed is a little ambiguous in that regard.
so take the design time control. drop it on the form. resize it to fit your requirement and anchor it as necessary. nothing else except making one of the panels fixed. There is a property called "IsSplitterFixed" don't touch it, it's one of the reasons this control becomes unstable and must stay false, the panel.minsize properties should be set at design time based on requirements and you will always have to take cognisance of their values.
live happily ever after!

Manually Activate/Deactivate SnapLines For Custom Controls

At this point, I almost never want to design a control with rich design-time support again. That said...
I am already overriding the SnapLines property in my ControlDesigner-derived class to manually forward out various snaplines from the child controls of my control -- the text baseline (pink) snap from the labels and comboboxes; the text inset snap from the labels; the top, bottom, left and right snaplines from the comboboxes. Those snaplines activate when the control itself is moved around on the form and when other controls are moved around it.
What I need is the ability to tell the designer to activate the snaplines and then deactivate them while I'm doing an internal move or resize of the underlying controls.
My comboboxes are resizable through overrides of OnMouseDragBegin, OnMouseDragMove, and OnMouseDragEnd in my control designer. The magic bullet I'm looking for is something I can call in Begin to tell the designer to show the snaplines and something in End to tell it to stop.
Allowing people to resize and move the internal controls at design time is kind of useless if they don't show snaplines for each other or for external controls.
As with stuff like this, it's incredibly hard to search for. I've found one post on a forum where someone asked this exact question that had (of course) no responses. That's about it.
Obviously the issue of actually snapping to the snaplines when/if they're shown remains. Just being able to see them would be a nice start.
Any ideas?
The best way to do what you want todo is to create a Design Surface MSDN Reference
I've used this to create my own design surface for my application so that the clients can customize the forms.
Hope this helps,
Johan J v Rensburg

ListView auto size to show all items

What I want to create is a list view that will resize itself in order to show all items. Normally I would use AutoSize, but this won't work here. Any other options how can I make ListView expand and shrink to fit all?
You will have to manually calculate your desired height and set it as items are added/removed.
Detecting item addition or removal isn't directly supported - so you'll either need to create your own Add/Remove Item calls for clients to call, or handle LVN_INSERTIEM type messages from WndProc.
Auto-sizing controls are usually trickier to use - as you have to track Min/Max sizes, allow room on the owner, and usually add to an awkward usability point for users. Only do something like this if typical solutions (i.e. scroll bars) truly can't work for your need.
"AutoSize" property is not supported for the "ListView" control. As #JohnArien mentioned, you will have to programmatically re-size your list view control's size according to the number of items available. But be warned that this may not be a good idea in terms of visual appeal of the Form. Your form design might look ugly if you change the size in run time. More over these types of controls are expected to expand their client area within the given size with the help of scroll bars. I would strongly suggest you to reconsider this option.

User controls do not properly fit on the screen

My application has several controls. Like in one screen has TreeView on left side, GridView with paging in the middle and 4 buttons at right side. The controls properly appear when the form is in a maximized state, but if I minimize it the controls do not properly fit on the screen.
I tried with different different tricks like table layout.. in dat I added a panel, etc...
But I could not solve the problem.
How can I create such type of screens which fits independently of size of my window?
Thanks
I had the same problem a while ago. In my case i had a Button and a ListView within a GroupBox that was inside a SplitContainer, which was within a UserControl on a TabControl. I wanted the button on the top right located and anchored and the ListView took the space leftover, so i couldn't dock it. Instead it was anchored in all four places.
In my case my button and the listview are worked and behaved correctly in the designer, but in my running app the button was positioned to far on the right and the ListView size had also a too much width.
For a first bugfix i did some iterations about positioning the button in the designer a little more to the left, check it running mode, re-align the button in designer from the impression i had in the last run.
So i got it to work and started over with some other thing i had to do in my app. After a while i got a new feature request and needed another button within this messy thing. This time i did a complete rebuild of the gui elements on a new usercontrol, just to see if the problem reappears. To my amazement this gui mock just behaved as expected.
Within my code i didn't anything about changing the location, size, anchors, docks, etc. So the problem had to be within the InitializeComponents() code created by the designer.
I started with a diff of both versions and couldn't see any big differences (there were a lot of them, but only minors like size, location or name). So i started to put code from my crazy usercontrol into the mock and running the mock in my app. After several copies the problem also appears in my mock, so i got the root cause of the problem.
What do you think, which property leaded to the crazy behaviour?
It was the MinimumSize of my SplitContainer!
So to get really the root cause of your problem you should start over with an empty UserControl (or Form) and just place all the elements on it with the desired behaviour (size, location, anchor, dock).
Nothing more!
Then test if this mock behaves the way you want and if not, post this code here and tell us what you expected to see.

Categories

Resources