What are the rules which I have to respect to make the Form scrollable...
I simple set the Property AutoScroll to true.
I also tried while Auto Scroll is true, to set AutoSize to true/false, but none of these worked... also tried to put Panel and added all components in there... still nothing...
Maybe using V or HScrollBar can help, but i really don't know how to link it with the Form...
form.AutoScroll = true;
formMainLayout.AutoScroll = true;
rootPanel.AutoScroll = true;
The content controls the scrolling. The scrollbars do not appear unless they are needed. Usually, there is a property available that you can set to force them to be visible always, and simply disabled until needed.
The AutoScroll property must be true, as you have already found. But then the content of the scrollable control must force the parent control to display the scrollbars. This part is up to how the controls are embedded within the parent.
Try these two experiments:
Place a Panel on your form and dock it to Fill. Set the AutoScroll property of the Panel to true. Into that panel, place a TextBox and set it to dock as Fill as well. Also set MultiLine to true. Run the application, and you will notice that the size of both is simply using the available space...no scrolling can occur because neither the Panel, nor its TextBox become larger than the space they occupy.
Perform the same steps as in #1, but this time, do not dock the TextBox. Instead, set it to a large size, something that you know will be larger than the amount of Panel that is visible. Running the application should now produce a scrolling Panel.
Hopefully this little test helps to demonstrate what is controlling the scroll on a form.
I was also having the same problem, I managed to fix it...
All the child controls inside the panel had a Left & Right anchor, and when I only set the anchor to Top, the scrollbars where working fine.
I am not sure as to why the Left and Right anchor (of the child controls) forces the panel not to show scrollbars.
But anyways... hope this will help anyone as of this date.
The AutoScroll property should work fine, but most likely you are not using it right: the bar appears only when required. Example: minimum Y of the Form is 0 and minimum Y of one of the controls in it (a TextBox) is -20.
If you want to include a scroll bar no matter what (controls inside the boundaries of the form or not), you can also do it. Sample code (from MSDN) for a vertical scroll bar:
// Create and initialize a VScrollBar.
VScrollBar vScrollBar1 = new VScrollBar();
// Dock the scroll bar to the right side of the form.
vScrollBar1.Dock = DockStyle.Right;
// Add the scroll bar to the form.
Controls.Add(vScrollBar1);
You need to set the properties for the parent panel.
Dock = Fill
Anchor = Top, Left
AutoScroll = true
That's it. Good luck! ^^
note its for vertical scroll
Turn On auto scroll property of your Form. insert one panel and
set panel width to the form width and panel height
equal to length of your total content or may be 1300 or 1500 as
required.
Place panel location as you want set panel anchor
property to top. place your all
content inside panel.
hope it will solve your problem
I had the same problem.
You have to add only this:
this.AdjustFormScrollbars(true);
Related
Well, I have a panel that contain some forms. I set panel Dock to Fill because it will be good when window maximized. I set the minimum size of panel too because the contents/forms quite be long to bottom.
Sadly, the scroll bar not shown even I set AutoScroll to True and set AutoScrollMinSize. How to configure this properly?
Let's say I have window height size only 300px but the panel (in window) that contain my forms have height about 600px. I need to always show the scroll bar.
Thanks in advance
Thanks all, I finally figured out what's wrong. I don't need set the MinimumSize of panel
Properties of Panel
Dock: Fill
AutoScroll: true
AutoScrollMinSize: 600px (Height)
It will be automatically force the panel to follow AutoScrollMinSize instead MinimumSize or default Size.
Make sure you don't have any child anchord to the right of the panel, as showed in MSDN documantation:
"There is currently a limitation in Windows Forms that prevents all
classes derived from ScrollableControl from acting properly when both
RightToLeft is enabled and AutoScroll is set to Yes. For example,
let's say that you place a control such as Panel—or a container class
derived from Panel (such as FlowLayoutPanel or TableLayoutPanel)—on
your form. If you set AutoScroll on the container to Yes and then set
the Anchor property on one or more of the controls inside of the
container to Right, then no scrollbar ever appears. The class derived
from ScrollableControl acts as if AutoScroll were set to No.
Currently, the only workaround is to nest the ScrollableControl inside
another ScrollableControl. For instance, if you need TableLayoutPanel
to work in this situation, you can place it inside of a Panel control
and set AutoScroll on the Panel to Yes."
Context:
I'm trying to build a form using Microsoft Prism guidelines.
I have two user controls that get injected into a form.
The form contains two panels that represent the containers which will hold the user controls.
The user controls get inject at run time via DI (I'm using the MVP pattern which is similar to MVVM but tweaked for Winforms).
The form has a default minimum size but the maximum size was not specified. The only way to resize the form is to make it fullscreen. It has the AutoSize property set to TRUE and the AutoSizeMode set to GrowAndShrink
Both user controls have the AutoSize set to TRUE.
All the containers inside the user controls have the AutoSize property set to TRUE, DOCK set to FILL and AutoSizeMode=GrowAndShrink. The maximum size for a control is not set.
The panels inside the the form are stacked one under another and have the Anchor property set to: TOP, LEFT,RIGHT, respectively: BOTTOM, LEFT, RIGHT.
Problem:
When resizing the form to fullscreen, I would expect that the user control to expand to fill the entire screen.
That is not happening.
The user controls do not change in size and I can't figure out a reason for it.
Thanks.
UPDATE
If I change the DOCK property of the panels inside the form to TOP, respectively
FILL, the panel will get resized, but the user controls inside the panels remain unchanged.
Forget about setting Dock and AutoSizeMode on your controls—just use Anchor and you will find it works just fine.
I never use AutoSize = true. I always have it at false (as a matter of fact I had to check some of my forms to verify that the AutoSize and AutoSizeMode properties even existed on controls on my forms).
In the scenario you describe, I would have the Anchor set to Top, Left, Bottom, Right, both for the panels and the controls contained within.
Set the Dock of the control in the panel also to Fill.
I'm making an instant messaging application in C#. The problem that I'm facing right now is that for the contacts list I've made a custom control extendinguserControl, which contains aFlowLayoutPanel`.
That panel will contain a list of userControls. I want to customize the VscrollBar, but no chance (not possible). Instead I have this genius idea to hide the VscrollBar from the FlowLayoutPanel, and make simple buttons (UP and down events). For this everything worked like a charm, but when I tried to hide the VscrollBar by making the property autoScroll = false , the buttons stopped working.
How can I hide the VscrollBar?
If you want to hide the the vertical bar, there are some possible solutions. ..
You could make an event for resize, controls add, controls remove and set all the child controls' width to flowlayoutpanel.width -20
You could add a panel to the flowlayoutpanel and set it to autosize and make the panels may width to flowlayoutpanel.width-20.
You could check if the width of the flowlayoutpanel is bigger than its real width (means vscrollbar appeared) , and resize the children that it'll/ld hide again
and if you are sure that your controls are smaller than the flowlayoutpanel's width, you simply could create a panel which covers the vertical bar. (use .BringToFront() to put it before the flowlayoutpanel's scrollbar)
I hope that I understood and perhaps have solved the problem
I'm not sure what it's called in the land of WinForms, but in web development terms, I'm looking for a frame type element that can be added to a winform.
I want a panel that is anchored top,bottom,left,right but if the form the panel is resized to a smaller size than the elements in the panel, scroll bars will appear around the panel allowing the user to see the contents of the panel without expanding the form.
I hope that makes sense, and that such a thing exists.
Thanks!
Yes, a Panel control. Set AutoScrollMinSize to the minimum size you want before scrollbars appear. Set AutoScroll to True. Set MinimumSize if necessary, it shouldn't be.
The controls inside the panel need to auto layout by themselves so they'll move as necessary when the panel gets smaller. Use their Dock or Anchor properties. If the layout gets complicated then switch to a TableLayoutPanel or FlowLayoutPanel control.
What about a panel? System.Windows.Forms.Panel
You are looking for a "Panel" control. Just set the "Dock" property to get docking going..
You add a Panel to your form and set Panel.Dock = Fill. Your Panel will auto-resize when you resize the form.
Set Panel.AutoScroll = True
Then, you add controls to your Panel. Set the controls' Dock property accordingly. Now, when you resize the form, scrollbars will appear if controls are covered up.
There are a couple of different panels in the standard windows controls that do what you want... just look in the toolbox when editing a windows form, under 'container'
What do you want it to contain? A web page, or just windows form controls?
I'm developing an app for Windows Mobile 5.0 and above, with C# and .NET Compact Framework 2.0 SP2.
I have a WinForm with two panels inside (upperPanel and bottomPanel). I want that upperPanel always fill 2/3 of form's height, and bottomPanel fills 1/3 of form's height. Both panels will fill completly form's width.
I've used this:
upperPanel.Dock = Fill;
bottomPanel.Dock = Bottom;
But upperPanel fills the form completly.
How can I do this? I want, more o less, the same gui on differents form factors and on landscape or protrait mode.
Thank you.
What you need to do is to put the bottom panel on first and set its Dock property to Bottom. Then set the panel's height to be 1/3 of the form's height. Finally, add a second panel and set its Dock property to Fill. The key here is that you want to add the control that will fill the remaining area to be added last. Alternatively, you can play around with the Bring to Front and Send to Back commands in Visual Studio to get the designer to cooperate.
You may also need to hook the OnSizeChanged event for the form and re-set the height of the bottom panel to account for layout changes. It's been a little while since I did compact framework programming, so I'm not sure.
Right click on the upperPanel and select Bring To Front. However, I don't think this will give you the result you want. When you resize, the bottom panel will remain the same height, while the upper panel will stretch to fill the form.
Using your docking settings, with this code might do the trick:
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
this.bottomPanel.Height = Convert.ToInt32((double)this.Height / 3.0);
}
Set both panels to "not anchored". That is: Remove Dock-Value and clear the Anchor property. Then, move the controls so they are sized the way you'd like them to be sized.
After that, upon resizing the form, they should resize relatively.
EDIT
Oops, just tried it and sure it doesn't work. I mixed this up with a solution that automatically keeps controls centered within the window...
Well, I'd guess you then have to create a handler for the form's Resize event and manually align the controls after the form has been resized.
Go to Tools, Other Windows, Document Outline. Find the two panels, and swap the order of them. The control that has DockStyle.Fill has to come first for it to be docked correctly. (or last.. never sure which one it is, but it is one of them :p)
This won't solve the always 1/3 and 2/3 issue though... cause the bottom panel will have a fixed height (unless I am mistaken). I think maybe the TableLayoutPanel supports this though...
Update: As noted in the comments, that panel doesn't exist in the compact framework. So, I suppose the easiest solution to this problem would then try to use the docking, but update the height of the bottom panel whenever the size of the form changes.
If you want this to work perfectly you'll need to add some code to the Resize event of the Form which then specifically works out the relative sizes and places the controls in the correct place after a resize.
If you're not worried about losing precision and the forms aren't going to move much you can avoid this by using some relatively smart anchoring. Essentially you're going to have to select a "grower" (the part of the form that gets bigger, the bigger the form gets). In this scenario I would probably anchor the top part to Top | Left | Right and the bottom part to Top | Left | Right | Bottom. This would mean that the lower part of the form will get bigger if the form is expanded. In most cases this is acceptable. If it isn't use the Resize event and some code.
The easiest way to do this is to nest panels. Just set up panels for top bottom and fill. Then use panels within those panels to do the same. The only issues I've had therein are datagrid resizing, which is always a pain anyway. in that case, you have to use some code to resize the datagrid control on the form resize event.
I would like to add a point to #jasonh answer.
For the panel that occupies 2/3 of the form, you will have to set the AutoScroll property of the panel to true.
This will enable the panel to display scroll when the control size exceed the visibility to the user and also ensure the visibility of the smaller panel which is 1/3 of the forms height.
You can get the required design by using nested panels along with few setting with Anchoring and Docking Properties.Follow the following steps:
1) Add the Form and put a Panel1 on it. Set its Dock Property as 'Fill' and ResizeMode as 'Grow&Shrink'.
2) Add Second panel2 and set its Dock Property to 'Bottom', Set the Height and set the Anchor property to 'Top,Left'.
3)Add Third panel and set its Dock Property to 'None', Set the Height and set the Anchor property to 'Top,Bottom,Left,Right'.
Save and Compile. Now all the panels Would maintain their relative Positioning With resizing.