vb.net 使用定时器移动图片框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22666555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:12:50  来源:igfitidea点击:

Moving PictureBox using Timer

vb.netwinformstimerpicturebox

提问by LeMarc

Basically what I'm trying to do is to make a picture box go up, then left, then down, then right, all based on timer ticks. I'm fairly new so I don't really know what's wrong. If you guys could give a simple answer or a better approach, that'd be great.

基本上我想要做的是让图片框向上,然后向左,然后向下,然后向右,所有这些都基于计时器滴答声。我是新手,所以我真的不知道出了什么问题。如果你们能给出一个简单的答案或更好的方法,那就太好了。

Dim slides As Integer

slides += 10
If slides < 20 Then
  PictureBox1.Left += 10
ElseIf slides > 20 AndAlso slides < 40 Then
  PictureBox1.Top += 10
ElseIf slides > 40 AndAlso < 60 Then
  PictureBox1.Left -= 10
ElseIf slides > 60 AndAlso < 80 Then
  PictureBox1.Top -= 10
Else
  slides = 0
End If

采纳答案by LarsTech

Two things. Make sure your slidesinteger is outside the Tick event. Also, make sure to cover the condition of "equals", which your code doesn't check for, so slidesis constantly falling into the "else" category and setting back to zero. That is, when slidesequals 20, you don't have a condition that satisfies it, so it resets to zero.

两件事情。确保您的slides整数在 Tick 事件之外。此外,请确保涵盖“等于”的条件,您的代码不会检查该条件,因此slides不断落入“其他”类别并设置回零。也就是说,当slides等于 20 时,您没有满足它的条件,因此它重置为零。

Private slides As Integer

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
  slides += 10
  If slides <= 20 Then
    PictureBox1.Left += 10
  ElseIf slides > 20 AndAlso slides <= 40 Then
    PictureBox1.Top += 10
  ElseIf slides > 40 AndAlso slides <= 60 Then
    PictureBox1.Left -= 10
  ElseIf slides > 60 AndAlso slides <= 80 Then
    PictureBox1.Top -= 10
  Else
    slides = 0
  End If
End Sub