Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Scrollbar form
hi,
 
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
good
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:
http://msdn.microsoft.com/en-us/library/system.windows.forms.scrollablecontrol.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.containercontrol.aspx[^].
 
This article explains how to use auto-scroll very clearly:
http://www.bobpowell.net/understanding_autoscroll.htm[^].
 
—SA
  Permalink  
Comments
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.)
--SA
Rate this: bad
good
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()
        {
            InitializeComponent();
            LoadPictureBoxes();
        }
        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;
                panel1.Controls.Add(pb);
                count++;
            }
        }
        private void InitializeComponent()
        {
            this.panel1 = new System.Windows.Forms.Panel();
            this.SuspendLayout();
            // 
            // 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.Controls.Add(this.panel1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
        }
 
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
    }
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 8,149
1 Sergey Alexandrovich Kryukov 7,287
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,910


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 31 Oct 2012
Copyright © CodeProject, 1999-2014
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