Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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

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
 
Share this answer
 
Comments
fjdiewornncalwe 31-Oct-12 11:43am    
+5. Nice.
Sergey Alexandrovich Kryukov 31-Oct-12 13:23pm    
Thank you, Marcus.
(And it really works.)
--SA
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);
        }
    }
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900