0

I have a feeling that Im am missing something obvious but:

I have a single row of pictures in a form, in theory the pictures could go on forever. I need a scroll bar so that the user can look at all of the pictures in the row. I know I need to enable auto scroll but I have no idea how to enable it. Can someone tell me how to enable it or something that I am missing?

If it helps this is the code i am using to generate the pictures:

private void imagePalletToolStripMenuItem_Click(object sender, EventArgs e)
    {

        MyPalletGui.Show();

        Dictionary<string,Bitmap> MyPallet = MyImageCollection.ToDictionary();
        int xcor = -50;
        int ycor = 0;
        foreach (Bitmap curtImage in MyPallet.Values){
            PictureBox myPicBox = new PictureBox();
            xcor += 50;
            myPicBox.Location = new Point(xcor, ycor);
            myPicBox.Width = 50;
            myPicBox.Height = 50;
            myPicBox.Visible = true;
            myPicBox.Image = new Bitmap(curtImage);
            this.MyPalletGui.Controls.Add(myPicBox);
James Thompson
  • 245
  • 1
  • 6
  • 13
  • 1
    I think there is a automatic way, otherwise you would need to use [view ports](http://support.microsoft.com/kb/186429). Eg Create a PictureBox1, 100 x 100 and populate PictureBox1 with sub pics, when these sub pictures exceed the width of PictureBox1 you need to show a horizontal scroll bar... – Jeremy Thompson Dec 03 '12 at 02:26
  • I think i get what your trying to say but can you elaborate or better yet show me an example. If it helps any ill add the code i am using to generate the pictures – James Thompson Dec 03 '12 at 02:29
  • We can guess all we want.. what is your exact issue? Are these pictures in PictureBoxes inside the form? If each picture has it's own PictureBox, then you can set the `AutoScroll` property of the Form to True.. which will add scrollbars for you as the pictureboxes exceed the form space. If the above isn't true, then there's more involved.. – Simon Whitehead Dec 03 '12 at 02:33
  • @SimonWhitehead~ thats exactly the issue. Each picture is in it's own picturebox but I have no idea how to "set the AutoScroll property of the Form to True" – James Thompson Dec 03 '12 at 02:36

1 Answers1

2

This code will do exactly what you want, it uses the Form as the ViewPort with AutoScroll:

public Form1()
{
InitializeComponent();

PopulatePictures();
}

private void PopulatePictures()
{
this.AutoScroll = true;

string[] list = Directory.GetFiles(@"C:\\Users\\Public\\Pictures\\Sample Pictures", "*.jpg");
PictureBox[] picturebox= new PictureBox[list.Length];
int y = 100;
  for (int index = 0; index < picturebox.Length; index++)
  {
  picturebox[index] = new PictureBox();
  this.Controls.Add(picturebox[index]);
  picturebox[index].Location=new Point(index * 120, y);
  if(x%12 == 0)
  y = y + 150;
  picturebox[index].Size = new Size(100,120);
  picturebox[index].Image = Image.FromFile(list[index]);
  }
}
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321