In C# (.Net) you can create a panel and set autoscroll to true. you can then add controls into it, including beyond it's size, then it will scroll.
I was wondering if when using the real WinAPI in c++ (not .net) how one could acheive the same result.
Must I keep track of the controls inside and move them all when I scroll or is there an easier way like in C#.
Thanks
For an edit control (textbox), you can supply ES_AUTOVSCROLL when creating it via CreateWindow or CreateWindowEx. For adding a scrollbar for multiple controls in a window, I believe you have to do it manually. Write a function to sum the vertical height of all the child controls + spacing between them and if it's smaller than the window, add a vertical scroll bar, if it's larger, remove the vertical scroll bar (if it exists). Be sure to call this after adding/removing child controls and on window resize.
Related
I am trying to place a panel that I want to have multiple textboxes inside of. I want this panel to fit inside the form and the panel to have a scrollbar. I have autoscroll on and its working as intended, but I want to be able to add controls below the size of the panel. Is there a way I can "full screen" the panel and place items in it or even make the scrollbar a set length instead of just the amount needed to fit the contents. I want to be able to place things underneath what I have there.
Dragging items onto the panel doesn't allow enough room to make things tidy
As an option, you can set the AutoScrollMinSize, in your case, the height of it to a larger value like 1000 temporarily, which sets the virtual (Scrillable) height of the control to 1000. The property determines the minimum size of scrollable area.
As another option, you can AutoScrollMargin, in your case, the height of it, to something like 500 temporarily, which provides an extra 500 pixels space at bottom of the panel. The property determines the minimum margin that should be between the edge of the child control and the edge of the scrollable control.
Then when you are done with the design, just right click on the property and Reset the value of it, to let the control calculate the scroll size.
And I assume you are aware of some obvious workarounds like:
Design the form (including the panel) in a larger size, setting proper dock and and anchor properties for controls, later set the size of form to desired size at run-time, or at design time (after you are done with the initial designs).
Or another workaround, could be just dropping the controls in the panel, and then selecting them and moving them using arrow keys.
I have a SplitContainer on my form that has its Dock property set to Fill. It contains several child controls, many of which have event handlers attached to them. Later I decide to put a StatusStrip at the bottom of my form. Guess what, I can't set the StatusStrip to dock to the bottom of my form. The SplitContainer will continue to Fill the entire form. Even though the StatusStrip apparently gets docked to the bottom, it actually hides the bottom part of the SplitContainer behind it.
The only around it is to CUT the SplitContainer and then PASTE it back. Cutting the SplitContainer makes the StatusStrip the only control on my form and thus lets it capture the bottom docking. Afterwards, pasting the SplitContainer allows it to fill the remaining area. In short, docking uses First Come, First Serve method.
Now since my controls have lots of event handlers attached to them, cutting and pasting becomes a nightmare for me. Having my project in C# means I have to attach all those event handlers manually.
Is there a better work around?
This is a z-order issue between the splitter and the statusstrip. When you have a control you want to dock fill and one or more controls you want to dock top, left, right, or bottom, you have to have the fill control be the first in the z-order.
The better way is to open the Document Outline tool, select the SplitContainer and use the up or down buttons to change its z-order.
I should add that in Winforms the z-order is specified by the order in which you add controls to the Controls collection. That order determines the order the associated system controls are created, hence their z-order. Using the Document Outline tool to alter z-order simply causes the generated code to be re-ordered.
I am developing an application that will use touch screens to navigate for use in a warehouse environment.
There is a need to present a list to the users and because this list could be quite long, the user will at some point need to scroll down the list.
As you all know the system scroll bars for panels and textboxes etc is quite small and I would like to know if I can either resize the controls that make up the scroll bar (the little arrows at the top and bottom, and the place holder bar in the scroll bar itself), or if I can create a couple of buttons that can scroll up and down my control for me?
Thanks,
Karl
Answer pulled from this post.
Check this out:
Winforms - Adjust width of vertical scrollbar on CheckedListBox
Worth mentioning too:
.NET Compact framework - make scrollbars wider
More of the same, but this time with a better solution through the use of the scrollbar control:
Change the width of a scrollbar
Another one in which the guy teaches how to create your own scrollbar control (interesting):
Set the Scrollbar width of a DataGridView
The last one (worth trying):
Is there a way to get the scrollbar height and width for a ListView control
You can add your own scroll buttons and programatically scroll like so:
myPanel.VerticalScroll.Value++;
Imagine a bar graph with horizontal bars that may be very wide. I have a Panel on a Form where I want to display these bars and scroll and zoom them. The Form, and thus the Panel, can be resized. The bars are dynamically created from a database. Each time the user zooms in or out, all bars have to be created anew to adjust their sizes on the Panel.
I use Label controls to create these bars, but the problem applies to all other controls as well: If I zoom in far enough, my bars will eventually exceed the magic 16 bit border of control sizes (>65536 pixels). This makes it impossible to simply create all the controls on the panel at start and let the panel handle the scrolling.
My idea: Clear the Panel of all bar controls and create only the ones that are visible in the current view window, according to the current position of the scroll bars and the zoom level. The bars exceeding far from the visible view will be cut short just outside the Panel, so their maximum size is limited by the Panel size.
My questions:
At which Panel event(s) should this clear/create process take place best?
There could be thousands of controls, so it should be as seldom as possible.
Is there a better way to handle this? Maybe I got it all wrong from the start.
This problem arises not only with huge controls but also when smaller controls are very far apart (>65536 pixels) on a Panel, so I think a good solution may be helpful for many projects.
I wouldn't like to have to create / destroy controls, or hide / resize controls just for their click events. It's quite easy to create a UserControl and override the OnPaint method to draw the bars, and override the OnClick or OnMouseXxx events.
Since you already know the positions of the bars in "virtual space", it's easy to map the location of the mouse cursor to a bar (or a click outside a bar).
I know you said winforms is Mandatory, but I really think you should look into the wpf viewbox. You can host a wpf element in winforms. So everything else can be forms related, and you have a panel that hosts and displays your controls. I could write up a quick example that might demonstrate this for you, but if you have no intent of going this way I really don't want to waste my time.
You could create a metafile (vector graphics), show that in an image control, and manually determine which logical element is clicked.
I'm new to Windows Forms in Visual Studio, and I am wondering how to automaticly resize controls to the window size.
Say, I have 2 controls in a panel, a List Box and a Button. I want the button to dock to the bottom, and I want the List Box to fit the rest of the space. when the window resizes, the button should be at the bottom (as expected with docking), and the list box should stretch down to the button.
Is there a way to do this without any code?
Thanks.
Dock is pretty easy to use, but I recommend using the Anchor properties instead. Resize your form to a reasonable size in the Designer. Then, place your controls to look the way you want. Then, decide which controls should resize with the form and set the Anchor property as follows:
If you want the control to resize with the form in width, set the Right anchor.
If you want to resize height, set the Bottom anchor.
If you want the control to stay right when the form resizes, unset the Left anchor.
If you want the control to stay bottom when the form resizes, unset the Top anchor.
The problem I have with Docks is that they sometimes act funny when controls are not declared in a specific order, and to get the effect you want, sometimes you have to create extraneous panels just to hold controls.
It really gets messy when you want to maintain the aspect ratio of each control. One way, which is not really up to the mark if you want to get into fixing the details, is to use TableLayoutPanel and use Dock and Anchor wisely to achieve what you want.
Use the dock and fill options on the controls. Look under properties for each object, and containers if they are in any.
You can use SplitContainer
Google for examples. Here is one
Try setting your ListBox's Dock property to Fill.
You'll need to watch for one thing though: by default the ListBox will size itself to display whole list items. If you resize the control so that it displays a partial item it will adjust itself so it will display a complete item. This can make the control appear to lose its 'Dock'ing behavior. The solution for this is to set the ListBox's IntegralHeight property to false, which specifies that the control not resize itself to fit complete items.