title: How to scroll Flow layout panel content as per mouse position
body: <p>i have taken one Flow layout panel and placed multiple picture box inside in it. now i want when i will place my mouse at the right or left most edge of the Flow layout panel then rest of picture will scroll out. just think about windows 8 start screen where many tiles appear in screen and when we place mouse at right most edge on the screen then rest of the tiles scroll out. i want to simulate same thing in windows form with Flow layout panel. </p>
  
  <p>i want my Flow layout panel will not show scroll bar but images will scroll out when i will place mouse right or left most part on the panel. here is my screen shot
  <img src="https://i.stack.imgur.com/14eJG.png" alt="enter image description here"></p>
  
  <p>some one told me to do it this way...here is bit code
  <strong>Set AutoScrollPosition property in MouseMove event of Panel.</strong></p>
  
  <pre><code>private void panel1_MouseMove(object sender, MouseEventArgs e)
  {
      panel1.AutoScrollPosition = new Point(e.X, e.Y);
  }
  </code></pre>
  
  <p>but this trick was not good. AutoScrollPosition works when scroll bar is visible but in my case i do not want to show scroll bar with Flow layout panel. i want smooth scrolling images from left to right or right to left. anyone can help me to achieve what i am trying to do....if possible guide me with respect of coding. thanks</p>
  
  <h2>EDIT</h2>
  
  <p>Here i am giving my full code after modification following @Taw suggestion but it is not working fine....rather flickering found when picture move. anyway here is the full code.</p>
  
  <pre><code>namespace ScrollTest
  {
      public partial class Form1 : Form
      {
          public Form1()
          {
              InitializeComponent();
              flowLayoutPanel1.MouseMove += MouseScroll;
  
              foreach (Control x in this.Controls)
              {
                  if (x is PictureBox)
                  {
                      ((PictureBox)x).MouseMove += MouseScroll;         
                  }
              }
          }
  
          int near = 33;
          private void MouseScroll(object sender, MouseEventArgs e)
          {
              Point mouse = flowLayoutPanel1.PointToClient(MousePosition);
              Rectangle C = flowLayoutPanel1.ClientRectangle;
  
              int dLeft = mouse.X - C.Left;
              int dTop = mouse.Y - C.Top;
              int dRight = C.Right - mouse.X;
              int dBottom = C.Bottom - mouse.Y;
  
              int dX = dLeft &lt; near ? dLeft : dRight &lt; near ? -dRight : 0;
              int dY = dTop &lt; near ? dTop : dBottom &lt; near ? -dBottom : 0;
  
              if (dX != 0 | dY != 0) scrollFLP(dX, dY);
          }
  
          void scrollFLP(int deltaX, int deltaY)
          {
              flowLayoutPanel1.Left += getSpeedFromDistance(deltaX);
              flowLayoutPanel1.Top += getSpeedFromDistance(deltaY);
              System.Threading.Thread.Sleep(11);
          }
  
          int getSpeedFromDistance(int delta)
          {
              int sig = Math.Sign(delta);
              int d = Math.Abs(delta);
              if (d &gt; near / 2) return sig;
              else if (d &gt; near / 3) return near / 10 * sig;
              else if (d &gt; near / 4) return near / 8 * sig;
              else if (d &gt; near / 5) return near / 5 * sig;
              else return near * sig;
          }
      }
  }
  </code></pre>
  
  <p>basically i am trying achieve something like suppose i have <code>flow layout panel</code> and which has many picture box inside it with many images as the screen shot but scroll bar should not show rather scroll will happen automatically when i will place my mouse at the top or bottom of the flow layout panel like <code>carousel</code>.</p>
  
  <p>see this picture of your application
  <img src="https://i.stack.imgur.com/2JfvO.png" alt="enter image description here"></p>
  
  <p>when place my mouse at the right end then it scroll and form background shown which i do not want. i want picture box will scroll &amp; scroll upto last one not more than that.</p>
  
  <p>any idea how to do it. thanks</p>
  
  <h2>2nd Edit</h2>
  
  <p><img src="https://i.stack.imgur.com/xfeCz.png" alt="enter image description here"></p>
  
  <p>this code i added as per your suggestion</p>
  
  <pre><code>public Form1()
              {
                  InitializeComponent();
  
                  for (int i = 0; i &lt; 666; i++)
                  {
                      PictureBox pan = new PictureBox();
                      //pan.MouseMove += MouseScroll;
                      //pan.MouseLeave += outSideCheck;
                      pan.Size = new Size(75, 75);
                      pan.BackColor = Color.FromArgb(255, (i * 2) &amp; 255, (i * 7) &amp; 255, (i * 4) &amp; 255);
                      flowLayoutPanel1.Controls.Add(pan);
  
                  }
                  //flowLayoutPanel1.MouseMove += MouseScroll;
                  //this.flowLayoutPanel1.MouseLeave += outSideCheck;
  
                  mouseScroller MSC = new mouseScroller();
                  MSC.registerControl(flowLayoutPanel1);  // FLP = your FlowLayouPanel
                  MSC.timerSpeed = 5;  // optional
                  MSC.nearness = 100;  // optional
  
                  flowLayoutPanel1.AutoScroll = false;
  
              }
  </code></pre>
  
  <p>now the apps doing wired behavior after adding new code. if i am making any mistake then guide me please. thanks</p>