如何启用自动滚动?

时间:2012-12-03 02:22:49

标签: c# winforms autoscroll

我有一种感觉,我错过了一些显而易见的东西,但是:

我在一张表格中有一排照片,理论上这些照片可以永远存在。我需要一个滚动条,以便用户可以查看该行中的所有图片。我知道我需要启用自动滚动但我不知道如何启用它。有人可以告诉我如何启用它或我错过的东西?

如果有帮助,这就是我用来生成图片的代码:

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);

1 个答案:

答案 0 :(得分:2)

此代码将完全按照您的意愿执行,它使用Form作为ViewPort和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]);
  }
}