-
Mar 10th, 2017, 07:06 PM
#1
Thread Starter
New Member
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
-
Mar 10th, 2017, 07:47 PM
#2
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:
Public Class Form1 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 End Class
-
Mar 10th, 2017, 08:04 PM
#3
Thread Starter
New Member
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
-
Mar 10th, 2017, 08:06 PM
#4
Thread Starter
New Member
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
-
Mar 10th, 2017, 08:30 PM
#5
Re: VB Picture box
Originally Posted by fatimaj
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.
-
Mar 10th, 2017, 08:33 PM
#6
Re: VB Picture box
Originally Posted by fatimaj
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.
-
Mar 10th, 2017, 10:19 PM
#7
Hyperactive Member
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:
Public Class Form1
Private moveRight As Boolean = True
Private WithEvents t As New System.Windows.Forms.Timer With {.Enabled = False, .Interval = 100}
Dim AmountofTimes As Integer = 0
Private Sub timerTicks() Handles t.Tick
If t.Enabled = True Then
If IsNumeric(TextBox1.Text) Then
If AmountofTimes <> 0 Then
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
'When a side is hit decreases the total amount of times to keep going
AmountofTimes = (AmountofTimes - 1)
End If
Else
t.Enabled = False
End If
End If
End If
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
If IsNumeric(TextBox1.Text) Then
AmountofTimes = CInt(TextBox1.Text)
t.Enabled = True
End If
End Sub
End Class
-
Mar 10th, 2017, 10:27 PM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|