VB.Net Answer Checker in Quiz - vb.net

Please help me solve my problem in quiz checker. I have 4 choices which display in Radio Button. So my problem is after i check my radio button and submit it gives me score on the corresponding question but after i click the submit button again it adds another score on the same question. how can i prevent my checker to add another score on the same question?
Here is my code.
Public Sub Check()
If Check1.Text < 2 And Check2.Text < 2 And Check3.Text < 2 And Check4.Text < 2 Then
If CorrectAns.Text = CoAnsDB.Text Then
score = score + 1
Else
score = score + 0
End If
Else
score = score + 0
End If
End Sub
Private Sub BtnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSubmit.Click
Call Check()
'Call CheckerB()
'Call CheckerC()
'Call CheckerD()
MyScore.Text = score.ToString
BtnSubmit.Enabled = False
AnsA.Enabled = False
AnsB.Enabled = False
AnsC.Enabled = False
AnsD.Enabled = False
End Sub
Private Sub AnsA_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AnsA.CheckedChanged
CorrectAns.Text = "A"
If AnsA.Checked = True Then
Check1.Text = Check1.Text + 1
End If
BtnSubmit.Enabled = True
End Sub
Private Sub AnsD_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AnsD.CheckedChanged
CorrectAns.Text = "D"
If AnsB.Checked = True Then
Check4.Text = Check4.Text + 1
End If
BtnSubmit.Enabled = True
End Sub
Private Sub AnsC_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AnsC.CheckedChanged
CorrectAns.Text = "C"
If AnsC.Checked = True Then
Check3.Text = Check3.Text + 1
End If
BtnSubmit.Enabled = True
End Sub
Private Sub AnsB_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AnsB.CheckedChanged
CorrectAns.Text = "B"
If AnsB.Checked = True Then
Check2.Text = Check2.Text + 1
End If
BtnSubmit.Enabled = True
End Sub
Thank you.

Related

VB.Net Move an object[Error]

i just start create a project with this(To The Topic)
an error is start in here(maybe)
First Button Code
Private Sub DeimosButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeimosButton2.Click
State(i)
DeimosButton3.Visible = False
DeimosButton4.Visible = False
End Sub
Private Sub Timer4_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer4.Tick
DeimosButton2.Top = DeimosButton2.Top - 1
If DeimosButton2.Location = New Point(22, 54) Then
Timer4.Stop()
DeimosButton2.Enabled = True
End If
End Sub
Private Sub State(ByVal Ref As Integer)
If Ref = 0 Then
Timer4.Start()
DeimosButton2.Enabled = False
i = 1
Else
Timer5.Start()
DeimosButton2.Enabled = False
i = 0
End If
End Sub
Private Sub Timer5_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer5.Tick
DeimosButton2.Top = DeimosButton2.Top + 1
If DeimosButton2.Location = New Point(22, 85) Then
Timer5.Stop()
DeimosButton2.Enabled = True
DeimosButton3.Visible = True
DeimosButton4.Visible = True
End If
End Sub
This Second Button Code
Private Sub DeimosButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeimosButton3.Click
states(o)
DeimosButton2.Visible = False
DeimosButton4.Visible = False
End Sub
Private Sub Timer6_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer6.Tick
DeimosButton3.Top = DeimosButton3.Top - 1
If DeimosButton3.Location = New Point(22, 54) Then
Timer6.Stop()
DeimosButton3.Enabled = True
End If
End Sub
Private Sub states(ByVal Def As Integer)
If Def = 0 Then
Timer6.Start()
DeimosButton3.Enabled = False
o = 1
Else
Timer7.Start()
DeimosButton3.Enabled = False
o = 0
End If
End Sub
Private Sub Timer7_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer7.Tick
DeimosButton3.Top = DeimosButton3.Top + 1
If DeimosButton3.Location = New Point(22, 116) Then
Timer7.Stop()
DeimosButton3.Enabled = True
DeimosButton2.Visible = True
DeimosButton4.Visible = True
End If
End Sub
The problem is when i click the second button
but when i click the first button, is no matter.
Problem : The button is still moving to top, although it is has reach the position.
there is a solution for this ?
You must change
DeimosButton3.Location = New Point(22, 116)
to
DeimosButton3.Location.y = 116
This is valid to all
for all your code

Formatting percentages more thorougly

I've been asked to make a program to find percentages of males, females and children, but I'm kind of stuck - it's hard to explain, but when you enter an age that's younger than or equal to 17 it would be a child and anything over would be a male or female. However when it's entered it counts it as two people instead of one because it asks for the gender after the age if it's younger than 17. If it's hard to explain, please let me know and I'll try to improve on how I'm telling it.
Current code:
Public Class Form1
Dim MalePercent, FemalePercent, ChildPercent As Single
Dim MaleCount, FemaleCount, ChildCount, CountTotal, Age As Integer
Dim Gender As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim buttonArray = {btnMale, btnFemale}
For Each button In buttonArray
button.Enabled = False
Next
MessageBox.Show("To start this program, please click a button of your choice.")
txtAge.Select()
txtAge.Clear()
End Sub
Private Sub btnMale_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMale.Click
MaleCount += 1
CountTotal += 1
lblMaleCount.Text = MaleCount
lblCountTotal.Text = CountTotal
btnMale.Enabled = False
btnFemale.Enabled = False
txtAge.Enabled = True
txtAge.Select()
txtAge.Clear()
End Sub
Private Sub btnFemale_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFemale.Click
FemaleCount += 1
CountTotal += 1
lblFemaleCount.Text = FemaleCount
lblCountTotal.Text = CountTotal
btnFemale.Enabled = False
btnMale.Enabled = False
txtAge.Enabled = True
txtAge.Select()
txtAge.Clear()
End Sub
Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
MalePercent = MaleCount / CountTotal
lblMalePercent.Text = Format(MalePercent, "#0.00%")
FemalePercent = FemaleCount / CountTotal
lblFemalePercent.Text = Format(FemalePercent, "#0.00%")
ChildPercent = ChildCount / CountTotal
lblChildPercent.Text = Format(ChildPercent, "#0.00%")
End Sub
Private Sub btnFinish_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFinish.Click
MessageBox.Show("Thank-you for using this program. Good-bye!")
Close()
End Sub
Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
Age = txtAge.Text
If Age <= 17 Then
MessageBox.Show("Now please select the appropriate gender.")
ChildCount += 1
CountTotal += 1
lblChildCount.Text = ChildCount
lblCountTotal.Text = CountTotal
ElseIf Age >= 18 Then
MessageBox.Show("Now please select the appropriate gender.")
End If
txtAge.Enabled = False
Dim buttonArray = {btnMale, btnFemale}
For Each button In buttonArray
button.Enabled = True
Next
End Sub
Looks like you're incrementing CountTotal twice when it's a Child, once then the age is entered and again when a gender is chosen.
Try this:
Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
Age = txtAge.Text
If Age <= 17 Then
ChildCount += 1
'CountTotal += 1 <= remove this
lblChildCount.Text = ChildCount
'lblCountTotal.Text = CountTotal <= remove this
End If
'messagebox here because you always ask for gender no matter the age
MessageBox.Show("Now please select the appropriate gender.")
txtAge.Enabled = False
Dim buttonArray = {btnMale, btnFemale}
For Each button In buttonArray
button.Enabled = True
Next
End Sub

VB.Net - Noncooperative decimal in string

I am writing a calculator WinForm in VB. I have a label used to display the output "Result". My problem comes when trying to add a "." to my label string. Example: I will type 355.5 and until the 5 is pressed after it, my string is showing up as .355 After the last 5 is pressed, it jumps into the correct location. I have exhausted my debugging skill and now am just going crazy. Anyone encounter this before?
Here's my entire code so far (ignore unfinished functions)
Public Class MyCalc
Private bDecFlag As Boolean
Private sMathOp As String
Private Sub ModeSel_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ModeSel.SelectedIndexChanged
If ModeSel.SelectedIndex = 3 Then
Me.Width = 360
ElseIf ModeSel.SelectedIndex > 2 Then
Me.Width = 590
Else
Me.Width = 250
End If
If ModeSel.SelectedIndex = 0 Then
For iCount As Integer = 0 To 9
Me.Controls("Digit" & iCount).Enabled = True
Next
End If
If ModeSel.SelectedIndex = 1 Then
For iCount As Integer = 2 To 9
Me.Controls("Digit" & iCount).Enabled = False
Next
End If
If ModeSel.SelectedIndex = 2 Then
For iCount As Integer = 0 To 7
Me.Controls("Digit" & iCount).Enabled = True
Next
Digit8.Enabled = False
Digit9.Enabled = False
End If
If ModeSel.SelectedIndex = 3 Then
For iCount As Integer = 0 To 9
Me.Controls("Digit" & iCount).Enabled = True
Next
For iCount As Integer = Asc("A") To Asc("F")
Me.Controls("Alpha" & Chr(iCount)).Enabled = True
Next
For iCount As Integer = Asc("G") To Asc("Z")
Me.Controls("Alpha" & Chr(iCount)).Enabled = False
Next
End If
If ModeSel.SelectedIndex = 4 Then
For iCount As Integer = 0 To 9
Me.Controls("Digit" & iCount).Enabled = True
Next
For iCount As Integer = Asc("A") To Asc("Z")
Me.Controls("Alpha" & Chr(iCount)).Enabled = True
Next
AlphaA.Enabled = False
AlphaE.Enabled = False
AlphaI.Enabled = False
AlphaO.Enabled = False
AlphaU.Enabled = False
End If
End Sub
Private Sub Digit0_Click(sender As System.Object, e As System.EventArgs) Handles Digit0.Click
UpdateResult(0)
End Sub
Private Sub Digit1_Click(sender As System.Object, e As System.EventArgs) Handles Digit1.Click
UpdateResult(1)
End Sub
Private Sub Digit2_Click(sender As System.Object, e As System.EventArgs) Handles Digit2.Click
UpdateResult(2)
End Sub
Private Sub Digit3_Click(sender As System.Object, e As System.EventArgs) Handles Digit3.Click
UpdateResult(3)
End Sub
Private Sub Digit4_Click(sender As System.Object, e As System.EventArgs) Handles Digit4.Click
UpdateResult(4)
End Sub
Private Sub Digit5_Click(sender As System.Object, e As System.EventArgs) Handles Digit5.Click
UpdateResult(5)
End Sub
Private Sub Digit6_Click(sender As System.Object, e As System.EventArgs) Handles Digit6.Click
UpdateResult(6)
End Sub
Private Sub Digit7_Click(sender As System.Object, e As System.EventArgs) Handles Digit7.Click
UpdateResult(7)
End Sub
Private Sub Digit8_Click(sender As System.Object, e As System.EventArgs) Handles Digit8.Click
UpdateResult(8)
End Sub
Private Sub Digit9_Click(sender As System.Object, e As System.EventArgs) Handles Digit9.Click
UpdateResult(9)
End Sub
Private Sub DecBut_Click(sender As System.Object, e As System.EventArgs) Handles DecBut.Click
If bDecFlag = False Then
If Result.Text = "0" Then
Result.Text = "0."
bDecFlag = True
Else
Result.Text = Result.Text + "."
bDecFlag = True
End If
End If
End Sub
Private Sub ClrBut_Click(sender As System.Object, e As System.EventArgs) Handles ClrBut.Click
Result.Text = 0
bDecFlag = False
End Sub
Private Sub DelBut_Click(sender As System.Object, e As System.EventArgs) Handles DelBut.Click
If Result.Text = "0" Then
ElseIf Result.Text.Substring(Result.Text.Length - 1) = "." Then
Result.Text = Result.Text.Substring(0, Result.Text.Length - 1)
bDecFlag = False
Else
Result.Text = Result.Text.Substring(0, Result.Text.Length - 1)
If Result.Text = "" Then
Result.Text = "0"
End If
End If
End Sub
Private Sub MyCalc_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyCode
Case Keys.NumPad0
Digit0_Click(Digit0, New EventArgs)
Case Keys.NumPad1
Digit1_Click(Digit1, New EventArgs)
Case Keys.NumPad2
Digit2_Click(Digit2, New EventArgs)
Case Keys.NumPad3
Digit3_Click(Digit3, New EventArgs)
Case Keys.NumPad4
Digit4_Click(Digit4, New EventArgs)
Case Keys.NumPad5
Digit5_Click(Digit5, New EventArgs)
Case Keys.NumPad6
Digit6_Click(Digit6, New EventArgs)
Case Keys.NumPad7
Digit7_Click(Digit7, New EventArgs)
Case Keys.NumPad8
Digit8_Click(Digit8, New EventArgs)
Case Keys.NumPad9
Digit9_Click(Digit9, New EventArgs)
Case Keys.Back
DelBut_Click(DelBut, New EventArgs)
Case Keys.Decimal
DecBut_Click(DecBut, New EventArgs)
End Select
End Sub
Private Sub MyCalc_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
bDecFlag = False
End Sub
Public Sub UpdateResult(ByVal sNum As String)
If Result.Text = "0" Then
Result.Text = sNum
Else
Result.Text &= sNum
End If
End Sub
End Class
So after the long debugging, I found out that you are using the Label/Textbox RightToLeft property set to yes. Nothing is wrong with it really, but it can't handle special characters including decimal points, commas, etc. properly. The workaround for this is to reverse what we have expected - to reverse the string concatenation.
Result.Text = "." & Result.Text
Not seeing entire code I'm not exactly sure but I think there might be something wrong with string concatenation.
Try changing into this:
Result.Text = Result.Text & "."

Visual Basic How to compare 3 values in a label?

I have tried every possible combination, but I just can't get it to work right. This game needs to test if all 3 dice are the same, and if so, the point value goes up by one and is displayed in lblPoints.
Here is the specific line of code that I need to change:
If (lblDice.Text = lblDice2.Text) And (lblDice2.Text = lblDice3.Text) Then
Points += 1
End If
Below is the whole game if that helps:
Dim randomObject As New Random()
Dim n, m, o, p As Integer
Dim RollNumber = 1
Dim TurnNumber As Integer
Dim Points As Integer
Private Sub btnRoll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRoll.Click
lblPoints.Text = Points
If (lblDice.Text = lblDice2.Text) And (lblDice2.Text = lblDice3.Text) Then
Points += 1
End If
If btnHold.Enabled = True Then
Timer1.Enabled = True
End If
If btnHold2.Enabled = True Then
Timer2.Enabled = True
End If
If btnHold3.Enabled = True Then
Timer3.Enabled = True
End If
TurnNumber += 1
lblTurns.Text = TurnNumber
If TurnNumber = 3 Then
RollNumber += 1
TurnNumber = 0
End If
lblRolls.Text = RollNumber
If RollNumber = 5 Then
btnRoll.Enabled = False
If Points = 0 Or 1 Then
lblPoints2.Text = "You have done poorly"
ElseIf Points = 2 Or 3 Then
lblPoints2.Text = "Mediocre at best"
ElseIf Points = 4 Then
lblPoints2.Text = "Almost perfect"
ElseIf Points = 5 Then
lblPoints2.Text = "Almost perfect"
picJackpot.Visible = True
End If
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
m = m + 1
If m < 10 Then
n = randomObject.Next(1, 7)
lblDice.Text = n
die1PictureBox.Image = ImageList1.Images(n - 1)
Else
Timer1.Enabled = False
m = 0
End If
End Sub
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
m = m + 1
If m < 10 Then
n = randomObject.Next(1, 7)
lblDice2.Text = n
die2PictureBox.Image = ImageList1.Images(n - 1)
Else
Timer2.Enabled = False
m = 0
End If
End Sub
Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
m = m + 1
If m < 10 Then
n = randomObject.Next(1, 7)
lblDice3.Text = n
die3PictureBox.Image = ImageList1.Images(n - 1)
Else
Timer3.Enabled = False
m = 0
End If
End Sub
Private Sub btnHold_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHold.Click
btnHold.Enabled = False
btnUnhold.Enabled = True
End Sub
Private Sub btnUnhold_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUnhold.Click
btnHold.Enabled = True
btnUnhold.Enabled = False
End Sub
Private Sub btnHold2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHold2.Click
btnHold2.Enabled = False
btnUnhold2.Enabled = True
End Sub
Private Sub btnUnhold2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUnhold2.Click
btnHold2.Enabled = True
btnUnhold2.Enabled = False
End Sub
Private Sub btnHold3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHold3.Click
btnHold3.Enabled = False
btnUnhold3.Enabled = True
End Sub
Private Sub btnUnhold3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUnhold3.Click
btnHold3.Enabled = True
btnUnhold3.Enabled = False
End Sub
Private Sub btnRestart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRestart.Click
Dim msg As String
Dim title As String
Dim style As MsgBoxStyle
Dim response As MsgBoxResult
msg = "Are you sure you want to start a new game? If not, you can select 'No' to continue with your current game."
style = MsgBoxStyle.DefaultButton2 Or _
MsgBoxStyle.Critical Or MsgBoxStyle.YesNo
title = "New Game"
response = MsgBox(msg, style, title)
If response = MsgBoxResult.Yes Then
Reset()
End If
End Sub
Private Sub ColorChangeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ColorChangeToolStripMenuItem.Click
'Notice the ... in the menu...that means a dialog box will come
Dim dialog As New ColorDialog ' Color Dialog
Dim result As DialogResult ' stores Button clicked
dialog.FullOpen = True ' show all colors
result = dialog.ShowDialog
' do nothing if user clicked dialog's Cancel Button
If result <> Windows.Forms.DialogResult.Cancel Then
' assign new color to Paint object
Label1.ForeColor = dialog.Color
End If
End Sub
Private Sub FontChangeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FontChangeToolStripMenuItem.Click
'Notice the ... in the menu...that means a dialog box will come
Dim dialog As New FontDialog ' Font Dialog
Dim result As DialogResult ' stores Button clicked
' show dialog and get result
result = dialog.ShowDialog()
' do nothing if user clicked dialog's Cancel Button
If result <> Windows.Forms.DialogResult.Cancel Then
' assign new font value to TextBox
If Windows.Forms.DialogResult.OK = MessageBox.Show("Changing Font. Click Ok to Change,\n Cancel for no change to occur.", _
"Warning", MessageBoxButtons.OKCancel) Then
Label1.Font = dialog.Font
End If
End If
End Sub
Private Sub SelectAColorToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SelectAColorToolStripMenuItem.Click
Dim dialog As New ColorDialog ' Color Dialog
Dim result As DialogResult ' stores Button clicked
dialog.FullOpen = True ' show all colors
result = dialog.ShowDialog()
' do nothing if user clicked dialog's Cancel Button
If result <> Windows.Forms.DialogResult.Cancel Then
Me.BackColor = dialog.Color
End If
End Sub
Private Sub SelectAnImageToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SelectAnImageToolStripMenuItem.Click
Dim msg As String
Dim title As String
Dim style As MsgBoxStyle
Dim response As MsgBoxResult
msg = "Are you sure you want to see what happens when you get a perfect score without actually playing? That's cheating.. and will reset the game!"
style = MsgBoxStyle.DefaultButton2 Or _
MsgBoxStyle.Critical Or MsgBoxStyle.YesNo
title = "New Game"
response = MsgBox(msg, style, title)
If response = MsgBoxResult.Yes Then
Dim FileChooser As New OpenFileDialog()
Dim result As DialogResult
result = DialogResult = FileChooser.ShowDialog()
If result <> Windows.Forms.DialogResult.Cancel Then
If FileChooser.FileName <> "" Then
Me.BackgroundImage = System.Drawing.Image.FromFile(FileChooser.FileName)
Reset()
Else
MessageBox.Show("No Change Made. File Not Selected!", "Warning", _
System.Windows.Forms.MessageBoxButtons.OK, _
System.Windows.Forms.MessageBoxIcon.Exclamation)
End If
End If
End If
End Sub
Private Sub btnHow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHow.Click
MessageBox.Show(" You will have 5 rolls. In each roll you have up to 3 turns. Your object in each of the turns is to get three of a kind on the dice. You can hold some die and roll only some of the die, or you can reroll all the dice. If in a turn you get three of a kind, you get a point. At the end of 5 turns you could have a maximum of 5 points and you will get a special prize.")
End Sub
Sub Reset()
RollNumber = 1
lblRolls.Text = RollNumber
TurnNumber = 0
lblTurns.Text = TurnNumber
Points = 0
lblPoints.Text = Points
lblPoints2.Text = ""
btnRoll.Enabled = True
btnHold.Enabled = True
btnHold2.Enabled = True
btnHold3.Enabled = True
btnUnhold.Enabled = False
btnUnhold2.Enabled = False
btnUnhold3.Enabled = False
die1PictureBox.Image = Nothing
die2PictureBox.Image = Nothing
die3PictureBox.Image = Nothing
lblDice.Text = ""
lblDice2.Text = ""
lblDice3.Text = ""
End Sub
End Class
What is happening is you are getting a valid compare on "" after you run your Reset Subroutine and when you start your program, because your dice are initialized to the same value. Try using a Boolean value to represent a fresh game, that way when your dice are starting from scratch you will not get a match. Something like this.
add a Boolean variable to your declarations
Dim Restart as Boolean = True
In your BtnRoll_Click EventHandler
If Not restart Then
If (lblDice.Text = lblDice2.Text) And (lblDice2.Text = lblDice3.Text) Then
Points += 1
End If
End If
Restart = False
and add this to the end of your Reset Subroutine.
Restart = True
Try
If String.Compare(lblDice.Text, lblDice2.Text) = 0 And String.Compare(lblDice2.Text, lblDice3.Text) = 0 Then
Points += 1
Else
tente (val(text.text)) then
exemplo if text1 = (val(text2.text)) then
text1 =0
end if

Visual Basic : Is there a WAY to shorten this raw codes

I designed some software to help sellers for calculating the products bought by the costumer.
I coded it too raw, I know this code can be shortened but I don't know what to do.
And this is my code:
Public Class Form1
Private Strg1, Strg2, Strg3, Strg4, Strg5, Strg6, Strg7, Strg8, Strg9, Strg10, Strg11, Strg12, Strg13, Strg14, Strg15, Strg16, Strg17, Strg18, Strg19, Strg20, Strg21, Strg22, Strg23, Strg24, Strg25, Strg26, Strg27, Strg28, Strg29, Strg30 As Double
Private p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17, p18, p19, p20, p21, p22, p23, p24, p25, p26, p27, p28, p29, p30, MedPrc1, MedPrc2, TotMedPrc As Double
Private Function QntCalc(ByVal x As Double, ByVal y As Double) As Double
Return x * y
End Function
Private Sub Form1_Load(ByVal sder As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MedPrc1 = 0
MedPrc2 = 0
TotMedPrc = 0
End Sub
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
If TextBox1.Enabled = False Then
TextBox1.Enabled = True
p1 = Val(Label1.Text)
Else
TextBox1.Enabled = False
TextBox1.Text = ""
p1 = 0
End If
End Sub
Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
If TextBox2.Enabled = False Then
TextBox2.Enabled = True
p2 = Val(Label2.Text)
Else
TextBox2.Enabled = False
TextBox2.Text = ""
p2 = 0
End If
End Sub
Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged
If TextBox3.Enabled = False Then
TextBox3.Enabled = True
p3 = Val(Label3.Text)
Else
TextBox3.Enabled = False
TextBox3.Text = ""
p3 = 0
End If
End Sub
Private Sub CheckBox4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox4.CheckedChanged
If TextBox4.Enabled = False Then
TextBox4.Enabled = True
p4 = Val(Label4.Text)
Else
TextBox4.Enabled = False
TextBox4.Text = ""
p4 = 0
End If
End Sub
Private Sub CheckBox5_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox5.CheckedChanged
If TextBox5.Enabled = False Then
TextBox5.Enabled = True
p5 = Val(Label5.Text)
Else
TextBox5.Enabled = False
TextBox5.Text = ""
p5 = 0
End If
End Sub
Private Sub CheckBox6_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox6.CheckedChanged
If TextBox6.Enabled = False Then
TextBox6.Enabled = True
p6 = Val(Label6.Text)
Else
TextBox6.Enabled = False
TextBox6.Text = ""
p6 = 0
End If
End Sub
Private Sub CheckBox7_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox7.CheckedChanged
If TextBox7.Enabled = False Then
TextBox7.Enabled = True
p7 = Val(Label7.Text)
Else
TextBox7.Enabled = False
TextBox7.Text = ""
p7 = 0
End If
End Sub
Private Sub CheckBox8_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox8.CheckedChanged
If TextBox8.Enabled = False Then
TextBox8.Enabled = True
p8 = Val(Label8.Text)
Else
TextBox8.Enabled = False
TextBox8.Text = ""
p8 = 0
End If
End Sub
Private Sub CheckBox9_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox9.CheckedChanged
If TextBox9.Enabled = False Then
TextBox9.Enabled = True
p9 = Val(Label9.Text)
Else
TextBox9.Enabled = False
TextBox9.Text = ""
p9 = 0
End If
End Sub
Private Sub CheckBox10_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox10.CheckedChanged
If TextBox10.Enabled = False Then
TextBox10.Enabled = True
p10 = Val(Label10.Text)
Else
TextBox10.Enabled = False
TextBox10.Text = ""
p10 = 0
End If
End Sub
Private Sub CheckBox11_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox11.CheckedChanged
If TextBox11.Enabled = False Then
TextBox11.Enabled = True
Else
TextBox11.Enabled = False
TextBox11.Text = ""
p1 = 0
End If
End Sub
Private Sub CheckBox12_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox12.CheckedChanged
If TextBox12.Enabled = False Then
TextBox12.Enabled = True
Else
TextBox12.Enabled = False
TextBox12.Text = ""
End If
End Sub
Private Sub CheckBox13_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox13.CheckedChanged
If TextBox13.Enabled = False Then
TextBox13.Enabled = True
Else
TextBox13.Enabled = False
TextBox13.Text = ""
p1 = 0
End If
End Sub
Private Sub CheckBox14_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox14.CheckedChanged
If TextBox14.Enabled = False Then
TextBox14.Enabled = True
Else
TextBox14.Enabled = False
TextBox14.Text = ""
End If
End Sub
Private Sub CheckBox15_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox15.CheckedChanged
If TextBox15.Enabled = False Then
TextBox15.Enabled = True
Else
TextBox15.Enabled = False
TextBox15.Text = ""
End If
End Sub
Private Sub CheckBox16_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox16.CheckedChanged
If TextBox16.Enabled = False Then
TextBox16.Enabled = True
Else
TextBox16.Enabled = False
TextBox16.Text = ""
End If
End Sub
Private Sub CheckBox17_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox17.CheckedChanged
If TextBox17.Enabled = False Then
TextBox17.Enabled = True
Else
TextBox17.Enabled = False
TextBox17.Text = ""
End If
End Sub
Private Sub CheckBox18_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox18.CheckedChanged
If TextBox18.Enabled = False Then
TextBox18.Enabled = True
Else
TextBox18.Enabled = False
TextBox18.Text = ""
End If
End Sub
Private Sub CheckBox19_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox19.CheckedChanged
If TextBox19.Enabled = False Then
TextBox19.Enabled = True
Else
TextBox19.Enabled = False
TextBox19.Text = ""
End If
End Sub
Private Sub CheckBox20_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox20.CheckedChanged
If TextBox20.Enabled = False Then
TextBox20.Enabled = True
Else
TextBox20.Enabled = False
TextBox20.Text = ""
End If
End Sub
Private Sub CheckBox21_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox21.CheckedChanged
If TextBox21.Enabled = False Then
TextBox21.Enabled = True
Else
TextBox21.Enabled = False
TextBox21.Text = ""
End If
End Sub
Private Sub CheckBox22_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox22.CheckedChanged
If TextBox22.Enabled = False Then
TextBox22.Enabled = True
Else
TextBox22.Enabled = False
TextBox22.Text = ""
End If
End Sub
Private Sub CheckBox23_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox23.CheckedChanged
If TextBox23.Enabled = False Then
TextBox23.Enabled = True
Else
TextBox23.Enabled = False
TextBox23.Text = ""
End If
End Sub
Private Sub CheckBox24_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox24.CheckedChanged
If TextBox24.Enabled = False Then
TextBox24.Enabled = True
Else
TextBox24.Enabled = False
TextBox24.Text = ""
End If
End Sub
Private Sub CheckBox25_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox25.CheckedChanged
If TextBox25.Enabled = False Then
TextBox25.Enabled = True
Else
TextBox25.Enabled = False
TextBox25.Text = ""
End If
End Sub
Private Sub CheckBox26_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox26.CheckedChanged
If TextBox26.Enabled = False Then
TextBox26.Enabled = True
Else
TextBox26.Enabled = False
TextBox26.Text = ""
End If
End Sub
Private Sub CheckBox27_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox27.CheckedChanged
If TextBox27.Enabled = False Then
TextBox27.Enabled = True
Else
TextBox27.Enabled = False
TextBox27.Text = ""
End If
End Sub
Private Sub CheckBox28_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox28.CheckedChanged
If TextBox28.Enabled = False Then
TextBox28.Enabled = True
Else
TextBox28.Enabled = False
TextBox28.Text = ""
End If
End Sub
Private Sub CheckBox29_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox29.CheckedChanged
If TextBox29.Enabled = False Then
TextBox29.Enabled = True
Else
TextBox29.Enabled = False
TextBox29.Text = ""
End If
End Sub
Private Sub CheckBox30_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox30.CheckedChanged
If TextBox30.Enabled = False Then
TextBox30.Enabled = True
Else
TextBox30.Enabled = False
TextBox30.Text = ""
End If
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Strg1 = Str(QntCalc(p1, Val(TextBox1.Text)))
End Sub
Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
Strg2 = Str(QntCalc(p2, Val(TextBox2.Text)))
End Sub
Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged
Strg3 = Str(QntCalc(p3, Val(TextBox3.Text)))
End Sub
Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox4.TextChanged
Strg4 = Str(QntCalc(p4, Val(TextBox4.Text)))
End Sub
Private Sub TextBox5_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox5.TextChanged
Strg5 = Str(QntCalc(p5, Val(TextBox5.Text)))
End Sub
Private Sub TextBox6_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox6.TextChanged
Strg6 = Str(QntCalc(p6, Val(TextBox6.Text)))
End Sub
Private Sub TextBox7_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox7.TextChanged
Strg7 = Str(QntCalc(p7, Val(TextBox7.Text)))
End Sub
Private Sub TextBox8_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox8.TextChanged
Strg8 = Str(QntCalc(p8, Val(TextBox8.Text)))
End Sub
Private Sub TextBox9_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox9.TextChanged
Strg9 = Str(QntCalc(p9, Val(TextBox9.Text)))
End Sub
Private Sub TextBox10_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox10.TextChanged
Strg10 = Str(QntCalc(p10, Val(TextBox10.Text)))
End Sub
Private Sub MedPrc_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MedPrc.TextChanged
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TotMedPrc = Strg1 + Strg2 + Strg3 + Strg4 + Strg5 + Strg6 + Strg7 + Strg8 + Strg9 + Strg10
MedPrc.Text = Val(TotMedPrc)
End Sub
End Class
Is there a way to shorten this?
Your code looks like VB.NET right? In VBA you would write something little bit different. Sample:
Option Explicit
Private Strg1 As String
Private p1 As Double
Private Sub CheckBox1_Change()
OnCheckBoxChange Me.TextBox1, Me.Label1, p1
End Sub
Private Sub TextBox1_Change()
OnTextBoxChanged Me.TextBox1, p1, Strg1
End Sub
Private Sub OnTextBoxChanged(ByRef textBoxControl As Variant, ByRef p As Double, Strg As String)
Strg = Str(QntCalc(p, Val(textBoxControl.Text)))
End Sub
Private Sub OnCheckBoxChange(ByRef textBoxControl As Variant, ByRef labelControl As Variant, ByRef p As Double)
If textBoxControl.Enabled = False Then
textBoxControl.Enabled = True
p = Val(labelControl.Caption)
Else
textBoxControl.Enabled = False
textBoxControl.Text = ""
p = 0
End If
End Sub
Private Function QntCalc(ByVal x As Double, ByVal y As Double) As Double
QntCalc = x * y
End Function