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
Related
This is my first time writing a small winform game. When researching into collision detection i know that i need to set my bounds first.
Me.Controls.Add(PlayerShip) 'imports the picture box onto the PlayScreen
PlayerShip.Width = 40
PlayerShip.Height = 40 'Dimensions of the player ship
PlayerShip.BorderStyle = BorderStyle.FixedSingle 'adds a border to the picturebox
PlayerShip.BackColor = Color.White 'adds colour to the ship background
PlayerShip.SetBounds(x:=40, y:=40, height:=40, width:=40)
And then the other picture box
Me.Controls.Add(WallNorth)
WallNorth.Width = 750
WallNorth.Height = 5
WallNorth.BorderStyle = BorderStyle.FixedSingle
WallNorth.BackColor = Color.Green
WallNorth.Top = 1
WallNorth.Left = 1
WallNorth.SetBounds(x:=1, y:=1, height:=5, width:=750)
Private Sub PlayScreen_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyValue
Case Keys.Right
MoveRight = True
Case Keys.Left
MoveLeft = True 'This edits the boolean value of the varibales when the correct key is pressed
Case Keys.Up
MoveUp = True
Case Keys.Down
MoveDown = True
End Select
End Sub
Private Sub PlayScreen_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
Select Case e.KeyValue
Case Keys.Right
MoveRight = False
Case Keys.Left
MoveLeft = False 'This edits the boolean value when the key has been lifted
Case Keys.Up
MoveUp = False
Case Keys.Down
MoveDown = False
End Select
End Sub
Private Sub MovementTimer_Tick(sender As Object, e As EventArgs) Handles MovementTimer.Tick
If MoveRight = True Then
PlayerShip.Left += 5
End If
If MoveLeft = True Then
PlayerShip.Left -= 5
End If
If MoveUp = True Then
PlayerShip.Top -= 5 ' I use a timer to tick every 10 milliseconds ato check the states of each key, this statement controlls the execution of the direction
End If
If MoveDown = True Then
PlayerShip.Top += 5
End If
End Sub
End Class
However when i try to write an if statement to check if they will collide or not i do not get a '.bounds' option. I have seen '.bounds' being needed after the name of the picture box. Instead i have been met with '.setbounds' even though i have set them before.
Is something wrong with my version of visual studio, or am I missing something?
I need to stagger each body parts release so that they dont just overlap like they do currently.
Is there a way to stagger the running of this code?
Public Class Form1
Public xx As New List(Of Integer)
Public yy As New List(Of Integer)
Public up As Boolean = True
Public down As Boolean = False
Public lefty As Boolean = False
Public righty As Boolean = False
Public sizey As Integer = -1
Public tik As Integer = 0
Public neww As Boolean = False
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
newpart()
newpart()
newpart()
End Sub
Public Sub square(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)
e.Graphics.Clear(Color.Black)
For a = 0 To sizey
e.Graphics.FillRectangle(Brushes.Aqua, xx(a), yy(a), 20, 20)
Next
End Sub
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Right Then
righty = True
lefty = False
up = False
down = False
ElseIf e.KeyCode = Keys.Left Then
righty = False
lefty = True
up = False
down = False
ElseIf e.KeyCode = Keys.Up Then
righty = False
lefty = False
up = True
down = False
ElseIf e.KeyCode = Keys.Down Then
righty = False
lefty = False
up = False
down = True
End If
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e _
As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
square(sender, e)
End Sub
Private Sub clock_Tick(sender As Object, e As EventArgs) Handles head.Tick
If up = True Then
yy(0) = yy(0) - 20
ElseIf down = True Then
yy(0) = yy(0) + 20
ElseIf lefty = True Then
xx(0) = xx(0) - 20
ElseIf righty = True Then
xx(0) = xx(0) + 20
End If
Me.Refresh()
For b = 0 To sizey - 1
If yy(b) - yy(b + 1) = 0 Then
xx(b + 1) = xx(b + 1) + (xx(b) - xx(b + 1))
ElseIf xx(b) - xx(b + 1) = 0 Then
yy(b + 1) = yy(b + 1) + (yy(b) - yy(b + 1))
If neww = True Then
neww = False
Exit For
End If
End If
Next
End Sub
Sub newpart()
xx.Add(100)
yy.Add(100)
sizey = sizey + 1
neww = True
Return
End Sub
End Class
Its mainly the bit in the clock tick as in I need it to wait another tick before running the for loop again. This edited version works but really poorly as the third body part jumps to the first occasionally then just stays still.
You over-complicated that for loop there.
Just make tail catch up before you move head. It can be done in single tick.
Public Class Form1
Public p As New List(Of Point)
Public direction As eDircetion = eDircetion.Up
Public Enum eDircetion
Up
Down
Left
Right
End Enum
Public sizey As Integer = -1
Public tik As Integer = 0
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
newpart()
newpart()
newpart()
head.Interval = 500
head.Start()
End Sub
Public Sub square(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)
e.Graphics.Clear(Color.Black)
For a = 0 To sizey
e.Graphics.FillRectangle(Brushes.Aqua, p(a).X, p(a).Y, 20, 20)
Next
End Sub
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.D Then
direction = eDircetion.Right
ElseIf e.KeyCode = Keys.A Then
direction = eDircetion.Left
ElseIf e.KeyCode = Keys.W Then
direction = eDircetion.Up
ElseIf e.KeyCode = Keys.S Then
direction = eDircetion.Down
End If
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e _
As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
square(sender, e)
End Sub
Private Sub clock_Tick(sender As Object, e As EventArgs) Handles head.Tick
'THIS!
For i = p.Count - 1 To 1 Step -1
p(i) = p(i - 1)
Next
Select Case direction
Case eDircetion.Up
p(0) -= New Size(0, 20)
Case eDircetion.Down
p(0) += New Size(0, 20)
Case eDircetion.Left
p(0) -= New Size(20, 0)
Case eDircetion.Right
p(0) += New Size(20, 0)
Case Else : Throw New Exception("Something went wrong")
End Select
'Me.Refresh()
me.Invalidate() 'This is faster.
Me.Update()
'For b = 0 To sizey - 1
' If p(b).Y - p(b + 1).Y = 0 Then
' p(b + 1).X = p(b + 1).X + (p(b).X - p(b + 1).X)
' ElseIf xx(b) - xx(b + 1) = 0 Then
' yy(b + 1) = yy(b + 1) + (yy(b) - yy(b + 1))
' If neww = True Then
' neww = False
' Exit For
' End If
' End If
'Next
End Sub
Sub newpart()
p.Add(New Point(100, 100))
sizey = sizey + 1
Return
End Sub
End Class
I made some minor changes:
-Use enum instead of 4 booleans, makes cleaner code
-Do not use Me.Refresh, it's slow and does things you do not need here
-It's better to use not two integer lists, but one point list
Hope it helps.
I'm developing a timer for my kids that will automatically shut down the computer once the time is up and I was trying to figure out a way that the program would automatically restart if it were to be closed via task manager.
I've posted my code for my program bellow if it's any help.
Imports System
Imports System.IO
Imports System.Text
Imports System.Collections.Generic
Public Class Digparent
'add to startupp:
' My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True).SetValue(Application.ProductName, Application.ExecutablePath)
'remove from startup
'My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True).DeleteValue(Application.ProductName)
'use application setting boolean to not add same application to startup more than once
'charge for this feature
'to do
'
'wrongn height when make timer unstopable
'above all
Dim X, Y As Integer
Dim NewPoint As New System.Drawing.Point
Public second As Integer
Public checkdone As Boolean
Public checkoff As Boolean
Public unstop As Boolean
Dim Mondayt As String
Dim Tuesdayt As String
Dim Wendsdayt As String
Dim Thursdayt As String
Dim Fridayt As String
Dim Saturdayt As String
Dim Sundayt As String
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' //
' //
' //
' //Start reader
' //
Dim timeinfo As String = "C:\Users\DigiParent\Desktop\Project data\good.txt"
' IO.File.SetAttributes("C:\Users\DigiParent\Desktop\Project data\Digitimeinfo.txt", IO.FileAttributes.Hidden)
Dim timeChecker As New System.IO.StreamWriter(timeinfo, True)
timeChecker.Close()
Dim readertime As New System.IO.StreamReader(timeinfo, Encoding.Default)
Dim texttime As String = readertime.ReadToEnd
readertime.Close()
If texttime = "" Then
Dim timeobjWriter As New System.IO.StreamWriter(timeinfo, True)
timeobjWriter.Write(",,,,")
timeobjWriter.Close()
End If
Dim startup As String = "C:\Users\DigiParent\Desktop\Project data\good.txt"
Dim reader As New System.IO.StreamReader(startup, Encoding.Default)
Dim data As String = reader.ReadToEnd
Dim aryTextFile(6) As String
aryTextFile = data.Split(",")
Mondayt = aryTextFile(0)
Tuesdayt = aryTextFile(1)
Wendsdayt = aryTextFile(2)
Thursdayt = aryTextFile(3)
Fridayt = aryTextFile(4)
'
'enable this for saturday and sunday
'
'Saturdayt = aryTextFile(5)
'Sundayt = aryTextFile(6)
reader.Close()
' //
' //
' //Finish reader
' //
End Sub
Private Sub Panel2_MouseMove(sender As Object, e As MouseEventArgs) Handles Panel2.MouseMove, time.MouseMove, timeup.MouseMove
If unstop = True Then
If e.Button = Windows.Forms.MouseButtons.Left Then
NewPoint = Control.MousePosition
NewPoint.X -= (X)
NewPoint.Y -= (Y)
Me.Location = NewPoint
End If
End If
End Sub
Private Sub Panel2_MouseDown(sender As Object, e As MouseEventArgs) Handles Panel2.MouseDown, time.MouseDown, timeup.MouseDown
If unstop = True Then
X = Control.MousePosition.X - Me.Location.X
Y = Control.MousePosition.Y - Me.Location.Y
End If
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
NumericUpDownhrs.Left -= 40
NumericUpDownmin.Left -= 40
NumericUpDownsec.Left -= 29 '25
Hourstxt.Left -= 40
Minutestxt.Left -= 30
secondstxt.Left -= 30
Panel1.Left -= 30
RadioButton2.Left -= 30
RadioButton1.Left -= 30
Label4.Left -= 30
Label5.Left -= 30
Button4.Left -= 30
time.Left -= 30
timeup.Left -= 30
If RadioButton1.Location = RadioButton5.Location Then
Timer1.Stop()
Else
End If
If Me.Height < 265 Then
Me.Height = Me.Height + 1
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
more.Visible = False
updateb.Visible = False
feedbackb.Visible = False
Timer1.Start()
Button1.Visible = False
End Sub
Private Sub RadioButton5_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton5.CheckedChanged
End Sub
Private Sub Button4_Click(snder As Object, e As EventArgs) Handles Button4.Click
My.Settings.Data = True
If RadioButton6.Checked = True Then
My.Settings.unstopable = True
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
Me.ShowInTaskbar = False
Me.ControlBox = False
Me.Text = ""
Me.ShowIcon = False
Me.ShowInTaskbar = False
Me.ControlBox = False
unstop = True
Me.Height = 149
Else
My.Settings.unstopable = False
End If
If RadioButton1.Checked = True Then
My.Settings.Shutdown = True
checkoff = True
' System.Diagnostics.Process.Start("ShutDown", "/s")
Else
My.Settings.Shutdown = False
End If
vhrs = NumericUpDownhrs.Value
vmin = NumericUpDownmin.Value
vsec = NumericUpDownsec.Value
My.Settings.hours = vhrs
My.Settings.min = vmin
My.Settings.second = vsec
PictureBox1.Dock = DockStyle.None
PictureBox1.Visible = False
starttime.Start()
realTimer.Start()
End Sub
Public Hrs As Integer 'number of hours '
Public Min As Integer 'number of Minutes '
Public Sec As Integer 'number of Sec '
Public Function GetTime(Time As Integer) As String
'Seconds'
Sec = Time Mod 60
'Minutes'
Min = ((Time - Sec) / 60) Mod 60
'Hours'
Hrs = ((Time - (Sec + (Min * 60))) / 3600) Mod 60
Return Format(Hrs, "00") & ":" & Format(Min, "00") & ":" & Format(Sec, "00")
End Function
Private Sub realTimer_Tick(sender As Object, e As EventArgs) Handles realTimer.Tick
second = second + 1
time.Text = GetTime(second)
'now
If Min >= vmin And Hrs >= vhrs And Sec >= vsec Then
checkdone = True
Me.TopMost = True
'Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedSingle
endtime.Start()
If unstop = True Then
closeb.Visible = True
End If
realTimer.Stop()
End If
If checkdone = True And checkoff = True Then
endtime.Start()
System.Diagnostics.Process.Start("ShutDown", "/s")
End If
End Sub
Private Sub starttime_Tick(sender As Object, e As EventArgs) Handles starttime.Tick
time.Left -= 30
Panel1.Left -= 30
RadioButton2.Left -= 30
RadioButton1.Left -= 30
Label4.Left -= 30
Label5.Left -= 30
Button4.Left -= 30
timeup.Left -= 30
If time.Location = Label2.Location Then
starttime.Stop()
End If
If Me.Height > 189 Then
Me.Height = Me.Height - 5
End If
End Sub
Private Sub endtime_Tick(sender As Object, e As EventArgs) Handles endtime.Tick
time.Left -= 30
timeup.Left -= 30
If timeup.Location = labeltimeup.Location Then
endtime.Stop()
End If
End Sub
Private Sub more_Click(sender As Object, e As EventArgs) Handles more.Click
Form3.Show()
'more.Visible = False
'moretimer.Start()
End Sub
Private Sub moretimer_Tick(sender As Object, e As EventArgs) Handles moretimer.Tick
If updateb.Location = Updatebutton.Location Then
moretimer.Stop()
End If
feedbackb.Left += 15
updateb.Left -= 15
End Sub
Private Sub updateb_Click(sender As Object, e As EventArgs) Handles updateb.Click
System.Diagnostics.Process.Start("http://digiparent.weebly.com/beta-20-update.html")
End Sub
Private Sub feedbackb_Click(sender As Object, e As EventArgs) Handles feedbackb.Click
System.Diagnostics.Process.Start("http://digiparent.weebly.com/feedback.html")
End Sub
Private Sub closeb_Click(sender As Object, e As EventArgs) Handles closeb.Click
Me.Close()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
NumericUpDownsec.Value = My.Settings.second
NumericUpDownmin.Value = My.Settings.min
NumericUpDownhrs.Value = My.Settings.hours
If My.Settings.Shutdown = True Then
RadioButton1.Checked = True
End If
If My.Settings.unstopable = True Then
RadioButton6.Checked = True
End If
Button2.Visible = False
End Sub
Private Sub Numericchanged(sender As Object, e As EventArgs) Handles NumericUpDownsec.ValueChanged, NumericUpDownmin.ValueChanged, NumericUpDownhrs.ValueChanged
If NumericUpDownsec.Value = 0 Then ' NumericUpDownhrs.Value = 0 NumericUpDownmin.Value = 0 Then
If NumericUpDownhrs.Value = 0 Then
If NumericUpDownmin.Value = 0 Then
Button2.Visible = True
Else
Button2.Visible = False
End If
Else
Button2.Visible = False
End If
Else
Button2.Visible = False
End If
End Sub
Private Sub Label4_Click(sender As Object, e As EventArgs) Handles Label4.Click
End Sub
End Class
Here's a very basic windowless watchdog app.
Start with a standard WinForms project.
Add a Module.
Add a Public Sub Main to it.
Go to Project --> Properties --> Application Tab, and Uncheck the "Enable Application Framework" box.
Above that, change the "Startup object:" dropdown from "Form1" to "Sub Main".
The code...
Module Module1
Public Sub Main()
Application.Run(New Watchdog)
End Sub
End Module
Public Class Watchdog
Inherits ApplicationContext
Private AppToWatch As String
Private FullPath As String = "C:\WINDOWS\system32\calc.exe"
Private WithEvents P As Process
Public Sub New()
AppToWatch = System.IO.Path.GetFileNameWithoutExtension(FullPath)
Dim PS() As Process = Process.GetProcessesByName(AppToWatch)
If PS.Length = 0 Then
StartIt()
Else
P = PS(0)
P.EnableRaisingEvents = True
End If
End Sub
Private Sub P_Exited(sender As Object, e As EventArgs) Handles P.Exited
StartIt()
End Sub
Private Sub StartIt()
P = Process.Start(FullPath)
P.EnableRaisingEvents = True
End Sub
End Class
Compile the program as a service, and configure it to start automatically.
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