Add vertical scroll bar to panel
Assuming you're using winforms, default panel components does not offer you a way to disable the horizontal scrolling components. A workaround of this is to disable the auto scrolling and add a scrollbar yourself:
ScrollBar vScrollBar1 = new VScrollBar();
vScrollBar1.Dock = DockStyle.Right;
vScrollBar1.Scroll += (sender, e) => { panel1.VerticalScroll.Value = vScrollBar1.Value; };
panel1.Controls.Add(vScrollBar1);
Detailed discussion here.
adding a vertical scrollbar for a WinForm panel
You can add a FlowLayoutPanel and set the following property to that:
flowLayoutPanel1.FlowDirection = System.Windows.Forms.FlowDirection.TopDown;
flowLayoutPanel1.WrapContents = false;
flowLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
flowLayoutPanel1.AutoScroll = true;
and you can see the scroll, you can use the Panels and design your application.
Taken from:
How to make Winform scrollable in C#
C# - how to set up vertical scrollbar in a panel?
Assuming your panel is named panel1(or else replace it),try the following and you shall see the scrollbars:
Button b = new Button();
b.Location = new Point(20, panel1.Height + 20);
panel1.Controls.Add(b);
I just added a button for demonstration purposes you add whatever you need.
Or as i said in the comments using the autoscrollminsize:
panel1.AutoScrollMinSize = new Size(panel1.Width + 50, panel1.Height + 50);
Panel scroll vertically
Since you have AutoScroll = true, you shouldn't have to do anything. Any control that you place in the panel that is below the visible boundary will automatically create the appropriate scroll distance in the panel.
If you want to manually override that, set AutoScroll = false and set the size of the canvas yourself using the AutoScrollMinSize property, example:
panel1.AutoScrollMinSize = new Size(0, 1200);
You might want to consider anchoring the panel to the four sides of the form as well, or dock-fill, since it looks like a resizable form. Again, the panel will handle the scrollbar size for you.
Add Horizontal Scrollbar for Panel
Try setting programmatically the panel box properties:
panel.Autoscroll = True
panel.VerticalScroll.Visible = True
panel.HorizontalScroll.Visible = True
Try adjusting your panel size then don't adjust the size of width of each data inside of it. I suppose it is because the data is EXACTLY fitted on your panel box.
vertical ScrollBar in TableLayoutPanel
The code you posted works for me, just set autoscroll to true and then tlp.HorizontalScroll.Visible = false;
EDIT:
Just noticed the horizontal bar anyway, my resolution hidden it. Anyway, set the autoscroll to false, then addtlp.AutoScroll = true;
after tlp.ResumeLayout();
in Add10Lines() method.
Adding a vertical scrollbar to a panel if drawn area is too large
The problem is that you are using graphics to draw on the panel. These are not controls, so they don't cause the panel to grow. you should create two panels - PanelA contains PanelB. PanelA has AutoScroll=true
, but you draw on PanelB. As you draw, you also set the height of PanelB, so that when it gets bigger than PanelA, PanelA gets a scroll bar.
Vertical & Horizontal scrollbars in a panel
I ran into something that sounds like what you describe. I wanted just a Vertical scroll because it was going to contain many many things eventually, but not a Horizontal scroll. I used a table layout panel; set the Panel's Vertical size so that the VScroll shows; set the width to accommodate what will go in there plus whatever margin or gutter your code will use.
Then, in the TableLayoutPanel
set the width of the scrolling panel's to absolute (I used 2 pixels more than the panel.width). If/when the user resizes, all the extra size gets distributed to everything else. Basically dont let the scrolling panel's width change. Might have to/want to set a minimum form size too.
The things I was adding were all the same width, if yours vary, you might have to set it to accommodate the widest.
Not sure if you are encountering the same thing, but sure sounds like it.
Related Topics
C# 8 Switch Expression with Multiple Cases with Same Result
C# Linq Where Date Between 2 Dates
Wpf/Mvvm - How to Handle Double-Click on Treeviewitems in the Viewmodel
C#: Detecting Which Application Has Focus
Get Application's Window Handles
Wpf Binding - Default Value for Empty String
Automatically Rename a File If It Already Exists in Windows Way
How to Set Read Permission on the Private Key File of X.509 Certificate from .Net
Best Way to Dynamically Set an Appender File Path
Wpf Equivalent to Textrenderer
Is There a .Net Way to Enumerate All Available Network Printers
How to Change Originating Ip in Httpwebrequest
How to Read Image Pixels' Values as Rgb into 2D Array
How to Set/Change/Remove Focus Style on a Button in C#
Getting the Index of a Particular Item in Array
Linq: Passing Lambda Expression as Parameter to Be Executed and Returned by Method