I have VB program which user is to enter grades into using an InputBox. Regardless of user input, a message box (msgbox) stating "Please enter a number" appears. How do I change this to only show this message if a number is not entered?
Option Strict Off
Public Class Form1
Dim totalpointsaccumultator As Object
Private Sub exitButton_Click(sender As Object, e As EventArgs) Handles exitButton.Click
Me.Close()
End Sub
Public Sub assignButton_Click(sender As Object, e As EventArgs) Handles assignButton.Click
Dim inputProjectPoints, inputTestPoints As String
Dim grade, projectpoints, testpoints As String
Dim projectcounter As Integer = 1
Dim testcounter As Integer = 1
Dim isconverted As Boolean
Dim totalpointsaccumulator As Integer
Do While projectcounter < 5
inputProjectPoints = InputBox("Enter the points earned on project " & projectcounter, "Grade Calculator", "0")
inputProjectPoints = projectpoints
isconverted = Integer.TryParse(inputProjectPoints, CInt(projectpoints))
If isconverted Then
totalpointsaccumultator = totalpointsaccumulator + projectpoints
projectcounter = projectcounter + 1
Else
MessageBox.Show("Please enter a number.", "Grade Calculator", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
Loop
Do While testcounter < 3
inputTestPoints = InputBox("Enter the points earned on test " & testcounter, "Grade Calculator", "0")
isconverted = Integer.TryParse(inputTestPoints, testpoints)
If isconverted Then
testcounter = testcounter + 1
totalpointsaccumulator = CInt(totalpointsaccumulator + testpoints)
Else
MessageBox.Show("Please enter a number.", "Grade calculator", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
Loop
' assign grade
Select Case totalpointsaccumulator
Case Is >= 360
grade = "A"
Case Is >= 320
grade = "B"
Case Is >= 280
grade = "C"
Case Is >= 240
grade = "D"
Case Else
grade = "F"
End Select
totalpointsLabel.Text = Convert.ToString(totalpointsaccumulator)
gradeLabel.Text = grade
End Sub
End Class
isconverted = Integer.TryParse(inputProjectPoints, CInt(projectpoints))
should be:
isconverted = Integer.TryParse(inputProjectPoints, projectpoints)
as well as:
Dim grade, projectpoints, testpoints As String
Should be:
Dim grade as String
Dim projectpoints, testpoints As Integer
You can't pass a reference variable as a different type by trying to convert/cast it, that just returns the value in the type you requested (if possible, which is ironic considering you're use of Integer.TryParse()) it doesn't actually change the underlying type of the variable you declared.
Because of this issue, your Integer.TryParse() always fails, meaning isconverted is always false and you'll always get the message box.
Edit: Forgot to add, Plutonix is right. Set Option Strict ON!!
Related
I have a code to color the cells in a datagridview based on defined criteria for several different pollutants, and it works well. However, there will often be occurrences of the character '<' in cases like "<0.005", meaning "below detection limit", and that crashes the routine with the message "Operator '<' is not defined for type 'DBNull' and type 'Double'."
Edit: This is the latest code as supplied by JohnG. I still get error messages when the subs encounter empty cells or invalid characters
Imports System.Data.SqlClient
Imports System.IO
Imports Microsoft.Office.Interop.Excel
Imports Microsoft.Office.Interop
Imports System.Runtime.InteropServices
Imports System.Text.RegularExpressions
Public Class Form1
Public Property gridResults As Object
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
OpenFileDialog2.Title = "Velg fil ..."
OpenFileDialog2.InitialDirectory = "C:users\<currentuser>\Documents"
OpenFileDialog2.Filter = "Alle filer|*.*|Excel 2003|*.xls|Excel|*.xlsx"
OpenFileDialog2.FilterIndex = 2
OpenFileDialog2.ShowDialog()
End Sub
Private Sub OpenFileDialog2_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog2.FileOk
Dim strm As System.IO.Stream
strm = OpenFileDialog2.OpenFile()
TextBox2.Text = OpenFileDialog2.FileName.ToString()
If Not (strm Is Nothing) Then
strm.Close()
End If
Me.Button5_Click(sender, e)
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
If String.IsNullOrEmpty(TextBox2.Text) Then
MessageBox.Show("Klikk ""Bla gjennom"" for å velge en fil", "Ingen inndatafil")
Exit Sub
End If
Dim FilePath As String = OpenFileDialog2.FileName
Dim MyConnection As System.Data.OleDb.OleDbConnection
Dim DtSet As System.Data.DataSet
Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & FilePath & ";Extended Properties=Excel 8.0;")
MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection)
MyCommand.TableMappings.Add("Table", "Net-informations.com")
DtSet = New System.Data.DataSet
MyCommand.Fill(DtSet)
DataGridView2.DataSource = DtSet.Tables(0)
MyConnection.Close()
End Sub
Public Function GetElementColorsValues(elementName As String) As Decimal()
Dim ULArray(4) As Decimal
Select Case elementName
Case "As (Arsen)"
ULArray(0) = 8
ULArray(1) = 20
ULArray(2) = 50
ULArray(3) = 600
ULArray(4) = 1000
Case "Cd (Kadmium)"
ULArray(0) = 1.5
ULArray(1) = 10
ULArray(2) = 15
ULArray(3) = 30
ULArray(4) = 1000
Case "Cu (Kopper)"
ULArray(0) = 100
ULArray(1) = 200
ULArray(2) = 1000
ULArray(3) = 8500
ULArray(4) = 25000
Case "Cr (Krom)"
ULArray(0) = 50
ULArray(1) = 200
ULArray(2) = 500
ULArray(3) = 2800
ULArray(4) = 25000
Case "Hg (Kvikksølv)"
ULArray(0) = 1
ULArray(1) = 2
ULArray(2) = 4
ULArray(3) = 10
ULArray(4) = 1000
Case "Ni (Nikkel)"
ULArray(0) = 60
ULArray(1) = 135
ULArray(2) = 200
ULArray(3) = 1200
ULArray(4) = 2500
Case "Pb (Bly)"
ULArray(0) = 60
ULArray(1) = 100
ULArray(2) = 300
ULArray(3) = 700
ULArray(4) = 2500
Case "Zn (Sink)"
ULArray(0) = 200
ULArray(1) = 500
ULArray(2) = 1000
ULArray(3) = 5000
ULArray(4) = 25000
End Select
Return ULArray
End Function
'Fargeleggingsrutine - gir feilmelding
Private Sub SetDGVColColor()
Dim ULArray As Decimal()
Dim curValue As String
Dim decimalValue As Decimal
Dim colName = ""
For col As Integer = 2 To DataGridView2.ColumnCount - 1
colName = DataGridView2.Columns(col).Name
ULArray = GetElementColorsValues(colName)
For Each row As DataGridViewRow In DataGridView2.Rows
If (Not row.IsNewRow) Then
curValue = row.Cells(colName).Value
If (curValue IsNot Nothing) Then
Decimal.TryParse(curValue, decimalValue)
' the above TryParse line will set decimalValue to 0 if curValue is not a valid decimal i.e `<0.005`
Select Case decimalValue
Case >= ULArray(4)
row.Cells(colName).Style.BackColor = Color.BlueViolet
Case >= ULArray(3)
row.Cells(colName).Style.BackColor = Color.Red
Case >= ULArray(2)
row.Cells(colName).Style.BackColor = Color.Orange
Case >= ULArray(1)
row.Cells(colName).Style.BackColor = Color.Yellow
Case >= ULArray(0)
row.Cells(colName).Style.BackColor = Color.LawnGreen
Case Else
row.Cells(colName).Style.BackColor = Color.DodgerBlue
End Select
End If ' ignore empty cell
End If ' ignore the new row
Next
Next
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
SetDGVColColor()
End Sub
'Første svar fra JohnG
'Fjerde forsøk på eksport
Private Sub ExportToExcel()
Dim excel As Microsoft.Office.Interop.Excel._Application = New Microsoft.Office.Interop.Excel.Application()
Dim workbook As Microsoft.Office.Interop.Excel._Workbook = excel.Workbooks.Add(Type.Missing)
Dim worksheet As Microsoft.Office.Interop.Excel._Worksheet = Nothing
excel.Visible = True
Try
worksheet = workbook.ActiveSheet
worksheet.Name = "ExportedFromDataGrid"
Dim cellRowIndex As Integer = 1
Dim cellColumnIndex As Integer = 1
'gets header rows.
For Each column In DataGridView2.Columns
worksheet.Cells(1, column.Index + 1).Value = column.Name
Next
'gets all other rows
Dim rowIndex = 2
For Each row As DataGridViewRow In DataGridView2.Rows
If Not row.IsNewRow Then
For colIndex As Integer = 0 To DataGridView2.Columns.Count - 1
worksheet.Cells(rowIndex, colIndex + 1).Value = row.Cells(colIndex).Value.ToString
Next
End If
rowIndex += 1
Next
' Substituted code below that loops through each column with data
' then sets the color for each of those columns by calling the SetColColor method
For index As Integer = 2 To DataGridView2.Columns.Count
Dim colName = DataGridView2.Columns(index).Name
SetExcelColColor(worksheet, colName, index + 1)
Next
MessageBox.Show("Closing excel: save if needed!")
'workbook.SaveAs("YourFileName..",)
workbook.Close()
excel.Quit()
Marshal.ReleaseComObject(worksheet)
Marshal.ReleaseComObject(workbook)
Marshal.ReleaseComObject(excel)
Catch
MessageBox.Show("Error")
End Try
End Sub
'andre eksportrutine med fargelegging fra JohnG
Private Sub SetExcelColColor(worksheet As Microsoft.Office.Interop.Excel._Worksheet, colName As String, colIndex As Integer)
Dim rIndex = 2
Dim cIndex = colIndex
Dim ULArray = GetElementColorsValues(colName)
Dim curValue As String
Dim decimalValue As Decimal
For Each row As DataGridViewRow In DataGridView2.Rows
If (Not row.IsNewRow) Then
curValue = row.Cells(colName).Value
If (curValue IsNot Nothing) Then
Decimal.TryParse(curValue, decimalValue)
Select Case decimalValue
Case >= ULArray(4)
worksheet.Cells(rIndex, cIndex).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.BlueViolet)
Case >= ULArray(3)
worksheet.Cells(rIndex, cIndex).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red)
Case >= ULArray(2)
worksheet.Cells(rIndex, cIndex).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Orange)
Case >= ULArray(1)
worksheet.Cells(rIndex, cIndex).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow)
Case >= ULArray(0)
worksheet.Cells(rIndex, cIndex).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LawnGreen)
Case Else
worksheet.Cells(rIndex, cIndex).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.DodgerBlue)
End Select
rIndex += 1
End If ' ignore empty cell
End If ' ignore new row
Next
End Sub
Private Sub btnBrowse_Click(sender As Object, e As EventArgs) Handles btnBrowse.Click
Me.ExportToExcel()
End Sub
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
System.Windows.Forms.Application.Exit()
End Sub
End Class
I am not completely sure if I follow what you are asking so correct me if I am wrong. I am guessing the value “<0.005” is a value in a DataGridView Cell. If this is the case then you will need to change this “String” value to a “Decimal” The previous code I supplied did not check for empty or invalid numbers before the comparison is made. Since the cell value could be anything, the code needs to check for two things: An empty or null cell value or an invalid number. The error you are getting could be coming from either case.
Your comment
How can I make the routine disregard the < character, replace it with "" or replace the entire string with zero?
In this case when the cell contains the value “<0.005” will throw the error you see because comparing a string to a double won’t work. Since you state above that setting this value to zero (0) is sufficient, then I recommend you use a TryParse method. If the TryParse method is given an invalid number it will return zero (0). You could use this knowledge to implement what you describe.
I would recommend you use the same strategy you used to color the Excel cells. I changed the GetElementColorsValues method to return a Decimal array. This change is necessary if the values in the DataGridView are decimal values.
Public Function GetElementColorsValues(elementName As String) As Decimal()
Dim ULArray(4) As Decimal
Select Case elementName
Case "Arsenic"
ULArray(0) = 8
ULArray(1) = 20
ULArray(2) = 50
ULArray(3) = 600
ULArray(4) = 1000
Case "Cadmium"
ULArray(0) = 1.5
ULArray(1) = 10
………..
Now with this array we can compare the decimal values in the DataGridView. I used a Decimal.TryParse to get the Decimal value from a cells string value like below
Decimal.TryParse(curValue, decimalValue)
Above curValue is a string from the DataGridView cell and decimalValue is the retuned Decimal value from parsing the string to a decimal. The whole line Decimal.TryParse(curValue, decimalValue) will return true if the parse was successful and false if not successful.
The convenient aspect of this is that if the parse is unsuccessful (like with a value of <0.005) the TryParse will set the variable decimalValue to zero (0) as you are asking. Simply using the Decimal.TryParse will set the variable decimalValue to zero when it fails and will set it to a valid decimal number if it succeeds. This can be seen in the code below which checks for null or empty values then, if not null or empty uses the Decimal.TryParse to get the decimal value to be used in the comparison for coloring. It uses the same GetElementColorsValues(colName) method used when coloring the Excel cells... you will have to change the excel coloring code also to accommodate the Decimal array… below this method)
Update Edit to catch BDNULL cells in the data table
I was incorrect and technically, you CAN have a row in a DataTable that contains no column data. So the line: row.Cells(colName).Value will obviously throw the error you are getting. I am not saying this is the problem, but that was the only way I could reproduce your error. So the code below checks for these missing columns of data. I changed the code to use DataBoundItems since you are using this in your code; below that is the change needed without using the data bound item. Both worked, however if feel that may not be the case if the table is sorted or rows deleted etc. My next question would be why you would read these empty rows into the data table if they were well… EMPTY?
Obviously, you will need to make these changes when writing the grid to excel.
Private Sub SetDGVColColor()
Dim ULArray As Decimal()
Dim curValue As String
Dim decimalValue As Decimal
Dim colName = ""
For col As Integer = 2 To dgvElements.ColumnCount - 1
colName = dgvElements.Columns(col).Name
ULArray = GetElementColorsValues(colName)
Dim curDataBoundRow
For Each row As DataGridViewRow In dgvElements.Rows
If (Not row.IsNewRow) Then
curDataBoundRow = row.DataBoundItem ' <-- Added Code
If (Not IsDBNull(curDataBoundRow(colName))) Then ' <-- Added Code
curValue = curDataBoundRow(colName)
If (curValue IsNot Nothing) Then
Decimal.TryParse(curValue, decimalValue)
' the above TryParse line will set decimalValue to 0 if curValue is not a valid decimal i.e `<0.005`
Select Case decimalValue
Case >= ULArray(4)
row.Cells(colName).Style.BackColor = Color.BlueViolet
Case >= ULArray(3)
row.Cells(colName).Style.BackColor = Color.Red
Case >= ULArray(2)
row.Cells(colName).Style.BackColor = Color.Orange
Case >= ULArray(1)
row.Cells(colName).Style.BackColor = Color.Yellow
Case >= ULArray(0)
row.Cells(colName).Style.BackColor = Color.LawnGreen
Case Else
row.Cells(colName).Style.BackColor = Color.DodgerBlue
End Select
End If ' cell is empty
End If ' ignore null cells in data table <-- Added Code
End If ' ignore the new row if present
Next
Next
End Sub
Changes to code without using data bound items.
…….
For Each row As DataGridViewRow In dgvElements.Rows
If (Not row.IsNewRow) Then
If (Not IsDBNull(row.Cells(colName).Value)) Then ' <-- ADDED code
curValue = row.Cells(colName).Value
If (curValue IsNot Nothing) Then
…….
Changes to color excel cells method using Decimal value comparisons.
Private Sub SetExcelColColor(worksheet As Microsoft.Office.Interop.Excel._Worksheet, colName As String, colIndex As Integer)
Dim rIndex = 2
Dim cIndex = colIndex
Dim ULArray = GetElementColorsValues(colName)
Dim curValue As String
Dim decimalValue As Decimal
For Each row As DataGridViewRow In dgvElements.Rows
If (Not row.IsNewRow) Then
curValue = row.Cells(colName).Value
If (curValue IsNot Nothing) Then
Decimal.TryParse(curValue, decimalValue)
Select Case decimalValue
Case >= ULArray(4)
worksheet.Cells(rIndex, cIndex).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.BlueViolet)
Case >= ULArray(3)
worksheet.Cells(rIndex, cIndex).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red)
Case >= ULArray(2)
worksheet.Cells(rIndex, cIndex).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Orange)
Case >= ULArray(1)
worksheet.Cells(rIndex, cIndex).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow)
Case >= ULArray(0)
worksheet.Cells(rIndex, cIndex).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LawnGreen)
Case Else
worksheet.Cells(rIndex, cIndex).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.DodgerBlue)
End Select
rIndex += 1
End If ' ignore empty cell
End If ' ignore new row
Next
End Sub
The error you report is nothing to do with the presence of "<" in your string. It's because you're trying to an actual less-than comparison on a null value. That's invalid - there's no value to compare. You need to check whether the field is null before you perform the operation, and do something else instead:
If Me.DataGridView2.Rows(i).Cells("Cd (Kadmium)").Value IsNot DBNull.Value Then
'continue with the comparisons
Else
'do something else
End If
However, you're right, the presence of "<" will also cause a problem when trying to cast the value to a Double for the comparison.
For that you can do a simple string replacement, e.g.
Dim val = Me.DataGridView2.Rows(i).Cells("Cd (Kadmium)").Value.ToString().Replace("<", "")
Dim dVal = Convert.ToDouble(val)
If dVal < Ul1Cd Then
'etc
Also check your second loop:
For i As Double = 0 To Me.DataGridView2.Rows.Count - 1
you only need
for i = 0 To Me.DataGridView2.Rows.Count - 1
since you declared it before and as double?
Also make sure to set option strict on and infer to off in project compile options.
I am trying to figure out how to search from the first Textbox in the second column in the DataGridView and after searching, to list a yes or no answer if the State Abbreviation is a Northwest State in the second Textbox(T2). This is what I have so far.
Private Sub L_Click(sender As Object, e As EventArgs) Handles Locate.Click
Dim NW As String = "OR"
Dim NW2 As String = "WA"
Dim A As String = T1.Text
If A = "" Then
MessageBox.Show("Must Enter Abbreviation")
Else
For Each row As DataGridViewRow In DataGridView1.Rows
If row.Cells.Item("State_Abbreviation").Value = T1.Text Then
If row.Cells.Item("State_Abbreviation").Value = NW Or NW2 Then
T2.Text = "Yes"
End If
T2.Text = "No"
End If
MessageBox.Show("Please Enter Valid Abbreviation")
Next
End If
End Sub
Trying to simplify your code....
Dim result as String = "No"
Dim cellValue = row.Cells.Item("State_Abbreviation").Value.ToString
If cellValue = T1.Text Then
If cellValue = NW Or cellValue = NW2 Then
result = "Yes"
End If
End If
T2.Text = result
Hello I am currently making an hangman game where you guess a randomly selected word and you have three rounds. Each time you win a round you gain 10 points, however if you don't guess the word before you run out of the 10 generous attempts. You will lose the round not gain anything.
After you win you three games of hangman, you are shown a new input text box in a high score form to input your name to save your high score to be displayed on the high score form and it has validation in (Meaning the user is required have at least one character inside the text box). This is where my main problem is, my input box will save your name and your points if you pass validation first time. However if you didn't pass validation first time but pass it the second time, your name is saved however your high score will be saved but only with one point. Sorry for my bad English, but is there anyway to keep the amount of points the user scores even if they failed validation first time instead of changing it to 1 point? Here is my code (Sorry for the bad indention):
Hangman Game Code (This is where the user gets their points from)
Imports System.IO
Public Class Hangman
'Public Variables
Public AttemptsLeft As Integer = 0
Public Rounds As Integer = 1
Public LetterChosen As Char
Dim EndWord() As Char
Dim AppPath As String = Application.StartupPath()
Dim FileRead() As String
Public GameWinner As Boolean = True
Dim HangmanShapes As New List(Of PowerPacks.Shape)
Public ScoreForRound As Integer
Dim NewControls As New List(Of Button)
Dim GameWord As New List(Of Label)
'Form Load code
Private Sub Hangman_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Load Word Game Code
If File.Exists(AppPath & "/wordlist.txt") Then
FileRead = IO.File.ReadAllLines(AppPath & "/wordlist.txt")
Dim RandomWord As New List(Of String)
For i = 0 To FileRead.Length - 1
RandomWord.Add(FileRead(i))
Next
Dim random As New Random() 'Using this to randomise each word
EndWord = RandomWord(random.Next(0, RandomWord.Count - 1)).ToUpper.ToCharArray 'Will put each character of the randomly chosen word into the labels.
Score.Text = UScore
Round.Text = Rounds
Letter1.Text = EndWord(0)
Letter1.Visible = False
Letter2.Text = EndWord(1)
Letter2.Visible = False
Letter3.Text = EndWord(2)
Letter3.Visible = False
Letter4.Text = EndWord(3)
Letter4.Visible = False
Letter5.Text = EndWord(4)
Letter5.Visible = False
Letter6.Text = EndWord(5)
Letter6.Visible = False
'Attempts left code
End If
With HangmanShapes
.Add(Attempt1)
.Add(Attempt2)
.Add(Attempt3)
.Add(Attempt4)
.Add(Attempt5)
.Add(Attempt6)
.Add(Attempt7)
.Add(Attempt8)
.Add(Attempt9)
.Add(Attempt10Part1)
.Add(Attempt10Part2)
End With
With NewControls
.Add(LetterA)
.Add(LetterB)
.Add(LetterC)
.Add(LetterD)
.Add(LetterE)
.Add(LetterF)
.Add(LetterG)
.Add(LetterH)
.Add(LetterI)
.Add(LetterJ)
.Add(LetterK)
.Add(LetterL)
.Add(LetterM)
.Add(LetterN)
.Add(LetterO)
.Add(LetterP)
.Add(LetterQ)
.Add(LetterR)
.Add(LetterS)
.Add(LetterT)
.Add(LetterU)
.Add(LetterV)
.Add(LetterW)
.Add(LetterX)
.Add(LetterY)
.Add(LetterZ)
End With
With GameWord
.Add(Me.Letter1)
.Add(Me.Letter2)
.Add(Me.Letter3)
.Add(Me.Letter4)
.Add(Me.Letter5)
.Add(Me.Letter6)
End With
End Sub
Private Sub AllBtnClicks(ByVal sender As System.Object, ByVal e As EventArgs) Handles LetterA.Click, LetterB.Click, LetterC.Click, LetterD.Click, LetterE.Click, LetterF.Click, LetterG.Click, LetterH.Click, LetterI.Click, LetterJ.Click, LetterK.Click, LetterL.Click, LetterM.Click, LetterN.Click, LetterO.Click, LetterP.Click, LetterQ.Click, LetterR.Click, LetterS.Click, LetterT.Click, LetterU.Click, LetterV.Click, LetterW.Click, LetterX.Click, LetterY.Click, LetterZ.Click
'Declartions
Dim LetterGuess As Button = sender
LetterGuess.Enabled = False
Dim LetterCorrect As Boolean = False
'Loop
For Each Letter In EndWord
If GetChar(LetterGuess.Name, 7) = Letter Then
Select Case Array.IndexOf(EndWord, Letter)
Case Is = 0
Letter1.Visible = True
Case Is = 1
Letter2.Visible = True
Case Is = 2
Letter3.Visible = True
Case Is = 3
Letter4.Visible = True
Case Is = 4
Letter5.Visible = True
Case Is = 5
Letter6.Visible = True
End Select
LetterCorrect = True
End If
Next
'Lives left code
If LetterCorrect = False Then
AttemptsLeft += 1
Select Case AttemptsLeft
Case 1
Attempt1.Visible = True
Attempts.Text = 1
Case 2
Attempt2.Visible = True
Attempts.Text = 2
Case 3
Attempt3.Visible = True
Attempts.Text = 3
Case 4
Attempt4.Visible = True
Attempts.Text = 4
Case 5
Attempt5.Visible = True
Attempts.Text = 5
Case 6
Attempt6.Visible = True
Attempts.Text = 6
Case 7
Attempt7.Visible = True
Attempts.Text = 7
Case 8
Attempt8.Visible = True
Attempts.Text = 8
Case 9
Attempt9.Visible = True
Attempts.Text = 9
Case 10
Attempt10Part1.Visible = True
Attempt10Part2.Visible = True
Attempts.Text = 10
LetterA.Enabled = False
LetterB.Enabled = False
LetterC.Enabled = False
LetterD.Enabled = False
LetterE.Enabled = False
LetterF.Enabled = False
LetterG.Enabled = False
LetterH.Enabled = False
LetterI.Enabled = False
LetterJ.Enabled = False
LetterK.Enabled = False
LetterL.Enabled = False
LetterM.Enabled = False
LetterN.Enabled = False
LetterO.Enabled = False
LetterP.Enabled = False
LetterQ.Enabled = False
LetterR.Enabled = False
LetterS.Enabled = False
LetterT.Enabled = False
LetterU.Enabled = False
LetterV.Enabled = False
LetterW.Enabled = False
LetterX.Enabled = False
LetterY.Enabled = False
LetterZ.Enabled = False
MsgBox("You have lost the round!")
ResetForm(0)
End Select
'Winning a round code
Else : Dim GameWinner As Boolean = True
Dim WordCheck As Label
For Each WordCheck In GameWord
If Not WordCheck.Visible Then
GameWinner = False
Exit For
End If
Next
If GameWinner Then
MsgBox("You have won the round!")
ResetForm(10)
'Losing a round code
End If
End If
End Sub
Private Sub ResetForm(ScoreForRound As Integer)
UScore += ScoreForRound
If Rounds = 3 Then
Me.Close()
HighScore.Show()
Else
Score.Text = +10
AttemptsLeft = 0
Attempts.Text = 0
Rounds += 1
Hangman_Load(Nothing, Nothing)
Dim HangmanReset As PowerPacks.Shape
For Each HangmanReset In HangmanShapes
HangmanReset.Visible = False
Next
Dim ControlReset As Control
For Each ControlReset In NewControls
ControlReset.Enabled = True
Next
End If
End Sub
End Class
High score form (This is where the user saves their points and also be able to view their high scores afterwards)
Imports System.IO
Public Class HighScore
Dim AppPath As String = Application.StartupPath()
Public Username As String
Private Sub HighScore_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim FileData() As String
Dim SizeArray As Integer
Try
FileData = File.ReadAllLines(AppPath & "/highscore.txt")
SizeArray = FileData.Length
Catch Break As Exception
MsgBox("The file is missing!", MsgBoxStyle.Critical)
End Try
For begin = 0 To SizeArray - 1 Step 1
Me.UserNameLabel.Text = UserNameLabel.Text & FileData(begin) & vbNewLine
Next
End Sub
Private Sub Backtomainmenu_Click(sender As Object, e As EventArgs) Handles Backtomainmenu.Click
MainMenu.Visible = True
Me.Visible = False
End Sub
Private Sub HelpButtonHighScore_Click(sender As Object, e As EventArgs) Handles HelpButtonHighScore.Click
MsgBox("This is the high score, this shows the top 10 players who achieved well in this game, this is ranked by the amount of points score. If you want to have your name in this high score, play the game well in order to achieve this.", MsgBoxStyle.Information)
End Sub
'This is where the user saves their high scores
Private Sub SaveName_Click(sender As Object, e As EventArgs) Handles SaveName.Click
Username = NameInput.Text
Try
File.WriteAllText(AppPath & "/highscore.txt", Username & " " & UScore)
Catch ex As Exception
MsgBox("The file is missing!", MsgBoxStyle.Critical)
End Try
UScore = vbNull
If NameInput.Text = "" Then
MsgBox("Enter a name please")
Else
File.WriteAllText(AppPath & "/highscore.txt", Username & " " & UScore)
Me.Close()
MainMenu.Show()
End If
End Sub
End Class
the user is required have at least one character inside the text box
Your code is currently saving to the file before any validation occurs:
Username = NameInput.Text
Try
File.WriteAllText(AppPath & "/highscore.txt", Username & " " & UScore)
Catch ex As Exception
MsgBox("The file is missing!", MsgBoxStyle.Critical)
End Try
UScore = vbNull
After that block of code (which has already written to the file), then you're attempting to validate:
If NameInput.Text = "" Then
MsgBox("Enter a name please")
Else
File.WriteAllText(AppPath & "/highscore.txt", Username & " " & UScore)
Me.Close()
MainMenu.Show()
End If
Consolidate the code, and only write to the file if your validation is successful:
Private Sub SaveName_Click(sender As Object, e As EventArgs) Handles SaveName.Click
Username = NameInput.Text.Trim
If Username = "" Then
MsgBox("Enter a name please!")
Else
Try
File.WriteAllText(AppPath & "/highscore.txt", Username & " " & UScore)
UScore = vbNull
Me.Close()
MainMenu.Show()
Catch ex As Exception
MsgBox("Error Saving High Score File!" & vbCrLf & vbCrLf & ex.ToString(), MsgBoxStyle.Critical)
End Try
End If
End Sub
With UScore = vbNull, you might be resetting the score even if NameInput.Text = "".
So, instead of
Try
File.WriteAllText(AppPath & "/highscore.txt", Username & " " & UScore)
Catch ex As Exception
MsgBox("The file is missing!", MsgBoxStyle.Critical)
End Try
UScore = vbNull
If NameInput.Text = "" Then
MsgBox("Enter a name please")
Else
File.WriteAllText(AppPath & "/highscore.txt", Username & " " & UScore)
Me.Close()
MainMenu.Show()
End If
Put UScore = vbNull inside the If statement so
Try
File.WriteAllText(AppPath & "/highscore.txt", Username & " " & UScore)
Catch ex As Exception
MsgBox("The file is missing!", MsgBoxStyle.Critical)
End Try
If NameInput.Text = "" Then
MsgBox("Enter a name please")
Else
File.WriteAllText(AppPath & "/highscore.txt", Username & " " & UScore)
Me.Close()
MainMenu.Show()
UScore = vbNull 'Put it here instead
End If
I can't figure out whats wrong with my code. The code is supposed to calculate the total cost of an item using cost, quantity and promo code if u have one. It keeps crashing when I put in unused characters such as ! . Any help or improvements would be welcome.
Public Class Form1
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
Dim decDisplayTotal As Decimal
Dim decPrice As Decimal = txtPrice.Text
Dim intQuantity As Integer = txtQuantity.Text
Dim strPromoCode As String = txtPromoCode.Text
decDisplayTotal = decPrice * intQuantity
lblDisplayTotal.Text = "$" & decDisplayTotal
If decPrice < 0 Then
lblDisplayTotal.Text = ("")
txtPrice.Text = Nothing
txtQuantity.Text = Nothing
txtPromoCode.Text = Nothing
MessageBox.Show("Please enter an appropriate price.", "Invalid Input")
End If
If intQuantity < 0 Then
lblDisplayTotal.Text = ("")
txtPrice.Text = Nothing
txtQuantity.Text = Nothing
txtPromoCode.Text = Nothing
MessageBox.Show("Please enter an approriate quantity.", "Invalid Input")
End If
If strPromoCode = ("132") Then
MessageBox.Show("You used a limited time, 10% off code! Watch your price drop 10%!", "10% off")
decDisplayTotal = 0.9 * (decPrice * intQuantity)
lblDisplayTotal.Text = "$" & decDisplayTotal
End If
If strPromoCode = ("129") Then
MessageBox.Show("You used a limited time, 20% off code! Watch your price drop 20%!", "20% off")
decDisplayTotal = 0.8 * (decPrice * intQuantity)
lblDisplayTotal.Text = "$" & decDisplayTotal
End If
If strPromoCode = ("136") Then
MessageBox.Show("You used a limited time, 30% off code! Watch your price drop 30%!", "30% off")
decDisplayTotal = 0.7 * (decPrice * intQuantity)
lblDisplayTotal.Text = "$" & decDisplayTotal
End If
If strPromoCode = ("264") Then
MessageBox.Show("You used a limited time, buy 1 get 1 free code, so watch your total cut in half!", "Buy 1 Get 1 Free")
decDisplayTotal = 0.5 * (decPrice * intQuantity)
lblDisplayTotal.Text = "$" & decDisplayTotal
End If
If strPromoCode = ("125") Then
decDisplayTotal = (decPrice * intQuantity)
lblDisplayTotal.Text = "$" & decDisplayTotal
End If
Try
decPrice = Convert.ToInt16(txtPrice.Text)
Catch ex As Exception
lblDisplayTotal.Text = Nothing
MessageBox.Show("Please enter an acceptable price.", "Invalid Input")
txtPrice.Text = Nothing
End Try
Try
intQuantity = Convert.ToInt16(txtQuantity.Text)
Catch ex As Exception
lblDisplayTotal.Text = Nothing
MessageBox.Show("Please enter an acceptable quanitity.", "Invalid Input")
txtQuantity.Text = Nothing
End Try
Try
strPromoCode = Convert.ToInt16(txtPromoCode.Text)
Catch ex As Exception
lblDisplayTotal.Text = Nothing
MessageBox.Show("Please enter a valid Promo Code.", "Invalid Input")
txtPromoCode.Text = Nothing
End Try
End Sub
Private Sub txtPrice_TextChanged(sender As Object, e As EventArgs) Handles txtPrice.TextChanged
lblDisplayTotal.Text = ("")
End Sub
Private Sub txtQuantity_TextChanged(sender As Object, e As EventArgs) Handles txtQuantity.TextChanged
lblDisplayTotal.Text = ("")
End Sub
Private Sub txtPromoCode_TextChanged(sender As Object, e As EventArgs) Handles txtPromoCode.TextChanged
lblDisplayTotal.Text = ("")
End Sub
End Class
The first thing to understand clearly is that a string is not a number. VB allows this laxity but it bites back in many subtle ways.
I presume that you put the single point in any of your textboxes and then try to use that TEXT as it was a string. Sometime it works sometime not.
The correct approach is to ask the Framework to attempt the conversion and if it fails inform your user of the problem.
So, instead of using the various Convert.ToXXXXx use SomeType.TryParse (I.E. Int32.TryParse)
Dim decPrice As Decimal
Dim intQuantity As Integer
Dim strPromoCode As String
if Not Decimal.TryParse(txtPrice.Text, decPrice) Then
MessageBox.Show("Please type a valid number for Price")
ClearInputs()
return
End if
if Not Int32.TryParse(txtPrice.Text, intQuantity) Then
MessageBox.Show("Please type a valid number for Quantity")
ClearInputs()
return
End if
Private Sub ClearInputs()
lblDisplayTotal.Text = ""
txtPrice.Text = ""
txtQuantity.Text = ""
txtPromoCode.Text = ""
End Sub
Now your typed values are stored in the correct datatype variables and you could proceed with the remainder of your code.....
....
decDisplayTotal = decPrice * intQuantity
lblDisplayTotal.Text = "$" & decDisplayTotal.ToString
....
An important configuration that you need to set for your projects is Option Strict On in your project properties. This configuration will disallow the implicit conversion between strings and numbers and force you to write a more correct code.
And by the way, after the checks on the strPromoCode, you don't need to repeat again the process to convert the strings in the textboxes to the corresponding variables
I connected recently to SMS provider API using vb.net
I have created a group table and inserted all numbers in this group and then reach each row and send trigger the API to process sending.
The sms is not reached to all group members, its only delivered successfully to the first mobile number in the group.
How to solve this problem ? I think I have to set a delay between each sending and i did with no use. my code is below :
Function GetGroupsMobileNumbers() As ArrayList
Dim MobileNumbersArrayList As New ArrayList
For Each Contact As FilsPayComponent.ContactAddress In FilsPayComponent.ContactAddress.GetAllContactAddressByGroupId(ddlGroup.SelectedValue)
MobileNumbersArrayList.Add(Contact.Mobile)
Next
Return MobileNumbersArrayList
End Function
Protected Sub btnSend_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSend.Click
If ddlGroup.SelectedValue = 0 Then
lbResult.Text = "No groups selected"
Exit Sub
End If
Dim MobileNumbersArrayList As ArrayList
MobileNumbersArrayList = GetGroupsMobileNumbers()
If MobileNumbersArrayList.Count = 0 Then
lbResult.Text = "Group doesnt contain numbers"
Exit Sub
End If
Dim TotalNo As Integer = FilsPayComponent.ContactAddress.AddressContactsCount(ddlGroup.SelectedValue)
If MobileNumbersArrayList.Count * messagecount.Value <= FilsPayComponent.SmSUser.GetSmSUserByUserId(Context.User.Identity.Name).Balance Then
Dim txtMsg As String
Dim smstype As Integer
If hidUnicode.Value <> "1" Then
txtMsg = txtMessage.Text
smstype = 1
Else
txtMsg = ConvertTextToUnicode(txtMessage.Text)
smstype = 2
End If
Dim x As Integer
'For Each Contact As FilsPayComponent.ContactAddress In FilsPayComponent.ContactAddress.GetAllContactAddressByGroupId(ddlGroup.SelectedValue)
For Each Contact In MobileNumbersArrayList.ToArray
Dim toMobile As String = Contact.Mobile
If toMobile.Length > 10 Then
Dim ExecArrayList As ArrayList
ExecArrayList = SendSMS(toMobile, txtMsg, smstype)
'-- give the excution more time
If ExecArrayList.Count < 1 Then
Threading.Thread.Sleep(1000)
End If
'-- give the excution more time
If ExecArrayList.Count < 1 Then
Threading.Thread.Sleep(1000)
End If
'-- give the excution more time
If ExecArrayList.Count < 1 Then
Threading.Thread.Sleep(1000)
End If
x = x + 1
' lbresult.Text = "Sent Successfully"
End If
Next
FilsPayComponent.SmSUser.RemoveSmsCredit(Context.User.Identity.Name, messagecount.Value * x)
Dim NewsmsarchiveItem As New FilsPayComponent.smsarchive
NewsmsarchiveItem.FromMobile = txtSenderID.Text
NewsmsarchiveItem.ToMobile = "0"
NewsmsarchiveItem.GroupId = ddlGroup.SelectedValue
NewsmsarchiveItem.DateSent = DateTime.Now
NewsmsarchiveItem.Msg = txtMessage.Text
NewsmsarchiveItem.GroupCount = x
NewsmsarchiveItem.Optional1 = Context.User.Identity.Name
NewsmsarchiveItem.Optional2 = "1"
NewsmsarchiveItem.MessageNo = messagecount.Value
Try
NewsmsarchiveItem.Addsmsarchive()
lbResult.Text = "Message sent successfully"
btnSend.Visible = False
Catch ex As Exception
lbResult.Text = ex.Message
End Try
Else
lbResult.Text = "Not enough credit, please refill "
End If
End Sub
Sub SendSMS(ByVal toMobile As String, ByVal txtMsg As String, ByVal smstype As Integer)
Dim hwReq As HttpWebRequest
Dim hwRes As HttpWebResponse
Dim smsUser As String = "xxxxxx"
Dim smsPassword As String = "xxxxxx"
Dim smsSender As String = "xxxxxx"
Dim strPostData As String = String.Format("username={0}&password={1}&destination={2}&message={3}&type={4}&dlr=1&source={5}", Server.UrlEncode(smsUser), Server.UrlEncode(smsPassword), Server.UrlEncode(toMobile), Server.UrlEncode(txtMsg), Server.UrlEncode(smstype), Server.UrlEncode(smsSender))
Dim strResult As String = ""
Try
hwReq = DirectCast(WebRequest.Create("http://xxxxx:8080/bulksms/bulksms?"), HttpWebRequest)
hwReq.Method = "POST"
hwReq.ContentType = "application/x-www-form-urlencoded"
hwReq.ContentLength = strPostData.Length
Dim arrByteData As Byte() = ASCIIEncoding.ASCII.GetBytes(strPostData)
hwReq.GetRequestStream().Write(arrByteData, 0, arrByteData.Length)
hwRes = DirectCast(hwReq.GetResponse(), HttpWebResponse)
If hwRes.StatusCode = HttpStatusCode.OK Then
Dim srdrResponse As New StreamReader(hwRes.GetResponseStream(), Encoding.UTF8)
Dim strResponse As String = srdrResponse.ReadToEnd().Trim()
Select Case strResponse
Case "01"
strResult = "success"
Exit Select
Case Else
strResult = "Error: " + strResponse
Exit Select
End Select
End If
Catch wex As WebException
strResult = "Error, " + wex.Message
Catch ex As Exception
strResult = "Error, " + ex.Message
Finally
hwReq = Nothing
hwRes = Nothing
End Try
End Sub
If function GetGroupsMobileNumbers() does not return an array list of numbers (as Strings)
then comment out. MobileNumbersArrayList = GetGroupsMobileNumbers()
then use the commented out code below (with three of your own tel. numbers) to set it for testing.
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
If ddlGroup.SelectedValue = 0 Then
lbResult.Text = "No groups selected"
Exit Sub
End If
Dim MobileNumbersArrayList As New ArrayList
MobileNumbersArrayList = GetGroupsMobileNumbers()
'MobileNumbersArrayList.Add("07702123456")
'MobileNumbersArrayList.Add("07702123457")
'MobileNumbersArrayList.Add("07702123458")
If MobileNumbersArrayList.Count = 0 Then
lbResult.Text = "Group doesnt contain numbers"
Exit Sub
End If
Dim TotalNo As Integer = FilsPayComponent.ContactAddress.AddressContactsCount(ddlGroup.SelectedValue)
If MobileNumbersArrayList.Count * messagecount.Value <= FilsPayComponent.SmSUser.GetSmSUserByUserId(Context.User.Identity.Name).Balance Then
Dim txtMsg As String
Dim smstype As Integer
If hidUnicode.Value <> "1" Then
txtMsg = txtMessage.Text
smstype = 1
Else
txtMsg = ConvertTextToUnicode(txtMessage.Text)
smstype = 2
End If
Dim x As Integer
For Each Contact In MobileNumbersArrayList
If Contact.Length > 10 Then
SendSMS(Contact, txtMsg, smstype)
x = x + 1
End If
Next
FilsPayComponent.SmSUser.RemoveSmsCredit(Context.User.Identity.Name, messagecount.Value * x)
Dim NewsmsarchiveItem As New FilsPayComponent.smsarchive
NewsmsarchiveItem.FromMobile = txtSenderID.Text
NewsmsarchiveItem.ToMobile = "0"
NewsmsarchiveItem.GroupId = ddlGroup.SelectedValue
NewsmsarchiveItem.DateSent = DateTime.Now
NewsmsarchiveItem.Msg = txtMessage.Text
NewsmsarchiveItem.GroupCount = x
NewsmsarchiveItem.Optional1 = Context.User.Identity.Name
NewsmsarchiveItem.Optional2 = "1"
NewsmsarchiveItem.MessageNo = messagecount.Value
Try
NewsmsarchiveItem.Addsmsarchive()
lbResult.Text = "Message sent successfully"
btnSend.Visible = False
Catch ex As Exception
lbResult.Text = ex.Message
End Try
Else
lbResult.Text = "Not enough credit, please refill "
End If
End Sub
This btnSend sub should work if the rest of your code is okay. Note your line.
Dim TotalNo As Integer = FilsPayComponent.ContactAddress.AddressContactsCount(ddlGroup.SelectedValue)
Doesn't appear to do anything.
If you need to set a delay you would be better off turning SendSMS into a function that returns a sent confirmation to your btnSend loop. Most texting APIs can handle lists of numbers rather than waiting for a response for each text message. Afterall they only get added to a queue at their end.