Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: C# Scrollbar form
i have some objects as pictureboxes, and the user can decide on programm start
how many object should be generated.
Now there is the problem if the user says he wants more than 7, the
pictureboxes get out of the form.
I want to implement like if you add more than 7 objects, a scrollbar should be visible, and you can scroll until the last object.
Thank you
Greets Niko
Posted 31-Oct-12 5:39am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You can use the features of the System.Windows.Forms.ScrollableControl. The class is a base class for System.Windows.Forms.ContainerControl, and this one is the base class for System.Windows.Forms.Form, so you can do it with your form.
But better yet, create some control class derived from System.Windows.Forms.ContainerControl and use auto-scrolling in it; put all your objects you've mentioned as a children of this control. And insert this control in your form.
Please see:[^],[^].
This article explains how to use auto-scroll very clearly:[^].
Marcus Kramer at 31-Oct-12 11:43am
+5. Nice.
Sergey Alexandrovich Kryukov at 31-Oct-12 13:23pm
Thank you, Marcus.
(And it really works.)
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Use a panel. Then add your pictureBoxes to the panel. Make sure you set AutoScroll = true.
public class Form1 : Form
        private System.ComponentModel.IContainer components = null;
        private System.Windows.Forms.Panel panel1;
        private string _dir = @"C:\img";
        private string _pat = "*.bmp";
        public Form1()
        private void LoadPictureBoxes()
            int count = 0;
            foreach(string path in Directory.GetFiles(_dir,_pat))
                PictureBox pb = new PictureBox();
                pb.Image = new Bitmap(path);
                pb.Height = 100;
                pb.Width = panel1.Width - 5;
                pb.Left = 0;
                pb.Top = count * 100;
        private void InitializeComponent()
            this.panel1 = new System.Windows.Forms.Panel();
            // panel1
            this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel1.Location = new System.Drawing.Point(0, 0);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(505, 659);
            this.panel1.TabIndex = 0;
            this.panel1.VerticalScroll.Enabled = true;
            this.panel1.AutoScroll = true;
            // Form1
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(505, 659);
            this.Name = "Form1";
            this.Text = "Form1";
        protected override void Dispose(bool disposing)
            if (disposing && (components != null))

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 410
1 Sergey Alexandrovich Kryukov 370
2 Maciej Los 285
3 ProgramFOX 275
4 CHill60 260
0 OriginalGriff 360
1 ProgramFOX 265
2 Maciej Los 235
3 CHill60 200
4 RyanDev 170

Advertise | Privacy | Mobile
Web03 | 2.8.150331.1 | Last Updated 31 Oct 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100