FlowLayoutPanel Scroll to Bottom doesn't work properly

kwok kan ho 21 Reputation points
2022-12-14T07:34:24.743+00:00

I am currently working on a FlowLayoutPanel and I find that the ScrollControlIntoView may not always work correctly.
It seems that when I add about 1,000 PictureBoxes into the FlowLayoutPanel, I use the ScrollControlIntoView to move to the last PictureBox, It fails to do so. But, if I use the vertical scrollbar context menu "Bottom", it works.
So, how can I use the scrollbar context menu "Bottom" programmatically?
Or, there is other method that I can programmatically to move to the last controls inside the FlowLayoutPanel.

Developer technologies | Windows Forms
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2022-12-15T02:47:26.537+00:00

    Hi @kwok kan ho , Welcome to Q&A.

    Scrolling the container to the bottom may not always work as expected if the container has many controls and not enough space to display them all at once.

    In this case, the scroll position may not be updated properly, or the container may not be able to scroll far enough to bring the last control into view.

    In this situation, you may need to consider using a different container, such as a List View or a DataGrid View, which are better suited for displaying large numbers of items.

    If you want to scroll to the bottom of the container, you can use the property of the container and set it to the maximum value.

    this is my code:

    private void Form1_Load(object sender, EventArgs e)  
    {  
        for (int i = 0; i < 1000; i++)  
        {  
            PictureBox a = new PictureBox();  
            a.Image = pictureBox1.Image;  
            flowLayoutPanel1.Controls.Add(a);  
        }  
        flowLayoutPanel1.AutoScroll = true;  
        flowLayoutPanel1.VerticalScroll.Value = flowLayoutPanel1.VerticalScroll.Maximum;  
    }  
    

    270744-image.png

    Best Regards,
    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. kwok kan ho 21 Reputation points
    2022-12-15T05:59:10.54+00:00

    Hi Jiale,
    Thank you for your reply and it works.
    As you mentioned in your reply, it is better to use ListView or DataGrid. I have tried the ListView before I use the FlowLayoutPanel, but I found that the images displayed in ListView is not good enough. Because I am try to display the shellFile.Thumbnail.ExtraLargeBitmap of some jpg images. So, is there a way that I can display good quality of image using ListView of DataGridView?

    The following is my code .

    private void LoadThunbnailFromPhotoList(DataTable oTblPhotoList, string filterDate = "")  
    {  
      int prevWidth = flpPhotoViewer.Width;  
      int prevHeight = flpPhotoViewer.Height;  
    
      Cursor = Cursors.WaitCursor;  
      txtMsg.AppendText("Photo Loading......", 1, true);  
    
      flpPhotoViewer.SuspendLayout();  
    
      if (flpPhotoViewer.Controls.Count > 0 )  
        flpPhotoViewer.Controls.Clear();  
    
      if (oTblPhotoList != null && oTblPhotoList.Rows.Count > 0)  
      {  
        List<PictureBox> picBoxes = new List<PictureBox>();  
        foreach(DataRow oRow in oTblPhotoList.Rows)  
        {  
          string imageFile = oRow["Filename"].ToString().Trim();  
          string shootingDate = oRow["ShootingDate"].ToString().Trim();  
          if (imageFile != "" && File.Exists(imageFile)  
           && (filterDate == "" || shootingDate.Equals(filterDate)))  
          {  
            PictureBox pictureBox = new PictureBox();  
            pictureBox.Name = "PicBox_" + i.ToString("0000#");  
            pictureBox.Tag = imageFile;  
            pictureBox.SizeMode = PictureBoxSizeMode.Zoom;  
            pictureBox.BackColor = gPicBoxBackColor;  
            pictureBox.BorderStyle = BorderStyle.FixedSingle;  
    
            toolTip1.SetToolTip(pictureBox, pictureBox.Name + "\r\n" + imageFile);  
    
            pictureBox.Width = picBoxWidth;  
            pictureBox.Height = picBoxHeight;  
    
            ShellFile shellFile = ShellFile.FromFilePath(imageFile);  
            pictureBox.Image = shellFile.Thumbnail.ExtraLargeBitmap;  
            shellFile.Dispose();  
    
            pictureBox.GotFocus += Control_GotFocus;  
            pictureBox.LostFocus += Control_LostFocus;  
            pictureBox.MouseClick += Control_MouseClick;  
            pictureBox.MouseDoubleClick += Control_MouseDoubleClick;  
    
            picBoxes.Add(pictureBox);  
          }  
        }  
        flpPhotoViewer.Controls.AddRange(picBoxes.ToArray());  
        picBoxes.Clear();  
        picBoxes = null;  
      }  
      // To make the first control visible   
      // and move to the top of flpPhotoView  
      flpPhotoViewer.VerticalScroll.Value = flpPhotoViewer.VerticalScroll.Minimum;  
      flpPhotoViewer.ResumeLayout();  
    
      txtMsg.AppendText("Photo Loading Completed......", 1, true);  
      Cursor = Cursors.Default;  
    }  
    

    Best Regards,
    KK


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.