How to automatically restart a program after it's closed - vb.net

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.

Related

How can I make a timer slower and slower until it stops?

Can someone tell me what's wrong or what I need to add to make it run the way that I want? I need to make it slower, then slower again, then make it stop.
Heres what I have tried:
Dim A As Integer
If Timer.Interval = 1 Then
A = Timer.Interval + 1000
If Timer.Interval = 1000 Then
Timer.Enabled = False
End If
End If
I want to stop the timer after a specific time but I have button 1 and 2 its random and stop, if I push the button random it will select any on given list then if I push stop the selection of random is going stop delay like spinning wheel, the missing on my program is the delay selection after a seconds its like selecting random then it will slowly selecting then it will stop.
Here's the btnRandom.Click event handler:
Private Sub btnRandom_Click(sender As Object, e As EventArgs) Handles btnRandom.Click
If lstList1.Items.Count <= 1 Then
MessageBox.Show("Name must more than 1 to run the randomizer.", "Error Loading", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Else
txtName.Enabled = False
btnRandom.Enabled = False
btnAdd.Enabled = False
btnRemove.Enabled = False
btnStop.Enabled = True
btnAdd.ForeColor = Color.FromArgb(64, 64, 64)
btnRemove.ForeColor = Color.FromArgb(64, 64, 64)
btnRandom.ForeColor = Color.FromArgb(64, 64, 64)
If btnRandom.Enabled = False Then
Timer2.Start()
lstList1.ClearSelected()
End If
End If
End Sub
And the Timer.Tick handler:
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
If btnRandom.Enabled = True Then
Timer2.Enabled = False
End If
If btnRandom.Enabled = False Then
txtNameLabel.Text = lstList1.Items(Rnd() * (lstList1.Items.Count - 1))
lstList1.SelectedItem = txtNameLabel.Text
txtNameLabel.Text = lstList1.SelectedItem
End If
End Sub
If you want something to decelerate then you need to multiply the speed by a constant less than one, which in this case is equivalent to increasing the display time interval by a constant greater than one.
So, you can start something at one speed, and then add a deceleration by changing the amount the interval is changed by.
Something like this:
Public Class Form1
Dim sw As Stopwatch
Dim tim As Timer
Dim intervalMultiplier As Double = 1.0
Dim itemsToDisplay As List(Of String)
Dim itemIndex As Integer = 0
Private Sub tim_Tick(sender As Object, e As EventArgs)
' When intervalMultiplier > 1.0 the time between ticks will increase.
tim.Interval = CInt(tim.Interval * intervalMultiplier)
Label1.Text = itemsToDisplay(itemIndex)
If tim.Interval >= 1000 Then
tim.Stop()
End If
itemIndex = (itemIndex + 1) Mod itemsToDisplay.Count
End Sub
Private Sub StartTimer()
If tim Is Nothing Then
tim = New Timer()
AddHandler tim.Tick, AddressOf tim_Tick
End If
If sw Is Nothing Then
sw = Stopwatch.StartNew()
Else
sw.Restart()
End If
intervalMultiplier = 1.0
tim.Interval = 100
tim.Start()
End Sub
Private Sub bnStop_Click(sender As Object, e As EventArgs) Handles bnStop.Click
intervalMultiplier = 1.25
End Sub
Private Sub btnRandom_Click(sender As Object, e As EventArgs) Handles btnRandom.Click
' Your code to disable buttons etc. goes here.
StartTimer()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Set up some data for this example.
itemsToDisplay = New List(Of String) From {"Capacitor", "Diode", "Inductor", "Resistor", "Transistor"}
End Sub
End Class

Need to stagger body parts of snake with timer

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.

How can I use collision detection with spawned arrays

Since I have been trying to make a space invaders style game I have been having trouble with collision detection with spawned objects in arrays (and having a bit of trouble with the bullets, they keep stopping and having another generate). I am new at coding and would like some help with these issues, or at least some links to some forums that had the same thread question.
here is my code:
Public Class Form1
'global variables
Dim intAmountOfEnemys As Short = 9
Dim intRowsOfEnemys As Integer = 0 '**
Dim intAmountOfBullets As Integer = 0
Dim picEnemysWave1(intAmountOfEnemys) As PictureBox
Dim lblBullets As New Label
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Welcome_Screen.Hide()
Call EnemyWaves(picEnemysWave1)
End Sub
Sub PlayerMovement(ByVal sender As Object, ByVal e As KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.A Then
If picShip.Right <= 0 Then
picShip.Left = 1567
Else
picShip.Left -= 10
End If
ElseIf e.KeyCode = Keys.D Then
If picShip.Left >= 1567 Then
picShip.Left = -15
Else
picShip.Left += 10
End If
ElseIf e.KeyCode = Keys.Space Then
Do
BulletGeneration(lblBullets)
Loop Until Keys.Space
lblBullets.Left = (picShip.Left + 7)
End If
End Sub
#Region "Enemy waves, Movement, and Properties"
Sub EnemyWaves(ByRef picEnemysWave1() As PictureBox)
'Enemy Generator
Const srtENEMYSPACING_Y As Short = 155
For intCounterForEnemys As Integer = 0 To intAmountOfEnemys
Dim intEnemySpacing As Integer = srtENEMYSPACING_Y * intCounterForEnemys
picEnemysWave1(intCounterForEnemys) = New PictureBox
picEnemysWave1(intCounterForEnemys).Location = New Point(42 + intEnemySpacing, 1)
picEnemysWave1(intCounterForEnemys).Image = My.Resources.enemy
picEnemysWave1(intCounterForEnemys).Width = 124
picEnemysWave1(intCounterForEnemys).Height = 84
picEnemysWave1(intCounterForEnemys).Show()
Me.Controls.Add(picEnemysWave1(intCounterForEnemys))
Next intCounterForEnemys
End Sub``
Private Sub TmrAlien1_Tick(sender As Object, e As EventArgs) Handles TmrAlien1.Tick
For intRandom As Integer = 0 To 9
picEnemysWave1(intRandom).Top += 3
Dim intRandomNum As Integer = Rnd()
If intRandomNum > 0.66 Then
picEnemysWave1(intRandom).Left += 2 'goes left randomly
ElseIf intRandomNum < 0.33 Then
picEnemysWave1(intRandom).Left -= 2 'goes right randomly
End If
If picEnemysWave1(intRandom).Top <= 0 Then
TmrAlien1.Start()
End If
If picEnemysWave1(intRandom).Top >= 952 Then
TmrAlien1.Stop()
End If
Next intRandom
End Sub
#End Region
#Region "Bullet Generation, Movement, and Properties"
Sub BulletGeneration(ByRef lblBullets As Object)
'Generation of Bullets
For intBulletCounter As Integer = 0 To intAmountOfBullets
lblBullets = New Label
lblBullets.location = New Point(760, 785)
lblBullets.image = My.Resources.blast2
lblBullets.width = 32
lblBullets.height = 64
lblBullets.show()
Me.Controls.Add(lblBullets)
Next intBulletCounter
End Sub
Private Sub tmrBullets_Tick(sender As Object, e As EventArgs) Handles tmrBullets.Tick
lblBullets.Top -= 20
End Sub
#End Region
#Region "Collision Detection"
Sub BulletCollision(ByRef lblBullets As Label, ByRef intAmontOfEnemys As Integer)
For Each picEnemy As PictureBox In picEnemysWave1
If lblBullets.Bounds.IntersectsWith(picEnemy.Bounds) Then
picEnemy.Location = New Point(3900, 8700)
Exit For
End If
Next
'what Im trying
End Sub
#End Region

Need assistance with writing to a CSV file and saving entries

I am creating a student test score application which shows student averages, class averages (I haven't done this part yet) and can search for a particular student which shows up in the text boxes.
Can someone please tell me how to write the test scores and name of student entered into the text boxes to an Excel CSV file?
Also the save button keeps clearing all the enteries to the CSV file when I click on it. If anyone could tell me whats going on there, that would be great.
Imports System.IO
Public Class Form1
Structure Students
Dim StudentName As String
Dim Test1 As Double
Dim Test2 As Double
Dim Test3 As Double
Dim Test4 As Double
Dim Test5 As Double
End Structure
Dim StudentCounter As Integer = 0
Dim MyStudentArray(StudentCounter) As Students
Private Sub LoadDataFromFileToArray()
Dim TextFile As New System.IO.StreamReader(“..\..\StudentDataFile.csv”)
Dim strStudentData As String
strStudentData = TextFile.ReadLine()
Do Until strStudentData Is Nothing
lstAllStudentData.Items.Add(strStudentData)
WriteToArray(strStudentData)
strStudentData = TextFile.ReadLine()
Loop
TextFile.Close()
End Sub
Private Sub WriteToArray(ByVal CurrentLine)
Dim Values() As String = Split(CurrentLine, ",")
ReDim Preserve MyStudentArray(StudentCounter)
MyStudentArray(StudentCounter).StudentName = Values(0)
MyStudentArray(StudentCounter).Test1 = (Values(1))
MyStudentArray(StudentCounter).Test2 = (Values(2))
MyStudentArray(StudentCounter).Test3 = (Values(3))
MyStudentArray(StudentCounter).Test4 = (Values(4))
MyStudentArray(StudentCounter).Test5 = (Values(5))
StudentCounter += 1
End Sub
Private Sub btnAddStudent_Click(sender As Object, e As EventArgs) Handles btnAddStudent.Click
ReDim Preserve MyStudentArray(StudentCounter)
MyStudentArray(StudentCounter).Test1 = CDbl(txtTest1.Text)
MyStudentArray(StudentCounter).Test2 = CDbl(txtTest1.Text)
MyStudentArray(StudentCounter).Test3 = CDbl(txtTest1.Text)
MyStudentArray(StudentCounter).Test4 = CDbl(txtTest1.Text)
MyStudentArray(StudentCounter).Test5 = CDbl(txtTest1.Text)
MyStudentArray(StudentCounter).StudentName = (txtStudentName.Text)
StudentCounter += 1
End Sub
Private Sub CalculateIndividual()
Dim AverageScore As Double = 0
Dim Test1 As Double = CDbl(txtTest1.Text)
Dim Test2 As Double = CDbl(txtTest2.Text)
Dim Test3 As Double = CDbl(txtTest3.Text)
Dim Test4 As Double = CDbl(txtTest4.Text)
Dim Test5 As Double = CDbl(txtTest5.Text)
Dim AverageGrade As String
AverageScore = (Test1 + Test2 + Test3 + Test4 + Test5) / 5
lblScoreOutput.Text = AverageScore
lblScoreOutput.Visible = True
Select Case AverageScore
Case > 90
AverageGrade = "A+"
Case 80 To 90
AverageGrade = "A"
Case 70 To 80
AverageGrade = "B"
Case 60 To 70
AverageGrade = "C"
Case 50 To 60
AverageGrade = "D"
Case 35 To 50
AverageGrade = "E"
Case Else
AverageGrade = "UG"
End Select
lblLetterOutput.Text = AverageGrade
lblLetterOutput.Visible = True
End Sub
Private Sub Form_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Dim response As MsgBoxResult
response = MsgBox("Do you want to close Student Calculator", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Confirm")
If response = MsgBoxResult.Yes Then
Me.Dispose()
ElseIf response = MsgBoxResult.No Then
e.Cancel = True
Exit Sub
End If
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Dim response As MsgBoxResult
response = MsgBox("Do you want to close Student Calculator?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Confirm")
If response = MsgBoxResult.Yes Then
Me.Dispose()
ElseIf response = MsgBoxResult.No Then
Exit Sub
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
lstAllStudentData.Items.Clear()
LoadDataFromFileToArray()
lstAllStudentData.Items.Clear()
LoadDataFromFileToArray()
Dim MySource As New AutoCompleteStringCollection()
For StudentCounter = 0 To StudentCounter - 1
MySource.Add(MyStudentArray(StudentCounter).StudentName)
Next
With txtSearch
.AutoCompleteCustomSource = MySource
.AutoCompleteMode = AutoCompleteMode.SuggestAppend
.AutoCompleteSource = AutoCompleteSource.CustomSource
.Visible = True
Me.Controls.Add(txtSearch)
End With
End Sub
Private Sub lstAllStudentData_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstAllStudentData.SelectedIndexChanged
txtStudentName.Text = MyStudentArray(lstAllStudentData.SelectedIndex).StudentName
txtTest1.Text = MyStudentArray(lstAllStudentData.SelectedIndex).Test1
txtTest2.Text = MyStudentArray(lstAllStudentData.SelectedIndex).Test2
txtTest3.Text = MyStudentArray(lstAllStudentData.SelectedIndex).Test3
txtTest4.Text = MyStudentArray(lstAllStudentData.SelectedIndex).Test4
txtTest5.Text = MyStudentArray(lstAllStudentData.SelectedIndex).Test5
CalculateIndividual()
End Sub
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
Dim SearchField As String = txtSearch.Text
For StudentCounter = 0 To StudentCounter
If rdbStudent.Checked = True Then
If UCase(SearchField) = UCase(MyStudentArray(StudentCounter).StudentName) Then
lstAllStudentData.SelectedIndex = StudentCounter
Exit For
End If
End If
If rdbStudent.Checked = True Then
If UCase(SearchField) = UCase(MyStudentArray(StudentCounter).StudentName) Then
lstAllStudentData.SelectedIndex = StudentCounter
Exit For
End If
End If
Next
End Sub
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim StudentFile As New System.IO.StreamWriter("..\..\StudentDataFile.csv")
For StudentCounter = 0 To MyStudentArray.Length - 1
StudentFile.Write(MyStudentArray(StudentCounter).StudentName)
StudentFile.Write(",")
StudentFile.Write(MyStudentArray(StudentCounter).Test1)
StudentFile.Write(",")
StudentFile.Write(MyStudentArray(StudentCounter).Test3)
StudentFile.Write(",")
StudentFile.Write(MyStudentArray(StudentCounter).Test4)
StudentFile.Write(",")
StudentFile.Write(MyStudentArray(StudentCounter).Test4)
StudentFile.Write(",")
StudentFile.WriteLine(MyStudentArray(StudentCounter).Test5)
Next
StudentFile.Close()
End Sub
End Class

How to stop button events from firing

I have a problem with my btnNext.
I have btnNext and I want to limit it at its 9thclick.
After 9 clicks the button should become disabled. How do I do it?
Private Sub Button3_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnnext.Click
If btnnext.Text = "Submit" Then
calculate()
btnnext.Text = "Next>"
ElseIf btnnext.Text = "Next>" Then
CurrentRow += 5
cat += 5
showdata()
updatelbl()
clear_radio()
' calculate_case(1)
' calculate_case(2)
' calculate_case(3)
' calculate_case(4)
btnnext.Text = "Submit"
If CurrentRow & cat = ds.Tables("evaluation").Rows.Count >= 20 Then
MsgBox("Last Questions is Reached!!!")
End If
If btnnext.Text = "Management of Learning" Then
btnnext.Text = "Finish"
If btnnext.Text = "Finish" Then
CurrentRow = 35
cat = 35
MsgBox("Comment")
End If
End If
End If
End Sub
Private btnCount As Integer
Private Sub Button3_Click(...)...
btnCount += 1
If btnCount = 9 Then Button3.Enabled = False
'remainder of code here...
Like this
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Static ctClicks As Integer = 0
ctClicks += 1
If ctClicks = 9 Then Button1.Enabled = False
'other code
'
End Sub