I created a button, but receive an Error End of statement expected error.
This is my code:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
X = TextBox1.Text
Y = TextBox2.Text
If RadioButton1.Checked = True Then Z = X + Y End IF
If RadioButton2.Checked = True Then Z = X - Y End IF
If RadioButton3.Checked = True Then Z = X * Y End IF
If RadioButton4.Checked = True Then Z = X / Y End IF
TextBox3.Text = Z
When writing single line If statements, you don't have to put End If.
That's why it's complaining that End of statement expected.
There doesn't appear to be anything wrong with your code (so long as it is properly formatted in your IDE; the formatting in the question is wrong).
Just for fun, I switched some things up for you. You might try something like this to make your code more readable:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
X = CDbl(TextBox1.Text)
Y = CDbl(TextBox2.Text)
Select Case True
Case RadioButton1.Checked
TextBox3.Text = X + Y
Case RadioButton2.Checked
TextBox3.Text = X - Y
Case RadioButton3.Checked
TextBox3.Text = X * Y
Case RadioButton4.Checked
TextBox3.Text = X / Y
End Select
Related
So I am making a basic pinball game using vb.net and am having an issue with the collisions that once the ball slows down a certain amount it glitches inside and then through the walls.
I am using a picturebox for the walls and the ball and am using the intersectwith(picturebox command to detect collision.
any help on how to fix this would be appreciated. Thanks
Bellow is most of the code: (pctball is the ball)
Private Sub Screen_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DoubleBuffered = True
dX = 6 ' Pixels per timer tick
dY = 6
Timer1.Enabled = True
'array for colliding scores
For Each obj In Me.Controls
If TypeOf obj Is PictureBox AndAlso obj.tag = "Disc" Then
DiscArray(i) = obj
i += 1
End If
Next
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If GameActive = True Then
Labelx.Text = PctBall.Left
Labely.Text = PctBall.Top
PctBall.Left = PctBall.Left + dX
PctBall.Top = PctBall.Top + dY
' check for ball hitting edges, reverse direction if so
If PctBall.Left < 10 Or PctBall.Left > MyBase.Width - PctBall.Width Then
dX = -dX
End If
If PctBall.Top < 10 Then
dY = -dY
End If
If LeftFlipper = True Then
FlipperDefaultL.Visible = False
FlipperUpL.Visible = True
Else
FlipperDefaultL.Visible = True
FlipperUpL.Visible = False
End If
If RightFlipper = True Then
FlipperDefaultR.Visible = False
FlipperUpR.Visible = True
Else
FlipperDefaultR.Visible = True
FlipperUpR.Visible = False
End If
GravityFunction()
ReboundAngle()
' If ball goes outside bottom
If PctBall.Bottom > MyBase.Height Then
GameActive = False
Game_Over()
End If
End If
End Sub
Private Sub Keys_Down(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyValue
Case Keys.Space
GameActive = True
Case Keys.D
LeftFlipper = True
Case Keys.K
RightFlipper = True
End Select
End Sub
Private Sub Keys_Up(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
Select Case e.KeyValue
'Case Keys.Space
' GameActive = False
Case Keys.D
LeftFlipper = False
Case Keys.K
RightFlipper = False
End Select
End Sub
Private Sub GravityFunction()
' gravity
gravity = CollisionCount
dY += gravity
PctBall.Top += dY
End Sub
Private Function CheckCollision()
For Each PictureBox In Me.Controls
If PictureBox IsNot PctBall AndAlso PictureBox.visible = True AndAlso PctBall.Bounds.IntersectsWith(PictureBox.Bounds) Then
collision = True
CollisionCount += 0.1
For discCount = 0 To DiscArray.Length - 1
If PictureBox IsNot PctBall AndAlso PctBall.Bounds.IntersectsWith(DiscArray(discCount).Bounds) Then
score = score + 1
ScoreBoard.scorelabel.Text = "Score: " & score
End If
Next
Exit For 'Exit when at least one collision found
Else : collision = False
End If
Next
If Collision = True Then
Coll.Text = "True"
Else
Coll.Text = "False"
End If
Return collision
End Function
Private Function ReboundAngle()
If CheckCollision() Then
dX = -dX
dY = -dY
End If
End Function
So I'm deliberately trying to make a Download ETA Calculator using Visual Basic 2013 Ultimate.. but after 3 hours of work, I tried running it but it won't show the final result (The Estimated Time). I am also asking you how to convert the result into Time format..
here is the code
Public Class Form1
Private Property z As Object
Private Sub Label1_Click(sender As Object, e As EventArgs)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs)
End Sub
Private Sub LinkLabel1_LinkClicked(sender As Object, e As
LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
Process.Start("www.speedtest.net")
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs)
Handles Button1.Click
Dim x, y, z, a As Integer
Dim sum
x = TextBox1.Text
y = TextBox2.Text
sum = Label1
If RadioButton1.Checked = True And RadioButton3.Checked = True Then
z = x / 1024 / y
a = z / 60
sum = a / 60
End If
If RadioButton1.Checked = True And RadioButton4.Checked = True Then
z = x / 1024 / y
a = z / 60 * 1024
sum = a / 60
End If
If RadioButton2.Checked = True And RadioButton3.Checked = True Then
z = x / 1048576 / y
a = z / 60
sum = a / 60
End If
If RadioButton2.Checked = True And RadioButton4.Checked = True Then
z = x / 1048576 / y
a = z / 60 * 1024
sum = a / 60
End If
End Sub
Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs)
Handles
RadioButton1.CheckedChanged
End Sub
End Class
Error 2
http://tny.cz/7b5711f7
Error 3
http://tny.cz/09bf1f96
Dim sum
sum = Label1
Presumably Label1 is a label control so you're setting a variable to be equal to the label control.
Later on, you're trying to set this variable of type label control to a decimal
sum = a / 60
I would say you want probably want to replace all your
sum = a / 60
with something like
label1.Text = New TimeSpan(a / 60, 0, 0).ToString()
and forget about variable sum all together
I am writing a Rock-Paper-Scissors game and have to use a ComboBox to get the user's selection. The problem is that no matter what I select, the selected index always reverts to 1, thus the user always selects rock. How can I fix this?
Imports System.Random
Public Class Form1
Dim played As Integer = 0
Dim won As Integer = 0
Dim lost As Integer = 0
Dim draw As Integer = 0
Dim percent As Single = 0.0
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Label3.Visible = False
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim a As New Random()
Dim b As Integer = a.Next(1, 4)
Dim selection As Integer
ComboBox1.SelectedIndex = selection
If b = 1 Then
Label3.Text = "Rock"
Label3.Visible = True
Select Case selection
Case selection = 1
MsgBox("Draw.")
draw += 1
Case selection = 2
MsgBox("Paper Covers Rock. You win!")
won += 1
Case selection = 3
MsgBox("Rock Crushes Scissors. You lose.")
lost += 1
End Select
ElseIf b = 2 Then
Label3.Text = "Paper"
Label3.Visible = True
Select Case selection
Case selection = 1
MsgBox("Paper Covers Rock. You lose.")
lost += 1
Case selection = 2
MsgBox("Draw.")
draw += 1
Case selection = 3
MsgBox("Scissors Cuts Paper. You win!")
won += 1
End Select
ElseIf b = 3 Then
Label3.Text = "Scissors"
Label3.Visible = True
Select Case selection
Case selection = 1
MsgBox("Rock Crushes Scissors. You win!")
won += 1
Case selection = 2
MsgBox("Scissors Cuts Paper. You lose.")
lost += 1
Case selection = 3
MsgBox("Draw.")
draw += 1
End Select
End If
played += 1
percent = (won / played) * 100
PlayedText.Text = played.ToString
WonText.Text = won.ToString
LostText.Text = lost.ToString
DrawText.Text = draw.ToString
PercentText.Text = percent.ToString
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub
End Class
I think you need to change this line to
Dim selection As Integer
selection = ComboBox1.SelectedIndex
and the syntax for the SELECT....CASE is
Select Case selection
Case 1
MsgBox("Draw.")
draw += 1
Case 2
MsgBox("Paper Covers Rock. You win!")
won += 1
Case 3
MsgBox("Rock Crushes Scissors. You lose.")
lost += 1
End Select
I am trying to detect the left and right mouse movements for a control - like you can use delta for up/down movement. Can anyone help with this? Thanks.
If e.x > 0 Then 'moved right
msgbox("Moved right!")
else 'moved left
msgbox("Moved left!")
End If
Private oldXY As Point = Point.Empty
Private Sub Form1_MouseMove(sender As Object,
e As MouseEventArgs) Handles Me.MouseMove
If e.X < oldXY.X Then
' ....
ElseIf e.X > oldXY.X Then
' ...
End If
oldXY.X = e.X
oldXY.Y = e.Y
End Sub
You will likely want to add a test for Point.Empty so that you dont misreport the first mousemove. Or try to initialize it to Cursor.Position to start with
I'm using Timer , and I get a good result
Dim lx As Integer = 0 ' last x position
Dim ly As Integer = 0 ' last y position
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim x As Integer = MousePosition.X
Dim y As Integer = MousePosition.Y
Dim s As String = ""
If x > lx Then
s &= "Right,"
ElseIf x < lx Then
s &= "Left,"
ElseIf x = lx Then
s &= "No Change,"
End If
If y > ly Then
s &= "Down"
ElseIf y < ly Then
s &= "Top"
ElseIf y = ly Then
s &= "No Change"
End If
lx = x
ly = y
Label1.Text = s
End Sub
Private firstTime As Boolean = False
Private oldX As Integer
Private Sub Button1_MouseEnter(sender As System.Object, e As System.EventArgs) Handles Button1.MouseEnter
firstTime = True
End Sub
Private Sub Button1_MouseMove(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseMove
If firstTime = True Then
firstTime = False
Else
If e.X > oldX Then
'moves right
ElseIf e.X < oldX Then
'moves left
End If
End If
oldX = e.X
End Sub
I basically want a survey document where the user selects a rating such as "always" and "never." They select a rating by hitting a radio button. The ratings are weighted and are averaged into a total score, like a GPA, i.e. always is worth 3, somewhat is worth 2 and each question should be added and then averaged. Below is a section of my code for one question.
How do I take the value at the end after the button is selected and average them for all the questions?
Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
If RadioButton1.Checked = True Then
RadioButton3.Checked = False
RadioButton2.Checked = False
RadioButton4.Checked = False
RadioButton5.Checked = False
num1 = 2
End If
End Sub
Private Sub RadioButton2_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton2.CheckedChanged
If RadioButton2.Checked = True Then
RadioButton3.Checked = False
RadioButton1.Checked = False
RadioButton4.Checked = False
RadioButton5.Checked = False
num1 = 3
End If
End Sub
Private Sub RadioButton3_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton3.CheckedChanged
If RadioButton3.Checked = True Then
RadioButton1.Checked = False
RadioButton2.Checked = False
RadioButton4.Checked = False
RadioButton5.Checked = False
num1 = 1
End If
End Sub
Private Sub RadioButton4_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton4.CheckedChanged
If RadioButton4.Checked = True Then
RadioButton3.Checked = False
RadioButton2.Checked = False
RadioButton1.Checked = False
RadioButton5.Checked = False
num1 = 0
End If
End Sub
Private Sub RadioButton5_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton5.CheckedChanged
If RadioButton5.Checked = True Then
RadioButton3.Checked = False
RadioButton2.Checked = False
RadioButton4.Checked = False
RadioButton1.Checked = False
num1 = 0
End If
End Sub
First of all, you can consolidate all of those RadioButton event handlers into ONE handler. Furthermore, you don't have to "uncheck" the other RadioButtons in the group as this will be done automatically for you. Finally, create a separate method that computes the rating and updates the form, then call that method at the bottom of the handler.
That might look something like this:
Private num1 As Integer
Private Sub set1_CheckedChanged(sender As Object, e As EventArgs) Handles _
RadioButton1.CheckedChanged, RadioButton2.CheckedChanged, RadioButton3.CheckedChanged, _
RadioButton4.CheckedChanged, RadioButton5.CheckedChanged
If RadioButton1.Checked Then
num1 = 2
ElseIf RadioButton2.Checked Then
num1 = 3
ElseIf RadioButton3.Checked Then
num1 = 1
ElseIf RadioButton4.Checked Then
num1 = 0
ElseIf RadioButton5.Checked Then
num1 = 0
End If
UpdateRating()
End Sub
Private Sub UpdateRating()
' ... compute the rating using "num1", "num2", "num3", etc ...
' ... then update the GUI with the new value ...
End Sub
Note that all five RadioButtons are listed after the "Handles" keyword making them all fire the same handler. You can do a similar thing for each group of RadioButtons...just make sure each set of RadioButtons is in its OWN container like a GroupBox or Panel. A RadioButton is mutually exclusive with all other RadioButttons in its own container.
I'm not so sure about your case .. but you can try this
Declare your var that will count all result ..
Dim num1 as Integer
Dim num2 as Integer '--> for other group radiobuttons
Dim num3 as Integer '--> for other group radiobuttons
Dim nTotal as Integer
Dim nAvg as Integer
Do this in each checkchanged
Private Sub RadioButton_CheckedChanged(..) Handles RadioButton1_CheckedChanged, RadioButton2_CheckedChanged, .....
'codes
countit()
End Sub
Sub CountIt()
nTotal = num1 + num2 + num3
nAvg = (num1 + num2 + num3) / 3
End Sub