Results 1 to 8 of 8

Thread: VB Picture box

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2017
    Posts
    3

    VB Picture box

    I wrote the following code but it's not right. I'm trying to create a program that will make my little picture box move across the form the number of times that the user inputs into the textbox. Right now it does nothing
    Can someone tell me what I've done wrong?

    Public Class Form1
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Me.Close()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    End Sub
    Sub Delay(ByVal i As Double)
    Dim TotalMS As Integer
    If i.ToString.Contains(".") Then
    Dim BeforeDecVal As String = i.ToString.Split("."c)(0)
    Dim AfterDecVal As String = i.ToString.Split("."c)(1)
    TotalMS = CInt(CDbl(AfterDecVal) * 100 + CDbl(BeforeDecVal) * 1000)
    Else
    TotalMS = CInt(i * 1000)
    End If
    Using w As New ManualResetEvent(False)
    w.WaitOne(TotalMS)
    End Using

    If IsNumeric(NumberBox.Text) Then
    If NumberBox.Text IsNot Nothing AndAlso CInt(NumberBox.Text) > 0 Then
    Delay(0.1)
    PictureBox1.Location = New Point(PictureBox1.Location.X - CInt(NumberBox.Text), PictureBox1.Location.Y)
    Delay(0.1)
    PictureBox1.Location = New Point(PictureBox1.Location.X - CInt(NumberBox.Text), PictureBox1.Location.Y)
    Delay(0.1)
    PictureBox1.Location = New Point(PictureBox1.Location.X + CInt(NumberBox.Text), PictureBox1.Location.Y)
    Delay(0.1)
    PictureBox1.Location = New Point(PictureBox1.Location.X + CInt(NumberBox.Text), PictureBox1.Location.Y)
    End If
    End If
    End Sub


    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    NumberBox.Text = String.Empty
    NumberBox.Focus()
    End Sub
    End Class

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: VB Picture box

    Use a Timer. Each time a Tick event is raised, move the control the appropriate amount in the appropriate direction. E.g.
    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private moveRight As Boolean = True
    4.  
    5.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    6.         Timer1.Enabled = Not Timer1.Enabled
    7.     End Sub
    8.  
    9.     Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    10.         Dim increment = If(moveRight, 5, -5)
    11.  
    12.         PictureBox1.Left += increment
    13.  
    14.         'Change direction if the control has moved to or past the edge.
    15.         If (moveRight AndAlso PictureBox1.Right >= ClientSize.Width) OrElse
    16.            (Not moveRight AndAlso PictureBox1.Left <= 0) Then
    17.             moveRight = Not moveRight
    18.         End If
    19.     End Sub
    20.  
    21. End Class

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2017
    Posts
    3

    Re: VB Picture box

    As in use the timer instead? Do you mind telling me my mistake in the code I wrote? I don't want to do it again

  4. #4

    Thread Starter
    New Member
    Join Date
    Mar 2017
    Posts
    3

    Re: VB Picture box

    Because now it won't move either

    Public Class Form1
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Me.Close()
    End Sub


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    End Sub
    Private moveRight As Boolean = True

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Timer1.Enabled = Not Timer1.Enabled
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Dim increment = If(moveRight, 5, -5)

    PictureBox1.Left += increment

    'Change direction if the control has moved to or past the edge.
    If (moveRight AndAlso PictureBox1.Right >= ClientSize.Width) OrElse
    (Not moveRight AndAlso PictureBox1.Left <= 0) Then
    moveRight = Not moveRight
    End If
    End Sub


    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    NumberBox.Text = String.Empty
    NumberBox.Focus()
    End Sub
    End Class

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: VB Picture box

    Quote Originally Posted by fatimaj View Post
    As in use the timer instead? Do you mind telling me my mistake in the code I wrote? I don't want to do it again
    I don't really know where to start with your original code. It doesn't look fit for purpose to me. I'm not prepared to examine it in detail to determine specifics.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: VB Picture box

    Quote Originally Posted by fatimaj View Post
    Because now it won't move either

    Public Class Form1
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Me.Close()
    End Sub


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    End Sub
    Private moveRight As Boolean = True

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Timer1.Enabled = Not Timer1.Enabled
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Dim increment = If(moveRight, 5, -5)

    PictureBox1.Left += increment

    'Change direction if the control has moved to or past the edge.
    If (moveRight AndAlso PictureBox1.Right >= ClientSize.Width) OrElse
    (Not moveRight AndAlso PictureBox1.Left <= 0) Then
    moveRight = Not moveRight
    End If
    End Sub


    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    NumberBox.Text = String.Empty
    NumberBox.Focus()
    End Sub
    End Class
    I don't see how that code could even compile because you have two methods with exactly the same signature. What I provided was an example. You should make an effort to understand what the example is doing and then implement the same principle(s) in your own code. Perhaps do as I did and create a new project with only what's required for the example, i.e. a Button, a PictureBox and a Timer. If you do that then you'll see that the PictureBox will move back and forth after clicking the Button and stop again with another click. You can then read and analyse the code and ask questions if required until you understand how it does what it does. You can then use that understanding to write your own code in your own project.

  7. #7
    Hyperactive Member Vexslasher's Avatar
    Join Date
    Feb 2010
    Posts
    429

    Re: VB Picture box

    I have tried to simplify this for you. I have taken jmcilhinney code which is well written and just added in a little to it and changed it so you don't need to add a timer to your design.

    You will need a TextBox1 and a PictureBox1 for these codes to work.
    vb.net Code:
    1. Public Class Form1
    2.     Private moveRight As Boolean = True
    3.     Private WithEvents t As New System.Windows.Forms.Timer With {.Enabled = False, .Interval = 100}
    4.     Dim AmountofTimes As Integer = 0
    5.     Private Sub timerTicks() Handles t.Tick
    6.         If t.Enabled = True Then
    7.             If IsNumeric(TextBox1.Text) Then
    8.                 If AmountofTimes <> 0 Then
    9.                     Dim increment = If(moveRight, 5, -5)
    10.                     PictureBox1.Left += increment
    11.                     'Change direction if the control has moved to or past the edge.
    12.                     If (moveRight AndAlso PictureBox1.Right >= ClientSize.Width) OrElse
    13.                        (Not moveRight AndAlso PictureBox1.Left <= 0) Then
    14.                         moveRight = Not moveRight
    15.                         'When a side is hit decreases the total amount of times to keep going
    16.                         AmountofTimes = (AmountofTimes - 1)
    17.                     End If
    18.                 Else
    19.                     t.Enabled = False
    20.                 End If
    21.             End If
    22.         End If
    23.     End Sub
    24.     Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    25.         If IsNumeric(TextBox1.Text) Then
    26.             AmountofTimes = CInt(TextBox1.Text)
    27.             t.Enabled = True
    28.         End If
    29.     End Sub
    30. End Class

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: VB Picture box

    If you want to user to specify a particular number of times to do something, it's advisable to use a NumericUpDown rather than a TextBox. That way, you can restrict them to just numbers and those in a range with no validation required.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width