Why does my loop in the timer control stops suddenly VB.net - vb.net

Now when I have my code in my one project where this is the ONLY code (note, I got a listbox that is full of items around 29 items!), the function works properly and it loops through every single item and gives it out as a messagebox.
When I enter this code into my bigger project where I meticulously copy pasted the code, the loop in the timer stops after 3 times EVERY TIME. Even when I enter in the timer1.
If Count < "29" Then
Still it stops after three times EVERY TIME. What am I doing wrong, the sources are pretty much the same?!
Code:
Public Class Form1
Dim NumberOfItems As Integer
Dim Count As String
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If Timer1.Enabled = False Then
Timer1.Enabled = True
Button2.Text = "Stop Timer"
NumberOfItems = ListBox1.Items.Count
Count = "0"
Else
Timer1.Enabled = False
Button2.Text = "Start Timer"
End If
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If Count < NumberOfItems Then
Dim CurrentIt3m As String = ListBox1.Items(Count)
MsgBox(CurrentIt3m)
Count = Count + 1
Else
Button2.Text = "Start Timer"
End If
End Sub
End Class

Related

Vb.net How to Make the Timer countdown restart at 30 seconds when clicked on a new button in the same form

I'm trying to do a project and i'm still learning and this timer thing is something very new to me.
*You are required to build a board game. The game should be played on a 4 x 4 grid in pairs. Each grid cell contains a hidden question which the pair needs to answer.
The players take turns in playing the game. Each player generates two random numbers and place the cell on the grid indicated by the numbers. Once clicked, the question for the clicked grid cell is displayed. For example if the generated number is a 2 and a 3 the player clicks in the 2 x 3 grid cell to view the hidden question. You may use any kind of questions.
A player should only be able to click and view the question hidden beneath the grid cell represented by the random numbers.
At the start of the game, the initial score for each player should have a value of 0. For each question answered, the score increases by 5. You can display the score anywhere within the interface.
Each player should be given a duration of 30 seconds to answer a question. This value should be displayed on the user interface. The value should decrease with time and this decrement should be visible on screen at any point in time. Once the value reaches 0 the other player should be given the chance to answer the question. If both the players fail in answering a question, the question should be marked as unanswered. You can display the time left anywhere within the user interface.
The game should end when all the questions have been attempted by the players. When the game ends, the winner should be announced. The winner should be the one with the highest score.
Once the game is over, the questions and answers along with the players should be displayed.*
This is what its supposed to look like
This is what mine looks like
Now this is working just fine when i click on the 1st button even when they get the answer wrong or they go over time the timers will reset and countdown from 30 but when i click on the 2nd button the timer doesn't work even when i attached a second timer.
here is the code
Public Class Form1
Private TargetDT As DateTime
Private TargetDT1 As DateTime
Public CountDownFrom As TimeSpan = TimeSpan.FromSeconds(33)
Public CountDownFrom1 As TimeSpan = TimeSpan.FromSeconds(33)
Private TargetDTQ2 As DateTime
Private TargetDTQ2a As DateTime
Public CountDownFromQ2 As TimeSpan = TimeSpan.FromSeconds(33)
Public CountDownFromQ2a As TimeSpan = TimeSpan.FromSeconds(33)
Private Sub frmSinglePlayer_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Timer1.Interval = 500
TargetDT = DateTime.Now.Add(CountDownFrom)
TargetDT1 = DateTime.Now.Add(CountDownFrom1)
TargetDTQ2 = DateTime.Now.Add(CountDownFromQ2)
TargetDTQ2a = DateTime.Now.Add(CountDownFromQ2a)
End Sub
Public Sub Timer1_Tick(sender As Object, e As System.EventArgs) Handles Timer1.Tick
Dim ts As TimeSpan = TargetDT.Subtract(DateTime.Now)
Dim ts1 As TimeSpan = TargetDT1.Subtract(DateTime.Now)
If ts.TotalMilliseconds > 0 Then
lblTime1.Text = ts.ToString("ss")
Else
lblTime1.Text = ":00"
Timer1.Stop()
MessageBox.Show("Out of Time!! Now player 2 can answer")
lblTime1.Text = ts1.ToString("ss")
Q1.TextBox1.Hide()
Q1.Button1.Hide()
Q1.TextBox2.Show()
Q1.Button2.Show()
End If
End Sub
Public Sub Timer2_Tick(sender As Object, e As System.EventArgs) Handles Timer2.Tick
Dim ts2 As TimeSpan = TargetDTQ2.Subtract(DateTime.Now)
Dim ts3 As TimeSpan = TargetDTQ2a.Subtract(DateTime.Now)
If ts2.TotalMilliseconds > 0 Then
lblTime1.Text = ts2.ToString("ss")
Else
lblTime1.Text = ":00"
Timer2.Stop()
MessageBox.Show("Out of Time!! Now player 2 can answer")
lblTime1.Text = ts3.ToString("ss")
Q1.TextBox1.Hide()
Q1.Button1.Hide()
Q1.TextBox2.Show()
Q1.Button2.Show()
End If
End Sub
Private Sub btn1_Click(sender As Object, e As EventArgs) Handles btn1.Click
Q1.Show()
Timer1.Start()
End Sub
Private Sub btn2_Click(sender As Object, e As EventArgs) Handles btn2.Click
Q2.Show()
Timer2.Start()
End Sub
Private Sub btn3_Click(sender As Object, e As EventArgs) Handles btn3.Click
End Sub
Private Sub btnNew_Click(sender As Object, e As EventArgs) Handles btnNew.Click
Application.Restart()
End Sub
End Class
This is my code for the second Button
Public Class Q2
Private TargetDT As DateTime
Public CountDownFrom As TimeSpan = TimeSpan.FromSeconds(36)
Private Sub frmSinglePlayer_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Timer3.Interval = 500
TargetDT = DateTime.Now.Add(CountDownFrom)
End Sub
Public Sub Timer3_Tick(sender As Object, e As System.EventArgs) Handles Timer3.Tick
Dim ts As TimeSpan = TargetDT.Subtract(DateTime.Now)
If ts.TotalMilliseconds > 0 Then
Form1.lblTime2.Text = ts.ToString("ss")
Else
'lblTime1.Text = ":00"
Timer3.Stop()
'MessageBox.Show("Out of Time!! Now player 2 can answer")
'Form1.lblTime1.Text = ts.ToString("ss")
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Correctanswer As String
Correctanswer = ("Venus")
Dim result As Integer
result = String.Compare(TextBox1.Text, Correctanswer, True)
Dim result2 = String.Compare(TextBox2.Text, Correctanswer, True)
If result = 0 Then
MessageBox.Show("Bingo!")
Form1.txtP1.Text = Val(Form1.txtP1.Text) + 1
Form1.lblTime1.Text = ":00"
Form1.lblTime1.Hide()
Me.Close()
ElseIf result < 0 Then
MessageBox.Show("Wrong!")
TextBox1.Text = " "
Form1.lblTime1.Text = ":00"
Form1.lblTime1.Hide()
Form1.lblTime2.Show()
Timer3.Start()
TextBox1.Hide()
TextBox2.Show()
Button1.Hide()
Button2.Show()
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim Correctanswer As String
Correctanswer = ("Venus")
Dim result As Integer
result = String.Compare(TextBox1.Text, Correctanswer, True)
Dim result2 = String.Compare(TextBox2.Text, Correctanswer, True)
If result2 = 0 Then
MessageBox.Show("Bingo!")
Form1.txtP2.Text = Val(Form1.txtP2.Text) + 1
Form1.lblTime2.Text = ":00"
Form1.lblTime2.Hide()
Me.Close()
ElseIf result < 0 Then
MessageBox.Show("Wrong!")
TextBox1.Text = " "
Form1.lblTime1.Text = ":00"
Form1.lblTime1.Hide()
Form1.lblTime2.Show()
Me.Close()
End If
End Sub
End Class
PS. Also if someone can help explain how to change a buttons color from the code and not on click (This is so that the palyers can see what grid they have already clicked on)

I want to display a list of names(3) from a textbox(user input) into a label

This is the code I have so far but is not working properly. The textbox for the names user should input and the button show to display the names in a label in the order entered.
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim intcount, w1, w2, w3 As Integer
Dim intMax As Integer = 2
For intcount = 0 To intMax
strSurnames(intcount) = TextBox1.Text
Next
TextBox1.Clear()
End Sub
Private Sub btnShow_Click(sender As Object, e As EventArgs) Handles btnShow.Click
Dim intMax As Integer = 2
For intcount = 0 To intMax
lblShow.Text &= strSurnames(intcount)
Next
End Sub
I am guessing you are going to use only 1 textbox and click the "add button" multiple times to store the name. If this is true, you will first need to create
Dim arrayStr As New List(Of String)
Every single time you click on the add button, it will add into this array.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
arrayStr.Add(TextBox1.Text.Trim())
TextBox1.Clear()
End Sub
And to show the full name in just one label when you click on the "Show button", can do it like this
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Label1.Text = "" //The label text is cleared.
For i As Integer = 0 To arrayStr.Count() - 1
Label1.Text += arrayStr(i) + ", "
Next
End Sub
UPDATE - BASED ON YOUR COMMENT QUESTION
This is the updated solution based on your question. I am only going to show you with the "Sur Names" only. You can implement the "Weight" the same way.
First, create your array and declare a count as integer for the size of your array.
Dim surNameStr(20) As String
Dim count As Integer = 0
In the "Add Button", you increase the count number by 1 every time you add a new sur name. Once it reached your "maximum" number, you disable your button by BtnAdd.Enabled = False.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If count < 20 Then
surNameStr(count) = TextBox1.Text.Trim()
count = count + 1
Else
Button1.Enabled = False
Button2.Enabled = True
End If
End Sub
Then, in "Show Button", this is how you can show all the surnames stored.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Label1.Text = ""
For i As Integer = 0 To 19
Label1.Text += surNameStr(i) + vbNewLine
Next
End Sub

read line by line from richtextbox and show in label (vb.net)

I would like to read line by line from richtextbox and show each line every a second in label.
I have this code blocks.
and I think I need a timer but I couldnt make it.
can you help me ?
Remarks :
If I use this code , I can only see the last line in label.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim RotateCount As String()
For i As Integer = 0 To RichTextBox1.Lines.Length - 1
Label1.Text = RichTextBox1.Lines(i)
Next
End Sub
I mean, assume that we have lines in richtextbox like..
a1
b2
c3
d4
e5
and I would like to show label1 in for each second like..
a1
(after 1 sec.)
b2
(after 1 sec.)
c3
(after 1 sec.)
like this...
You seems to expect that, because you set the Text property, the label repaints itself immediately with the new text. This doesn't happen until you exit from the event handler and the system could repaint the label. Of course, with this code, only the last text is shown.
To reach your goal, you could use a Timer set to 1 second interval and a counter that keeps track of the current line dispayed:
Dim tm As System.Windows.Forms.Timer = new System.Windows.Forms.Timer()
Dim counter As Integer = 0
At this point your button click just start the timer and exits
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
tm.Interval = 1000
AddHandler tm.Tick, AddressOf onTick
tm.Start()
' Don't allow to click again this button until
' the timer is stopped
Button1.Enabled = False
Button2.Enabled = True
End Sub
When the Tick event is raised you change the label text to the line indexed by the counter, increment it and check if you have reached the last line restarting from the first one if this is the case. Note that the button is disabled before exiting. This is required to avoid a second/third/fourth/etc click on the same button while the timer runs..... More on Button2 later....
Sub onTick(sender as Object, e as EventArgs)
Label1.Text = RichTextBox1.Lines(counter)
counter += 1
if counter >= RichTextBox1.Lines.Count Then
counter = 0
End If
End Sub
Of course, now you need another button to stop the Timer run and reenable the first button
' This button stops the timer and reenable the first button disabling
' itself - It should start as disabled from the form-designer
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
tm.Stop
RemoveHandler tm.Tick, AddressOf onTick
Button1.Enabled = True
Button2.Enabled = False
End Sub
You're almost there. Your problem is that you keep setting the text, not adding to it. Label1.Text = ... sets the text, if you want to keep adding to it you'd use Label1.Text &= ...
Also note that you need to include something like Environment.NewLine in order to include line breaks.
For i As Integer = 0 To RichTextBox1.Lines.Length - 1
Label1.Text &= RichTextBox1.Lines(i) & If(i < RichTextBox1.Lines.Length - 1, Environment.NewLine, "")
Next
thank you for your help !!!
I solved with this code ;
Public Class Form1
Dim tm = New System.Windows.Forms.Timer()
Dim counter As Integer = 0
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Sub onTick(sender As Object, e As EventArgs)
Label1.Text = RichTextBox1.Lines(counter)
counter += 1
If counter >= RichTextBox1.Lines.Count Then
counter = 0
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
For i = 0 To RichTextBox2.Lines.Count - 1
TextBox1.Text = RichTextBox2.Lines(i)
wait(2000)
Next
End Sub
Private Sub wait(ByVal interval As Integer)
Dim sw As New Stopwatch
sw.Start()
Do While sw.ElapsedMilliseconds < interval
' Allows UI to remain responsive
Application.DoEvents()
Loop
sw.Stop()
End Sub
End Class
It is very simple.
Declare one more string variable and load all string to this variable .
Improved code is given below.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As system.EventArgs) Handles Button1.Click
Dim a1 as int32
a1=0
'get number of lines of the rich text box content
a1=RichTextBox1.Lines.Count()
Dim str As String
For i As Int32 = 0 To a1-1
str = str + RichTextBox1.Lines(i)
Label1.Text= str
Next
End Sub

Visual Studio Button Event

I would like to code a button that after 3 clicks, links the user to my site. The first 2 should generate a code in the textbox and the last one should then link them. This is what i have so far
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
ProgressBar1.Increment(3)
If ProgressBar1.Value = 100 Then
TextBox1.Text = "Thank you"
Timer1.Stop()
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If RadioButton1.Checked = True Then
Timer1.Start()
ElseIf RadioButton2.checked = True Then
Timer1.Start()
Else
TextBox1.Text = "Please Select Option"
End If
End Sub
I dont know what is the use of ProgressBar and Timer. But if you wan to know how many times user has click on a button, a counter variable should do.
Private _clickCounter As Integer
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
_clickCounter += 1
If _clickCounter = 3 Then
MsgBox("3 times")
_clickCounter = 0
'link to your site
Else
'generate code in textbox
End If
End Sub
Use an integer that increases each time the button is clicked and a Select Case statement to detect that;
Dim NumberOfClicks As Integer = 0
Dim webAddress As String = "http://www.YourWebsite.com/"
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
number = number + 1
Select Case number
Case 1
''''' Code for your textbox
Case 2
''''' Code for your textbox
Case 3
Process.Start(webAddress)
End Sub
This should do. Please mark my answer as solved if it helps.

Add minutes to an existing countdown timer

how To add minutes to an existing countdown timer like while the timer is counting down I can add 10 mins with a push of a button so the time pluses like if the displayed time is 9.59 with the press of a button the time should display 19.59 and continue counting down
Public Class TIMER
Private alarmtime As Date
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If alarmtime < Date.Now Then
Me.Timer1.Stop()
MessageBox.Show("Time up")
Else
Dim remainingtime As TimeSpan = Me.alarmtime.Subtract(Date.Now)
Me.Label1.Text = String.Format("{0}:{1:d2}:{2:d2}", _
remainingtime.Hours, _
remainingtime.Minutes, _
remainingtime.Seconds)
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If RadioButton1.Checked = True Then
Me.alarmtime = Date.Now.AddHours(TextBox1.Text)
Me.Timer1.Start()
ElseIf RadioButton2.Checked = True Then
Me.alarmtime = Date.Now.AddMinutes(TextBox1.Text)
Me.Timer1.Start()
ElseIf RadioButton3.Checked = True Then
Me.alarmtime = Date.Now.AddSeconds(TextBox1.Text)
Me.Timer1.Start()
End If
End Sub
Private Sub GameTIMER_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
End Class
its just a basic countdown timer it works fine all I need for it to do is add minutes to it while its is counting down because when the timer is up I have to click the text box type in the number then press ok then the timer starts so instead off writhing in the textbox I just want to press a button and the countdown timer automatically adds lets say 10 minutes to current time being displayed while its counting down
If you're talking about a normal Timer, this should be your answer:
Me.Timer1.Interval += (60000 * 10)
EDIT:
Try stopping it first:
Me.Timer1.Stop()
Me.Timer1.Interval += (60000 * 10)
Me.Timer1.Start()