0

I have a button inside a form. The button moves horizontally from one point to another by continuously varying the Button.Location.X from 0 to 1000 using a timer. See code below.

Timer1.Interval = 100
Dim i as Integer = -1
Private Sub Timer1_tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Button1.Location = New Point(i, 50)
If i = 1000
i = 0
End If
i = i + 1
End Sub

Now the problem is while the button is moving it flickers. How do I stop the flickering?
Tried using Hans Passant's answer to this question but it's not working.

Somanna
  • 284
  • 1
  • 13
  • Try to set Form's property **DoubleBuffered** to `True` – evry1falls Jun 06 '20 at 16:13
  • @evry1falls tried it. Not working. – Somanna Jun 06 '20 at 16:24
  • Does this [help](https://stackoverflow.com/a/2613272/13393566) ? – evry1falls Jun 06 '20 at 16:40
  • @evry1falls not working. – Somanna Jun 06 '20 at 17:14
  • 1
    Instead of a Button, you can draw an Image/Shape on a PictureBox (or a Flat Label), using a Timer set to 55-75 and increments of `.5f` (so use a Single type). Adjust as needed to increment the speed. Set `InterpolationMode = InterpolationMode.Low` and `SmoothingMode = SmoothingMode.HighSpeed`. The Form's DoubleBuffer is irrelevant. – Jimi Jun 06 '20 at 18:02
  • @Jimi after trying your solution the flickering has reduced a little bit but still noticeable. – Somanna Jun 06 '20 at 18:09
  • @Jimi In the button's paint event handler I set: `e.graphics.InterpolationMode = InterpolationMode.Low` and `e.Graphics.SmoothingMode = SmoothingMode.HighSpeed` – Somanna Jun 06 '20 at 18:17
  • Read again my comment. I wrote something completely different. – Jimi Jun 06 '20 at 18:18
  • @Jimi But I wanted a button. Does your solution only work for picture boxes?? – Somanna Jun 06 '20 at 18:20
  • 2
    That doesn't *work with PictureBoxes*, it work better because you're drawing on a buffered context using floating point values. Your Button doesn't *flicker*, it *stutters*, since the movement is performed inside a fixed grid. With that *speed*, a human eye can easily detect the difference in position. Drawing a graphics object and using floating point values to define its position (plus subtle anti-alias to *fade* the borders), the movement is more difficult to detect, so it appears more *fluid*. – Jimi Jun 06 '20 at 20:17
  • 2
    Don't set the `Location` if you're only varying one aspect. Set `Top` to move vertically and `Left` to move horizontally. ALWAYS read the documentation so you know what members are available. – jmcilhinney Jun 07 '20 at 02:13

0 Answers0