Equals Operator with Listbox & Math not working properly - vb.net

I'm finished with my homework my teacher asked for. The math isn't calculating out to what it should be. The math is working corectly for Handling Stress, Time management, and supervision skills. When I get to Negotiation, and How to interview. I am getting random numbers.
How to interview(395) + Dallas(110) = 505
But I am getting 725. I think it's doing 395 + (110x3).
Public Class frmMain
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim Workshops As New ArrayList()
Workshops.Add(New Workshop("Handling Stress", "595"))
Workshops.Add(New Workshop("Time Management", "695"))
Workshops.Add(New Workshop("Supervision Skills", "995"))
Workshops.Add(New Workshop("Negotiation", "1295"))
Workshops.Add(New Workshop("How to Interview", "395"))
lbWorkshop.DataSource = Workshops
lbWorkshop.DisplayMember = "Workshop"
lbWorkshop.ValueMember = "Price"
lbWorkshop.ClearSelected()
Dim Location As New ArrayList()
Location.Add(New Workshop("Austin", "95"))
Location.Add(New Workshop("Chicago", "125"))
Location.Add(New Workshop("Dallas", "110"))
Location.Add(New Workshop("Orlando", "100"))
Location.Add(New Workshop("Phoenix", "92"))
Location.Add(New Workshop("Raleigh", "90"))
lbLocation.DataSource = Location
lbLocation.DisplayMember = "Workshop"
lbLocation.ValueMember = "Price"
lbLocation.ClearSelected()
End Sub
Private Sub lblAdd_Click(sender As Object, e As EventArgs) Handles lblAdd.Click
Dim description As String
Dim value As Integer
If (lbWorkshop.SelectedIndex > -1) Then
If (lbLocation.SelectedIndex > -1) Then
lblStatus1.Text = String.Empty
If (lbWorkshop.SelectedItem.Equals("How to Interview")) Then
value = lbWorkshop.SelectedValue + lbLocation.SelectedValue
ElseIf (lbWorkshop.SelectedItem.Equals("Negotiation")) Then
value = lbWorkshop.SelectedValue + (lbLocation.SelectedValue * 5)
Else
value = lbWorkshop.SelectedValue + (lbLocation.SelectedValue * 3)
End If
description = value.ToString
lbTotal.Items.Add(description)
lbTotal.ValueMember = value
Dim total As Integer
For Each Str As String In lbTotal.Items
total = total + CInt(Str)
Next
lblTotal.Text = FormatCurrency(total.ToString)
Else
lblStatus1.Text = "Please Select a Location"
End If
Else
lblStatus1.Text = "Please Select a Workshop"
End If
End Sub

it's doing what you asked, as far as I can tell. Is this your intention?
Else 'Supervision Skills, Negotiation, How to Interview
value = lbWorkshop.SelectedValue + (lbLocation.SelectedValue * 3)

Related

If I click the calculate button the program crashes?

Just for context;
I need to calculate the average of 5 numbers located in 5 textboxes.
Nummer means number
Gemiddelde means average
and
Bereken means calculate
What is causing it to crash?
Private Sub butBereken_Click(sender As Object, e As EventArgs) Handles butBereken.Click
'Variabelen'
Dim nummer1 As Decimal = txtNummer1.Text
Dim nummer2 As Decimal = txtNummer2.Text
Dim nummer3 As Decimal = txtNummer3.Text
Dim nummer4 As Decimal = txtNummer4.Text
Dim nummer5 As Decimal = txtNummer5.Text
Dim somNummers As Decimal = nummer1 + nummer2 + nummer3 + nummer4 + nummer5
Dim Gemiddelde As String = (somNummers) / 5
lblGemiddelde.Text = Gemiddelde
If Gemiddelde < 5.5 Then
lblGemiddelde.Text = Gemiddelde + " Dit is onvoldoende"
End If
If nummer1 = "" Or nummer2 = "" Or nummer3 = "" Or
nummer4 = "" Or nummer5 = "" Then
butBereken.Enabled = False
MessageBox.Show("your mom")
Else
butBereken.Enabled = True
End If
End Sub
I don't see what would crash the program but check to that the TextBoxes have values before assigning them to numeric variables. A Decimal value will never = "".
Private Sub butBereken_Click(sender As Object, e As EventArgs) Handles butBereken.Click 'Variabelen'
If Not IsNumeric(txtNummer1.Text) Or _
Not IsNumeric(txtNummer2.Text) Or _
Not IsNumeric(txtNummer3.Text) Or _
Not IsNumeric(txtNummer4.Text) Or _
Not IsNumeric(txtNummer5.Text) Then
MessageBox.Show ("your mom wants you to fill in all the number boxes")
Exit Sub
End If
Dim nummer1 As Decimal = CDec(txtNummer1.Text)
Dim nummer2 As Decimal = CDec(txtNummer2.Text)
Dim nummer3 As Decimal = CDec(txtNummer3.Text)
Dim nummer4 As Decimal = CDec(txtNummer4.Text)
Dim nummer5 As Decimal = CDec(txtNummer5.Text)
Dim somNummers As Decimal = nummer1 + nummer2 + nummer3 + nummer4 + nummer5
Dim Gemiddelde As String = (somNummers) / 5
lblGemiddelde.Text = Gemiddelde
If Gemiddelde < 5.5 Then
lblGemiddelde.Text = Gemiddelde + "Dit is onvoldoende"
End If
If nummer1 = 0 Or nummer2 = 0 Or nummer3 = 0 Or nummer4 = 0 Or nummer5 = 0 Then
butBereken.Enabled = False
MessageBox.Show ("your mom")
Else
butBereken.Enabled = True
End If
End Sub
If this doesn't work I would consider setting breakpoints in the could to determine what line is causing the crash.
If that doesn't work consider adding this line to the form's initialization:
butBereken.Caption = "Warning: Do not Click!"
Assuming the user populated all the textboxes with numeric only data, (and you have checked this) try replacing these lines in your code with this code
Dim nummer1 As Decimal = txtNummer1.Text
Dim nummer2 As Decimal = txtNummer2.Text
Dim nummer3 As Decimal = txtNummer3.Text
Dim nummer4 As Decimal = txtNummer4.Text
Dim nummer5 As Decimal = txtNummer5.Text
Dim somNummers As Decimal = nummer1 + nummer2 + nummer3 + nummer4 + nummer5
Dim Gemiddelde As Decimal = somNummers / 5
lblGemiddelde.Text = Gemiddelde.ToString("##0.0")
I'd do something more like:
Private Sub butBereken_Click(sender As Object, e As EventArgs) Handles butBereken.Click
Dim TBs() As TextBox = {txtNummer1, txtNummer2, txtNummer3, txtNummer4, txtNummer5}
Dim inputs() As String = TBs.Select(Function(x) x.Text).ToArray()
Dim values() As Decimal
Try
values = Array.ConvertAll(inputs, Function(s) Decimal.Parse(s))
Dim Gemiddelde As String = values.Average()
lblGemiddelde.Text = Gemiddelde & If(Gemiddelde < 5.5, " Dit is onvoldoende", "")
Catch ex As Exception
MessageBox.Show("your mom")
End Try
End Sub
I prefer this approach as it doesn't require repetitive lines of code, manually converting each TextBox to a Decimal. Since the TextBoxes are in an Array, we could also add another TextBox to the form and then add that name to the end of the Array. The rest of the code would not need to change at all; it would still just work as is.
From the Array of TextBox, we use a LINQ statement to extract the Text from each TextBox and add it to an Array of String called "inputs". From there, we convert every single String to a Decimal using Array.ConvertAll(), again avoiding repetitive code. If any of the input values is not a valid Decimal then an Exception will be thrown and we'll jump the the Catch block where the not so well written error message is displayed.
If there are no exceptions, then all String inputs were successfully converted to Decimals and stored in the "values" Array. Next we simply use the LINQ function Average() to get the average of all the values.
Lastly we display the computed average in the Label, adding the "Dit is onvoldoende" message if approapriate. The If() function used in this line is simply a shorthand version of a longer If...Else...End If statement.
In your original attempt, it looks like you wanted to disable the button if any of the values are blank (or maybe if they are invalid decimals?):
If nummer1 = "" Or nummer2 = "" Or nummer3 = "" Or nummer4 = "" Or nummer5 = "" Then
butBereken.Enabled = False
MessageBox.Show("your mom")
Else
butBereken.Enabled = True
End If
This makes no sense as if you disable the button, how would it get turned back on so that user could click it again?
A different approach would be to handle the TextChanged() event of all the TextBoxes and simply update your Label with the average in real-time whenever one of the TextBoxes is changed:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
UpdateAverage()
End Sub
Private Sub txtAll_TextChanged(sender As Object, e As EventArgs) Handles txtNummer5.TextChanged, txtNummer4.TextChanged, txtNummer3.TextChanged, txtNummer2.TextChanged, txtNummer1.TextChanged
UpdateAverage()
End Sub
Private Sub UpdateAverage()
Dim TBs() As TextBox = {txtNummer1, txtNummer2, txtNummer3, txtNummer4, txtNummer5}
Dim inputs() As String = TBs.Select(Function(x) x.Text).ToArray()
Try
Dim values() As Decimal = Array.ConvertAll(inputs, Function(s) Decimal.Parse(s))
Dim average As Decimal = values.Average
lblGemiddelde.Text = average & If(average < 5.5, " Dit is onvoldoende", "")
Catch ex As Exception
lblGemiddelde.Text = "{ Invalid Input }"
End Try
End Sub
End Class
Sample run:

Adding 1 everytime i use button vb.net

Basically, I need 2 make a student number and after every new entry I put the student number should +1 so 20182(or 3 if male)001 will be 20182(or 3 if male)002 after I push the button and it must keep +1 but once it reaches the 10th registered student the format changes to 20182(3 if male)010.
I have done everything but make the number +1 every time I use the button
so basically the answer must be:
Student Number is 20182001
Surname , Name
contact details
but, I done everything besides the 001, 002,003 till 010 part so if anybody could help I would be thankful
Public Class Form1
Public Number As Integer = 2018000
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim strSurname As String
Dim strFullName As String
Dim strContact As String
Dim strGender As String
Dim x As Integer
'IF statement'
If Me.cboGender.SelectedItem = "Female" Then
Number = 2018300
Else
End If
If Me.cboGender.SelectedItem = "Male" Then
Number = 2018200
Else
End If
'Finding The Student Number'
Dim i As Integer = 0
Do While (i < 1)
i = i + 1
Loop
If i = 201820010 Then
Number = 201800
Else
If i = 201830010 Then
Number = 201800
End if
End If
'Add Items To ListBox'
ListBox1.Items.Add("Student number: " & Number & i)
ListBox1.Items.Add(txtSurname.Text & " , " & txtFullName.Text)
ListBox1.Items.Add(txtContact.Text)
ListBox1.Items.Add("============================================")
End Sub
End Class
Not sure what your code was doing, but based on your requirements:
Public baseFemale As Integer = 20182000
Public baseMale As Integer = 20183000
Public autoNumber As Integer = 0
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Number as Integer;
autoNumber = autoNumber + 1
If Me.cboGender.SelectedItem = "Female" Then
Number = baseFemale + autoNumber
Else
Number = baseMale + autoNumber
Else
'Add Items To ListBox'
ListBox1.Items.Add("Student number: " & Number & i)
ListBox1.Items.Add(txtSurname.Text & " , " & txtFullName.Text)
ListBox1.Items.Add(txtContact.Text)
ListBox1.Items.Add("============================================")
End Sub
Additionally you may want to check for autoNumber exceeding 999 - but I leave that as an exercise for you.

how to memorize and randomize the questions in MCQ?

Dears,
I'm looking for assistance in visual basic with respect to multiple choices questions (MCQ).
by using visual basic for Visual Studio 2015
apply the codes without using database:
1- how to Not make any duplications in the questions?(for memorizing purpose... what is wrong and what is right?) E.g. Assuming I open the program and the first question is the word “rich” ,and I chose the correct answer, which is “IT”, I don’t want to see “rich” again until I finish with the whole list. However, if I make the wrong choice for “rich” for anything else e.g. “HR”, I want the word “rich” to appear after a while until I get the question correct. The point here to make the person memorize “rich” is “IT”.
Please write the codes down in your comment (the point you answering)
sorry for asking long question
Thank you
Public Class Form1
Private Structure questionsNanswers
Public Q As String
Public A As String
Public QT As Integer
Public QC As Integer
End Structure
Private wstart As Integer = 0
Private adad As Integer = 10
Private QA(9999) As questionsNanswers
Private word(15) As String
Private aray(15) As Integer
Private Sub RandomizeArray(a As Integer, ByRef array() As Integer)
Dim i As Integer
Dim j As Integer
Dim tmp As Integer
Randomize()
For i = 0 To a - 1
j = Int((6 - i + 1) * Rnd() + i)
tmp = array(i)
array(i) = array(j)
array(j) = tmp
Next
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
' next
CheckEntry()
wstart = wstart + 1
If wstart >= adad Then
wstart = 0
End If
WriteText()
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
' previous
CheckEntry()
wstart = wstart - 1
If wstart < 0 Then
wstart = adad - 1
End If
WriteText()
End Sub
Private Sub CheckEntry()
RadioButton1.Visible = True
RadioButton2.Visible = True
RadioButton3.Visible = True
RadioButton4.Visible = True
RadioButton1.ForeColor = Color.Black
RadioButton2.ForeColor = Color.Black
RadioButton3.ForeColor = Color.Black
RadioButton4.ForeColor = Color.Black
RadioButton1.Checked = False
RadioButton2.Checked = False
RadioButton3.Checked = False
RadioButton4.Checked = False
End Sub
Private Sub WriteText()
Dim out As Boolean = False
For kk = 0 To 6
aray(kk) = kk
Next
RandomizeArray(7, aray)
Do Until out
For j = 0 To 3
If out = False Then
If aray(j) = QA(wstart).QT Then
out = True
Exit Do
End If
End If
Next
For kkk = 0 To 6
aray(kkk) = kkk
Next
RandomizeArray(7, aray)
Loop
RadioButton1.Text = word(aray(0))
RadioButton2.Text = word(aray(1))
RadioButton3.Text = word(aray(2))
RadioButton4.Text = word(aray(3))
Label1.Text = CStr(wstart + 1) & ") " & QA(wstart).Q
' ==============================
Dim go As Boolean = False
If go Then
Dim msg As String
For ll = 0 To 6
msg = msg + CStr(aray(ll)) + "|"
Next
MsgBox(msg)
End If
End Sub
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
word(0) = "TA"
word(1) = "GR"
word(2) = "HR"
word(3) = "FIN"
word(4) = "commercial"
word(5) = "Proc"
word(6) = "IT"
QA(0).Q = "rich"
QA(0).A = word(6)
QA(0).QT = 6
QA(0).QC = -1
QA(1).Q = "Tal"
QA(1).A = word(1)
QA(1).QT = 1
QA(1).QC = -1
QA(2).Q = "sau"
QA(2).A = word(2)
QA(2).QT = 2
QA(2).QC = -1
QA(3).Q = "pat"
QA(3).A = word(3)
QA(3).QT = 3
QA(3).QC = -1
QA(4).Q = "del"
QA(4).A = word(5)
QA(4).QT = 5
QA(4).QC = -1
WriteText()
End Sub
End Class
Procedures :
1•Store the questions in a database(E.g. MySql/MSSQL/EXCEL)
2•In the database, create a table of 6 columns(column 1 for questions,column 2 for mcq option1,column 3 for mcq option 2, column 4 for mcq option 3,column 5 for mcq option 4 and finally column 6 for mcq answer )
3•Add 1 label and 4 Radio buttons
Now all you have to do is follow this and use the code..
That'll do the work

Finding the average of an array after dropping lowest value? (VB)

I'm developing a program that asks the user to input 5 numbers. The lowest of the 5 numbers will be dropped, and then the other 4 numbers are to be averaged.
I'm quite new to VB, but I believe I'm currently on the right path here...
I've sorted the array to help identify the lowest number, but I do not know how to exclude the lowest number and to then average the other remaining 4.
Here is my code so far:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim IntArr(4) As Integer
Dim i As Integer
For i = 1 To 5
IntArr(4) = InputBox("Enter Number" & i)
Next
Array.Sort(IntArr)
'textbox1.text = ???
End Sub
End Class
Can anyone please assist or at least point me in the right direction?
In keeping with the spirit of your code, something like the following would work.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim IntArr(4) As Integer
Dim OutArr(3) As Integer
For i = 0 To 4
IntArr(i) = InputBox("Enter Number " & i)
Next
Array.Sort(IntArr)
Array.Copy(IntArr, 1, OutArr, 0, 4) 'exclude the lowest number
TextBox1.Text = OutArr.Average()
End Sub
End Class
Using built in LINQ functions
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim numberOfItems = 5
Dim numberOfItemsToRemove = 1
Dim inArray(numberOfItems - 1) As Integer
Dim outArray(numberOfItems - 1 - numberOfItemsToRemove) As Integer
Dim i As Integer
For i = 0 To numberOfItems - 1
While Not Integer.TryParse(InputBox("Enter Number " & i + 1), inArray(i))
MessageBox.Show("Invalid input!")
End While
Next
outArray = inArray.OrderBy(Function(j) j).Skip(numberOfItemsToRemove).ToArray()
MessageBox.Show(
String.Format(
"Input: [{0}], Output: [{1}], average: {2:0.0}",
String.Join(", ", inArray),
String.Join(", ", outArray),
outArray.Average))
End Sub
Your code as it is above will continue to simply change the value of index 4 with each box if I'm not mistaken. I would do something like this (I will use your variable names for your convenience).
Button1_Click(procedure junk that is auto-inserted)
Dim intArr(4) As Integer
Dim OutArr(3) As Integer
Dim intCounter, intAverage, intLowest, intLowIndex As Integer
'populate all indexes of intArr()
For intCounter = 0 to 4
intArr(intCounter) = InputBox("Please enter a number.")
Next intCounter
intCounter = 1 'reset counter for check
intLowest = intArr(intLowIndex) 'start with index 0
'find lowest number and its index
For intCounter = 1 to 4
If intLowest > intArr(intCounter) Then 'defaults to previous
intLowest = intArr(intCounter)
intLowIndex = intCounter
End If
Next intCounter
intCounter = 0 'reset counter again for possible For...Next loops
Select Case intLowIndex
Case = 0
For intCounter = 0 to 3
OutArr(intCounter) = intArr(intCounter + 1)
Next intCounter
Case = 1
OutArr(0) = intArr(0)
OutArr(1) = intArr(2)
OutArr(2) = intArr(3)
OutArr(3) = intArr(4)
Case = 2
OutArr(0) = intArr(0)
OutArr(1) = intArr(1)
OutArr(2) = intArr(3)
OutArr(3) = intArr(4)
Case = 3
OutArr(0) = intArr(0)
OutArr(1) = intArr(1)
OutArr(2) = intArr(2)
OutArr(3) = intArr(4)
Case = 4
For intCounter = 0 to 3
OutArr(intCounter) = intArr(intCounter)
Next intCounter
End Select
intAverage = (OutArr(0) + OutArr(1) + OutArr(2) + OutArr(3)) / 4
'insert your preferred method to display OutArr() and intAverage

GPA Calculation

I'm having trouble finding out what the proper code would be to calculate the GPA. Everything I try ends up in the wrong GPA. Any help would be greatly appreciated I'm still a beginner at visual basic but this is the best I could do.
Option Explicit On
Option Strict On
Option Infer Off
Public Class mainForm
Private Sub exitButton_Click(sender As Object, e As EventArgs) Handles exitButton.Click
Me.Close()
End Sub
Private Sub dataButton_Click(sender As Object, e As EventArgs) Handles dataButton.Click
Const Prompt As String = "Enter number of Credit Hours:"
Const Title As String = "Credit Hours"
Const Prompt2 As String = "Enter grade:"
Const Title2 As String = "Grades"
Dim inputCredit As String
Dim inputGrades As String
Dim creditHours As Integer
Dim grades As Char
Dim gradesCounter As Integer
Dim point As Integer
Dim gpaTotal As Double
Dim creditHoursAccumulator As Integer
Dim pointAccumulator As Integer
inputCredit = InputBox(Prompt, Title)
inputGrades = InputBox(Prompt2, Title2)
Do While inputCredit <> String.Empty
Integer.TryParse(inputCredit, creditHours)
Char.TryParse(inputGrades, grades)
Select Case grades
Case CChar("A")
point = 4
Case CChar("B")
point = 3
Case CChar("C")
point = 2
Case CChar("D")
point = 1
Case CChar("F")
point = 0
End Select
pointAccumulator += 1
gradesCounter += 1
creditHoursAccumulator += creditHours
inputCredit = InputBox(Prompt, Title)
inputGrades = InputBox(Prompt2, Title2)
Loop
gpaTotal = pointAccumulator / creditHoursAccumulator
totalCreditsLabel.Text = "Total credit hours:" & creditHoursAccumulator
gpaLabel.Text = "GPA:" & gpaTotal
totalGradesLabel.Text = "Number of grades entered:" & gradesCounter
End Sub
End Class
Shouldn't line:
pointAccumulator += 1
be:
pointAccumulator += point