how to count the number of check boxes checked in visual basic? - vb.net

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

Related

I'm doing a VB.net Caesar Cipher

Good day,
i'm having trouble of finishing this homework :<
i'm doing a caesar cipher where the user will have a txt file
then the program will find the text file and read what's inside of it
then overwrite the text file
so far i'm able to do the code but i'm getting a
index out of range and nullreference exception
here's my code
Imports System.IO
Public Class main_form
Dim x, y, z, str_len As Integer 'where x is a counter for the array
Dim loc, read(5), write(), str_1, str_2, aa As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub brw_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles brw_btn.Click
TextBox1.Clear()
OFD1.ShowDialog()
TextBox1.Text = OFD1.FileName
loc = OFD1.FileName
read = File.ReadAllLines(OFD1.FileName)
End Sub
Private Sub enc_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles enc_btn.Click
'Formula of Caesar's Cipher
z = 1
x = 0
If read(x) <> "" Then
While read(x) <> ""
str_2 = ""
str_1 = read(x)
str_len = Len(str_1)
MessageBox.Show(str_2)
For i As Integer = 0 To str_len - 1
y = Asc(Mid(str_1, i + 1, i + 2))
y = y + Val(TextBox2.Text)
str_2 = str_2 + Chr(y)
MessageBox.Show(str_2)
Next
MessageBox.Show(str_2)
write(x) = str_2
File.AppendAllLines(OFD1.FileName, write(x))
x += 1
End While
Else
End If
End Sub
End Class
Thanks!
Of course you are getting out of range exception because :
While read(x) <> ""
...
...
x += 1 'if x goes out of range the next loop in while will access read(x <-out of range)
End While
You have to insert
While read(x) <> ""
...
...
x += 1
If x >= read.Length
Exit While
End If
End While
Also:
Dim write() as String
...
write(x) = str_2 '<-write is used un initialized, null reference exception
Change write() to write or:
Private Sub enc_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles enc_btn.Click
ReDim write(read.Length) '<---
z = 1
x = 0
If read(x) <> "" Then
...
...
Valter

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

The button.BackColor statement seem to be executed, but no colour change is show

I'm trying to re-create the classic game "Simon" for a project. The code I have here should hopefully create a random number, translate that to a colour change for a random button, wait a short time, and then do the same for another random button. I can't spot any problems, but on execution the buttons remain uchanged.
Public Class MenuForm
Dim failure As Boolean
Dim pattern() As Integer
Dim maincounter As Integer = 1
Dim diff As Integer
Dim sender As Object
Dim e As EventArgs
Dim timewaited As Integer
Dim timefinished As Boolean
Private Sub Menuform_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Get the difficulty level from the player
Dim InputtedDifficulty As Integer = InputBox("Please enter difficulty. 1-Easy 2-Medium 3-Hard")
'Validate difficulty choice
Do While InputtedDifficulty > 3 Or InputtedDifficulty < 1
InputtedDifficulty = InputBox("Input incorrect. Please re-enter selection. 1-Easy 2-Medium 3-Hard")
Loop
'Set speed of blinking based on difficulty choice
Select Case InputtedDifficulty
Case 1
diff = 1000
Case 2
diff = 500
Case 3
diff = 20
End Select
End Sub
Private Sub run_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles run.Click
Call GameController()
End Sub
Private Sub GameController()
Dim buttonRepeater As Integer
'Call checkFail()
Do While failure = False
maincounter = maincounter + 1
Call Pattern_creator(sender, e)
For buttonRepeater = 1 To maincounter
Call button_controller(sender, e)
timewaited = 0
timefinished = False
ButtonTimer.Enabled = True
If timefinished = True Then
End If
Button1.BackColor = Color.Blue
Button2.BackColor = Color.Blue
Button3.BackColor = Color.Blue
Button4.BackColor = Color.Blue
Next buttonRepeater
Loop
End Sub
Private Sub Pattern_creator(ByVal sender As System.Object, ByVal e As System.EventArgs)
ReDim Preserve pattern(maincounter)
Randomize()
pattern(maincounter) = Int((Rnd() * 4) + 1)
ReDim Preserve pattern(maincounter + 1)
End Sub
Private Sub button_controller(ByVal sender As System.Object, ByVal e As System.EventArgs)
'Ths case statement takes the random number generated earlier and translates that to
'a button flash
Select Case pattern(maincounter)
Case 1
Button1.BackColor = Color.Red
Case 2
Button2.BackColor = Color.Red
Case 3
Button3.BackColor = Color.Red
Case 4
Button4.BackColor = Color.Red
End Select
End Sub
Private Sub ButtonTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonTimer.Tick
If timewaited = 5 Then
ButtonTimer.Enabled = False
timefinished = True
Else
timewaited = timewaited + 1
End If
End Sub
End Class
Any help would be very much appreciated, I've been staring at this for ages with no progress.

How can I end my loop?

I made a loop to show the balance when entered a name, but when I try to do it a second time. It doesn't return another output(messagebox). How can I end/restart the loop?
Public Class Form1
Dim intMax_Subscript As Integer = 7
Dim arrayNames(intMax_Subscript) As String
Dim arrayBalance(intMax_Subscript) As Double
Dim found As Boolean
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CBO1.Items.Add("John One")
arrayNames(0) = CStr(CBO1.Items(0))
CBO1.Items.Add("Jack Two")
arrayNames(1) = CStr(CBO1.Items(1))
CBO1.Items.Add("John Three")
arrayNames(2) = CStr(CBO1.Items(2))
CBO1.Items.Add("Jack Four")
arrayNames(3) = CStr(CBO1.Items(3))
CBO1.Items.Add("John Five")
arrayNames(4) = CStr(CBO1.Items(4))
CBO1.Items.Add("Jack Six")
arrayNames(5) = CStr(CBO1.Items(5))
CBO1.Items.Add("John Seven")
arrayNames(6) = CStr(CBO1.Items(6))
CBO1.Items.Add("Jack Eight")
arrayNames(7) = CStr(CBO1.Items(7))
CBO2.Items.Add("235.50")
arrayBalance(0) = CDbl(CBO2.Items(0))
CBO2.Items.Add("78943.98")
arrayBalance(1) = CDbl(CBO2.Items(1))
CBO2.Items.Add("230781.10")
arrayBalance(2) = CDbl(CBO2.Items(2))
CBO2.Items.Add("78362.00")
arrayBalance(3) = CDbl(CBO2.Items(3))
CBO2.Items.Add("12097.20")
arrayBalance(4) = CDbl(CBO2.Items(4))
CBO2.Items.Add("89267.34")
arrayBalance(5) = CDbl(CBO2.Items(5))
CBO2.Items.Add("34959.06")
arrayBalance(6) = CDbl(CBO2.Items(6))
CBO2.Items.Add("559284.50")
arrayBalance(7) = CDbl(CBO2.Items(7))
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBalance.Click
Dim input As String = InputBox("Input a name")
Dim intcount As Integer = -1
Dim intSubscript As Integer = 0
Do While Not found And intcount < arrayNames.Length
If arrayNames(intSubscript) = input Then
found = True
MessageBox.Show("The balance of " & input & "'s is " & arrayBalance(intSubscript))
intSubscript = 0
intcount = -1
End If
intSubscript += 1
Loop
If Not arrayNames.Contains(input) Then
MessageBox.Show("Error: Name not found.")
End If
End Sub
You Need to reset the found to false after showing message box
If Not arrayNames.Contains(input) Then
MessageBox.Show("Error: Name not found.")
found = False
End If
you don't need the 'found' variable at all
Do While intcount < arrayNames.Length
If arrayNames(intSubscript) = input Then
MessageBox.Show("The balance of " & input & "'s is " & arrayBalance(intSubscript))
intSubscript = 0
Exit Do
End If
intSubscript += 1
Loop