Timer enables, when it should not in vb.net - vb.net

With a button click i'm trying to check existing rows in the listview.
Private Sub Button8_Click(sender As Object, e As EventArgs) Handles
Button8.Click
If ListViewExtended1.Items.Count >= 0 Then
timer_firstROW.Enabled = True
Else
timer_firstROW.Enabled = False
End If
If ListViewExtended1.Items.Count >= 1 Then
timer_SecondRow.Enabled = True
Else
timer_SecondRow.Enabled = False
End If
If ListViewExtended1.Items.Count >= 2 Then
Timer_ThirdRow.Enabled = True
Else
Timer_ThirdRow.Enabled = False
End If
End Sub
the problem is that i got an invalid argument exception = 1 in timer's sub
Private Sub timer_SecondRow_Tick(sender As Object, e As EventArgs) Handles
timer_SecondRow.Tick
timer_SecondRow.Interval = ListViewExtended1.Items(1).SubItems(3).Text
End Sub
all timers a disabled by default, so when we a checking if else statements and have only 1 item with index 0 in listview, i got that exception, it means that timer enabled, but why..
I tried to make code that, will check existing rows in listview, if row exist, timer will be enabled then timer will send continous messages thourgh serial. if 3 rows existing, 3 rows timers will be enabled and send messages with an interval will assign with "ListViewExtended1.Items(1).SubItems(3).Text". COde of timers:
Private Sub Timer4_Tick(sender As Object, e As EventArgs) Handles
timer_firstROW.Tick
timer_firstROW.Interval = ListViewExtended1.Items(0).SubItems(3).Text
count_for_rx = ListViewExtended1.Items(0).SubItems(4).Text + 1
ListViewExtended1.Items(0).SubItems(4).Text = count_for_rx
GetValueFromlv2Row = "434D4431" & ListViewExtended1.Items(0).SubItems(0).Text & ListViewExtended1.Items(0).SubItems(1).Text & ListViewExtended1.Items(0).SubItems(2).Text.Replace(" ", "")
Dim count As Short
Dim bytestosend(0) As Byte
For count = 1 To GetValueFromlv2Row.Length Step 2 'For count = 249 To Text4.Text * 2 Step 8 page 32
bytestosend(0) = CLng("&H" & Mid(GetValueFromlv2Row, count, 2))
spObj.Write(bytestosend, 0, bytestosend.Length)
Next
spObj.Write(Microsoft.VisualBasic.ControlChars.Cr)
End Sub
Private Sub timer_SecondRow_Tick(sender As Object, e As EventArgs)
Handles timer_SecondRow.Tick
If Not ListViewExtended1.Items(1).ToString() = "" Then
timer_SecondRow.Interval =
ListViewExtended1.Items(1).SubItems(3).Text
count_for_rx = ListViewExtended1.Items(1).SubItems(4).Text + 1
ListViewExtended1.Items(1).SubItems(4).Text = count_for_rx
GetValueFromlv2Row = "434D4431" &
ListViewExtended1.Items(1).SubItems(0).Text &
ListViewExtended1.Items(1).SubItems(1).Text &
ListViewExtended1.Items(1).SubItems(2).Text.Replace(" ", "")
Dim count As Short
Dim bytestosend(0) As Byte
For count = 1 To GetValueFromlv2Row.Length Step 2 'For count = 249
To Text4.Text * 2 Step 8 page 32
bytestosend(0) = CLng("&H" & Mid(GetValueFromlv2Row, count, 2))
spObj.Write(bytestosend, 0, bytestosend.Length)
Next
spObj.Write(Microsoft.VisualBasic.ControlChars.Cr)
Else
timer_SecondRow.Enabled = False
End If
End Sub
Private Sub Timer_ThirdRow_Tick(sender As Object, e As EventArgs) Handles
Timer_ThirdRow.Tick
Timer_ThirdRow.Interval = ListViewExtended1.Items(2).SubItems(3).Text
count_for_rx = ListViewExtended1.Items(2).SubItems(4).Text + 1
ListViewExtended1.Items(2).SubItems(4).Text = count_for_rx
GetValueFromlv2Row = "434D4431" &
ListViewExtended1.Items(2).SubItems(0).Text &
ListViewExtended1.Items(2).SubItems(1).Text &
ListViewExtended1.Items(2).SubItems(2).Text.Replace(" ", "")
Dim count As Short
Dim bytestosend(0) As Byte
For count = 1 To GetValueFromlv2Row.Length Step 2 'For count = 249 To
Text4.Text * 2 Step 8 page 32
bytestosend(0) = CLng("&H" & Mid(GetValueFromlv2Row, count, 2))
spObj.Write(bytestosend, 0, bytestosend.Length)
Next
spObj.Write(Microsoft.VisualBasic.ControlChars.Cr)
End Sub

Related

how to count the number of check boxes checked in visual basic?

Private Sub Btn_Cast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Cast.Click
Dim Counter As Integer = 0
If ChkBox_Barton.CheckState = 1 And ChkBox_Martin.CheckState = 1 And ChkBox_Richards.CheckState = 1 Then
MsgBox("Don't vote for more than 2")
End If
Dim Count_Barton As Integer
Dim Count_Martin As Integer
Dim Count_Richards As Integer
If ChkBox_Barton.Checked Then Count_Barton += 1
If ChkBox_Martin.Checked = 1 Then Count_Martin += 1
If ChkBox_Richards.CheckState = 1 Then Count_Richards += 1
End Sub
Problem is, I'm trying to count it everytime, then let it reset and count again.
Example. I select Barton one time, click vote, then i should be able to select someone new and click vote and it should keep counting.
what can I do?
I need to then display my results. Should I just hold the number in a text or Integer file then display it that way?
I quickly set up your application myself.
Following Code applies to this GUI:
Code:
Public Class VoteCounter
Dim intCountBarton As Integer
Dim intCountMartin As Integer
Dim intCountRichards As Integer
Private Sub ButtonVote_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonVote.Click
If CheckBoxBarton.CheckState = 1 And CheckBoxMartin.CheckState = 1 And CheckBoxRichards.CheckState = 1 Then
MsgBox("Don't vote for more than 2")
CheckBoxBarton.Checked = False
CheckBoxMartin.Checked = False
CheckBoxRichards.Checked = False
End If
If CheckBoxBarton.Checked Then
intCountBarton += 1
End If
If CheckBoxMartin.Checked Then
intCountMartin = intCountMartin + 1
End If
If CheckBoxRichards.Checked Then
intCountRichards = intCountRichards + 1
End If
CheckBoxBarton.Checked = False
CheckBoxMartin.Checked = False
CheckBoxRichards.Checked = False
End Sub
Private Sub ButtonResult_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonResult.Click
MsgBox("Barton: " & intCountBarton & vbNewLine & "Martin: " & intCountMartin & vbNewLine & "Richards: " & intCountRichards)
End Sub
Private Sub ButtonReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonReset.Click
CheckBoxBarton.Checked = False
CheckBoxMartin.Checked = False
CheckBoxRichards.Checked = False
intCountBarton = 0
intCountMartin = 0
intCountRichards = 0
End Sub
End Class
Dim Count_Barton As Integer = 0
Dim Count_Martin As Integer = 0
Dim Count_Richards As Integer = 0
Private Sub Btn_Cast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Cast.Click
Dim Counter As Integer = 0 'NOT SURE WHAT THIS IS DOING... NOT BEING USED
If ChkBox_Barton.CheckState = 1 And ChkBox_Martin.CheckState = 1 And ChkBox_Richards.CheckState = 1 Then
MsgBox("Don't vote for more than 2")
Else
If ChkBox_Barton.Checked Then Count_Barton += 1
If ChkBox_Martin.Checked = 1 Then Count_Martin += 1
If ChkBox_Richards.CheckState = 1 Then Count_Richards += 1
End If
End Sub

Score not being calculated correctly

Hi I'm created a program for a project and I've now started running some tests and the score for the user isn't being calculated correctly and I believe that it can't compare the answer given to the correct answer. I'm very confused and need any help that can be given. My code looks like this, any confusing parts and I'll try and explain.
Imports System.IO
Public Class QuestionScreen
Dim score As Integer = 0
Dim count As Integer
Dim Difficulty_ext As String
Dim questions, answers As New List(Of String)()
Private i As Integer
Sub ReadFile()
If Main.Diff_DDown.Text = "Easy" Then
Difficulty_ext = "questions - Easy"
ElseIf Main.Diff_DDown.Text = "Medium" Then
Difficulty_ext = "questions - Medium"
Else
Difficulty_ext = "questions - Difficult"
End If
Randomize()
Dim countline = File.ReadAllLines("c:\Users\Alice\Desktop\programme files\" & Difficulty_ext & ".txt").Length
Dim numline As Integer
Dim values() As String
Using sr As New StreamReader("c:\Users\Alice\Desktop\programme files\" & Difficulty_ext & ".txt")
While Not sr.EndOfStream
values = sr.ReadLine().Split(","c)
questions.Add(values(0))
answers.Add(values(1))
End While
End Using
numline = Int(Rnd() * countline)
For i As Integer = 0 To numline
Question.Text = questions(i)
Act_ANS.Text = answers(i)
Next
End Sub
Private Sub Pass_Btn_Click(sender As Object, e As EventArgs) Handles Pass_Btn.Click
If count < 10 Then
Call ReadFile()
count = count + 1
Ans_TxtBx.Text = ""
Hid_Score.Text = score
Else
ResultsScreen.Show()
Me.Hide()
End If
End Sub
Public Sub Submit_Btn_Click(sender As Object, e As EventArgs) Handles Submit_Btn.Click
If count < 10 Then
Call ReadFile()
count = count + 1
If Ans_TxtBx.Text = answers(i) Then
score = score + 1
End If
Hid_Score.Text = score
Else
ResultsScreen.Show()
Me.Hide()
count = 0
End If
Ans_TxtBx.Text = ""
End Sub
Private Sub QuestionScreen_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Call ReadFile()
End Sub
End Class
OK..... Try this, I have debugged your code trying to leave it pretty much as you had it, there were a number of problems, nothing major just a few lines of code in the wrong place....
Imports System.IO
Public Class Form1
Dim score As Integer = 0
Dim count As Integer
Dim Difficulty_ext As String
Dim questions, answers As New List(Of String)()
Private i As Integer
Sub ReadFile()
If Diff_DDown.Text = "Easy" Then
Difficulty_ext = "questions - Easy"
ElseIf Diff_DDown.Text = "Medium" Then
Difficulty_ext = "questions - Medium"
Else
Difficulty_ext = "questions - Difficult"
End If
Randomize()
Try
Dim countline = File.ReadAllLines("c:\Users\Alice\Desktop\programme files\" & Difficulty_ext & ".txt").Length
Dim numline As Integer
Dim values() As String
' clear the list of questions and answers
answers.Clear()
questions.Clear()
'''''''''''''''''''''''''''''''''''''''''
Using sr As New StreamReader("c:\Users\Alice\Desktop\programme files\" & Difficulty_ext & ".txt")
While Not sr.EndOfStream
values = sr.ReadLine().Split(","c)
questions.Add(values(0))
answers.Add(values(1))
End While
End Using
numline = Int(Rnd() * countline)
For i = 0 To numline
Question.Text = questions(i)
Act_ANS.Text = answers(i)
Next
Catch ex As Exception
End Try
End Sub
Private Sub Pass_Btn_Click(sender As Object, e As EventArgs) Handles Pass_Btn.Click
If count < 10 Then
count = count + 1
Ans_TxtBx.Text = ""
Hid_Score.Text = score
Else
ResultsScreen.Show()
Me.Hide()
End If
Call ReadFile() ' move this to the bottom
End Sub
Public Sub Submit_Btn_Click(sender As Object, e As EventArgs) Handles Submit_Btn.Click
If count < 10 Then
count = count + 1
If Ans_TxtBx.Text = answers(i - 1) Then ' need to subtract 1 here
score = score + 1
End If
Hid_Score.Text = score
Else
ResultsScreen.Show()
Me.Hide()
count = 0
End If
Ans_TxtBx.Text = ""
Call ReadFile() ' move this to the bottom
End Sub
Private Sub QuestionScreen_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Call ReadFile()
End Sub
End Class

Specify which button to run on event vb.net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim rows, column, j, i
rows = 4
column = 13
For j = 0 To rows - 1
Dim r As New TableRow()
For i = 0 To column - 1
Dim c As New TableCell()
If i = 0 And j = 0 Then
c.Text = "TIME/COURT"
r.Cells.Add(c)
ElseIf i = 0 And j = 1 Then
c.Controls.Add(New LiteralControl("Court A"))
r.Cells.Add(c)
ElseIf i = 0 And j = 2 Then
c.Controls.Add(New LiteralControl("Court B"))
r.Cells.Add(c)
ElseIf i = 0 And j = 3 Then
c.Controls.Add(New LiteralControl("Court C"))
r.Cells.Add(c)
ElseIf i >= 1 And j = 0 Then
c.Controls.Add(New LiteralControl(x & "-" & x + 1))
r.Cells.Add(c)
x += 1
Else
btn = New Button
btn.ID = "btnr" & j & "c" & i
AddHandler btn.Click, AddressOf Change_Colour
c.Controls.Add(btn)
r.Cells.Add(c)
End If
Next
booktable.Rows.Add(r)
Next
End Sub
Protected Sub Change_Colour(sender As Object, e As EventArgs)
btn.BackColor = Drawing.Color.LightGreen
End Sub
When I click on any of the button, only the last button turns light green.
How can I set the button to run the event when I click it?
Can I call the button by button.ID? How?
If multiple buttons can call your Change_Colour routine them you will need to actually determine which one did call it. so you will need something like:
Protected Sub Change_Colour(sender As Object, e As EventArgs)
Dim btn as Button = Ctype(sender, Button)
'here you will need to determine which button is the sender which you could do by checking its name for example
'eg Select case btn.name
'case add stuff here
'end select
btn.BackColor = Drawing.Color.LightGreen
End Sub

how to count the total number of checked data grid view check boxes by rows not by columns

We are currently doing a student monitoring attendance , and we want to count the total number of days each student is present and absent .
Subject LN FN MI 05/21/14 05/20/14 05/21/14 05/22/14 05/23/14 P A
Comp101 Yu Erick C (checked|(unchecked)|(checked)|(checked)|(checked)|4 | 1
"This is what we wanted to do but instead of counting horizontally it counts vertically.Is there anyone who can help us solving this problem?
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Present As Integer = 0
Dim Absent As Integer = 0
For a = 0 To DataGridView1.RowCount - 1
For b = 0 To DataGridView1.ColumnCount - 8
If DataGridView1.Rows(a).Cells(b + 5).Value = True Then
Present += 1
Else
Absent += 1
End If
Next
DataGridView1.Rows(a).Cells(10).Value = Present
DataGridView1.Rows(a).Cells(11).Value = Absent
Present = 0
Absent = 0
Next
End Sub
Try This ....
I Think this is what you want...
This Code Actually Check every Cell that contains Checkbox Or DatePresented
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim count As Integer = 0
For a = 0 To DataGridView1.RowCount - 1
For b = 0 To DataGridView1.ColumnCount - 2
If DataGridView1.Rows(a).Cells(b + 1).Value = True Then
count += 1
End If
Next
DataGridView1.Rows(a).Cells(6).Value = count
count = 0
Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim count As Integer = 0
For a = 0 To DataGridView1.RowCount - 1
For b = 0 To DataGridView1.ColumnCount - 6
If DataGridView1.Rows(a).Cells(b + 5).Value = True Then
count += 1
End If
Next
DataGridView1.Rows(a).Cells(4).Value = count
count = 0
Next
End Sub
Leave the Attendance Column Blank cause the Total will be insert there...
Wew, I Think it solves your Problem =D
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Present As Integer = 0
Dim Absent As Integer = 0
For a = 0 To DataGridView1.RowCount - 1
For b = 0 To DataGridView1.ColumnCount - 6
If DataGridView1.Rows(a).Cells(b + 5).Value = True Then
Present += 1
Else
Absent += 1
End If
Next
DataGridView1.Rows(a).Cells(4).Value = "Present: " & Present & " Absent: " & Absent
Present = 0
Absent = 0
Next
End Sub
Okay Here it is all done...

VB.Net ComboBox.SelectedIndex always reverts to 1

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