I want to convert selected item in combobox into a number and calculate it.
I got message like this when i try to start my vb.net program.
Conversion from string "< 20 " to type 'Integer' is not valid
Any help is greatly appreciated.
this is my code:
Private Sub SaveBtn_Click(sender As Object, e As EventArgs) Handles SaveBtn.Click
Dim LHR As Integer
Dim TipeRetak As Integer
Dim LbRetak As Integer
Dim LuKer As Integer
Dim Alur As Integer
Dim Tambal As Integer
Dim Kasar As Integer
Dim amblas As Integer
Select Case ComboLHR.SelectedIndex
Case "< 20 "
LHR = 0
Case "20 - 50"
LHR = 1
Case "50 - 200"
LHR = 2
Case "200 - 500"
LHR = 3
Case "500 - 2000"
LHR = 4
Case "2000 - 5000"
LHR = 5
Case "5000 - 20000"
LHR = 6
Case "20000 - 50000"
LHR = 7
Case "> 50000"
LHR = 8
End Select
Select Case ComboTipeRetak.SelectedIndex
Case "Buaya"
TipeRetak = 5
Case "Acak"
TipeRetak = 4
Case "Melintang"
TipeRetak = 3
Case "Memanjang"
TipeRetak = 1
Case "Tidak Ada"
TipeRetak = 1
End Select
Select Case ComboLebarRetak.SelectedIndex
Case "> 2 mm"
LbRetak = 3
Case "1 - 2 mm"
LbRetak = 2
Case "< 1 mm"
LbRetak = 1
Case "Tidak Ada"
LbRetak = 0
End Select
Select Case ComboLuasKerusakan.SelectedIndex
Case "> 30%"
LuKer = 3
Case "10 - 30%"
LuKer = 2
Case "< 10%"
LuKer = 1
Case "0"
LuKer = 0
End Select
Select Case ComboKedalamanAlur.SelectedIndex
Case "> 20 mm"
Alur = 7
Case "11 - 20 mm"
Alur = 5
Case "6 - 10 mm"
Alur = 3
Case "0 - 5 mm"
Alur = 1
Case "Tidak Ada"
Alur = 0
End Select
Select Case ComboTambal.SelectedIndex
Case ">30 %"
Tambal = 3
Case "20 - 30 %"
Tambal = 2
Case "10 - 20%"
Tambal = 1
Case "< 10%"
Tambal = 0
End Select
Select Case ComboKekasaran.SelectedIndex
Case "Desintegration"
Kasar = 4
Case "Pelepasan Butir"
Kasar = 3
Case "Rough(Hungry)"
Kasar = 2
Case "Fatty"
Kasar = 1
Case "Close Texture"
Kasar = 0
End Select
Select Case ComboAmblas.SelectedIndex
Case "> 5/100 m"
amblas = 4
Case "2 - 5/100 m"
amblas = 2
Case "0 - 2/100 m"
amblas = 1
Case "Tidak Ada"
amblas = 0
End Select
Dim comand As New MySqlCommand("INSERT INTO `tb_bnkt`(`nomor`, `Nama`, `kondisi prioritas`) VALUES (#nomor,#NamaRuas,#kondisi)", Connector)
comand.Parameters.Add("#nomor", MySqlDbType.VarChar).Value = TextNomor.Text
comand.Parameters.Add("#NamaRuas", MySqlDbType.VarChar).Value = ComboNamaRuas.Text
comand.Parameters.Add("#kondisi", MySqlDbType.VarChar).Value = 17 - (Val(LHR + TipeRetak + LbRetak + LuKer + Alur + Tambal + Kasar + amblas))
If comand.ExecuteNonQuery() = 1 Then
MessageBox.Show("Data disimpan")
Loading()
TextNomor.Clear()
ComboNamaRuas.Text = String.Empty
ComboLHR.Text = String.Empty
ComboTipeRetak.Text = String.Empty
ComboLebarRetak.Text = String.Empty
ComboLuasKerusakan.Text = String.Empty
ComboKedalamanAlur.Text = String.Empty
ComboTambal.Text = String.Empty
ComboKekasaran.Text = String.Empty
ComboAmblas.Text = String.Empty
Else
MessageBox.Show("Error")
End If
End Sub
With simple class and data-binding you can simplify your code a little bid and get rid of current problem as well.
Public Class MyItem
Public ReadOnly Name As String
Public ReadOnly Value As Integer
Public Sub New(name As String, value As Integer)
Me.Name = name
Me.Value = value
End Sub
End Class
' Then in constructor create collection of values and bind it ot the combobox
Dim LHRValues As New List(Of MyItem) From
{
New MyItem("< 20 ", 0),
New MyItem("20 - 50", 1),
New MyItem("50 - 200", 2),
New MyItem("200 - 500", 3),
New MyItem("500 - 2000", 4),
New MyItem("2000 - 5000", 5),
New MyItem("5000 - 20000", 6),
New MyItem("20000 - 50000", 7),
New MyItem("> 50000", 8)
}
ComboLHR.DisplayMember = "Name" ' Property Name will be used as a text
ComboLHR.ValueMember = "Value" ' Property Value will be used as a value
ComboLHR.DataSource = LHRValues
' Then in the code where you need selected value
Private Sub SaveBtn_Click(sender As Object, e As EventArgs) Handles SaveBtn.Click
Dim selectedLHR As Integer = DirectCast(ComboLHR.SelectedValue, Integer)
' Other selected values
End Sub
You only need to cast ComboLHR.SelectedValue to the type you expect (Integer), because SelectedValue is of type object. I hope you have Option Strict set On.
Related
how can I make this code work? Let's say we have in Textbox1 - the following: (2,4,6) - the textbox that will be scanned to determine the analysis.
TxtBoxIntDraws.Lines(0) = 3,6,7,9
TxtBoxIntDraws.Lines(1) = 2,6,9,10
TxtBoxIntDraws.Lines(2) = 3,5,7,10
then it will result: >
and if exists count reset to 0, if not exists count +1. the analysis will begin from the last line to the first line. TxtBoxIntDraws.Lines(2) - and finish with TxtBoxIntDraws.Lines(0)
1/3 - 0 (because one value exists 2 or 4 or 6, in TxtBoxIntDraws.Lines(0),
2/3 - 1 (because not not 2/3 - 2,6 or 4,6) and +1 add to value.
3/3 - 3 (because not 3/3 - 2,4,6) and +1 add to value.
so briefly, an analysis, 1/3, 2/3 and 3/3, if the value exists then it will be 0, if the value does not exist, it will be added + 1. It has to start from Step -1 to Line 1. I'm trying to make this code, and I'm not doing worked it.
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
Dim lastDraw1 As Integer = 0 '1/3
Dim lastDraw2 As Integer = 0 '2/3
Dim lastDraw3 As Integer = 0 '3/3
For i As Integer = Step to -1 To TxtBoxIntDraws.Lines.Count - 0
Dim lineVals As String() = TxtBoxIntDraws.Lines(i).Split(",")
Select Case lineVals.Count()
Case 1
lastDraw1 = 0
lastDraw2 += 1
lastDraw3 += 1
TextBox1.Text = lastDraw1
TextBox2.Text = lastDraw2
TextBox3.Text = lastDraw3
Case 2
lastDraw1 = 0
lastDraw2 = 0
lastDraw3 += 1
TextBox1.Text = lastDraw1
TextBox2.Text = lastDraw2
TextBox3.Text = lastDraw3
Case 3
lastDraw1 = 0
lastDraw2 = 0
lastDraw3 = 0
TextBox1.Text = lastDraw1
TextBox2.Text = lastDraw2
TextBox3.Text = lastDraw3
Case Else
'This should probably also be handled
End Select
Next
End Sub
Not sure what you are doing but to fix the For...Next
Dim StartIndex = TxtBoxIntDraws.Lines.Length - 1
For i = StartIndex To 0 Step -1
'Your code here
Next
Essentially, the purpose of this program is for revision. The program will generate a random mathematical expression, convert this into a visual representation of a binary tree and the user will have to traverse the binary tree. However, when I run this code, the initial node is far off centre. How would I go about re-positioning the binary tree to be in the middle of the PictureBox? Here is my code:
Public Class BTT
'VARAIBLES DECLARED CANNOT BE A FAULT
Dim nodes(7) As Object
'maybe try to alter the form so that the user can only get two incorrect answers'
Dim operators(6) As String
Dim actualAnswer As String = ""
Dim ogEquation(11) As String
Dim newLabel As String = "" 'used to store the equation to be stored in the label'
Dim userAnswer As String
Dim myTime As Double
Dim traversal(3) As String
Dim selectedTraversal As String
Dim treeCounter As Integer = 0
Dim draw As Boolean = False
Structure tree
Dim name As String
Dim left As Integer
Dim right As Integer
End Structure
Dim TreeNode(7) As tree
Dim scoreValue As Integer = 0 'stores the user's score for the game just completed'
Dim updating As Boolean = False 'if there are already 10 scores, the first one will need to be removed, so updating = true'
Class node
Public lineColour As Color
Public lineWidth As Integer
Public posX As Integer
Public posY As Integer
Public radius As Integer
Public Sub draw(e As PaintEventArgs)
Dim myPen As New Pen(Me.lineColour, Me.lineWidth)
e.Graphics.DrawEllipse(myPen, Me.posX, Me.posY, Me.radius, Me.radius)
End Sub
End Class
Sub DrawTree()
'these are the coordinates of the top left of the PictureBox
Dim leftX As Integer = 171
Dim rightX As Integer = 171 + PictureBox1.Width 'will be set to the edge of the picturebox
Dim topY As Integer = 138
Dim bottomY As Integer = 138 + PictureBox1.Height 'will be that number of pixels down, WILL NEVER CHANGE
Dim currentNode As Integer = 1 'will initially be the root node
For i = 1 To treeCounter 'loops based on the number of nodes in the array'
'assigns the basic information common to all of the nodes
nodes(i) = New node
nodes(i).radius = 70
nodes(i).lineWidth = 2
nodes(i).lineColour = Color.Black
Next
'need to go through the binary tree and determine x & y positions, with labels inside the ellipses
ConstructTree(currentNode, leftX, rightX, topY, bottomY)
draw = True
PictureBox1.Refresh()
End Sub
Sub ConstructTree(ByRef currentNode As Integer, ByRef leftX As Integer, ByRef rightX As Integer, ByRef topY As Integer, ByRef bottomY As Integer)
'ASK ISABEL ABOUT DYNAMICALLY GENERATING A LABEL'
'e.g. Dim test As New Label
nodes(currentNode).posX = (leftX + rightX) / 2 'gets average of x coordinates'
nodes(currentNode).posY = topY + ((bottomY - topY) * (1 / 3)) 'gets number of pixels down between bottom of form & last node, goes a third of the way down
If TreeNode(currentNode).left <> 0 Then 'if there is a node to the left
ConstructTree(TreeNode(currentNode).left, leftX, (leftX + rightX) / 2, nodes(currentNode).posY, bottomY)
End If
If TreeNode(currentNode).right <> 0 Then 'if there is a node to the right
ConstructTree(TreeNode(currentNode).right, (leftX + rightX) / 2, rightX, nodes(currentNode).posY, bottomY) 'swaps the left and right x-coords which have been changed
End If
End Sub
Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
If draw = True Then
For i = 1 To treeCounter
nodes(i).draw(e)
Next
'ALSO need to draw lines between the nodes, but IGNORE FOR NOW
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
TextBox1.Text = myTime - (0.1)
myTime = TextBox1.Text
If myTime = 0 Then
Timer1.Enabled = False
MsgBox("Time is up!")
checkupdate()
resetForm()
End If
'add another if statement checking for two wrong answers, will stop the timer and tell the user that they have got too man questions wrong'
End Sub
Sub resetForm()
Score.Text = "Score:"
Label1.Text = ""
scoreValue = 0
End Sub
Sub writefile()
FileOpen(1, "BTTscores.txt", OpenMode.Output)
Select Case updating
Case True
For i = 2 To 11
WriteLine(1, scores(i))
Next
Case False
For i = 1 To numberOfScores + 1
WriteLine(1, scores(i))
Next
End Select
FileClose()
End Sub
Sub checkupdate()
'need to check whether there are already ten elements in the array. If so, then delete the first score, move all the indices of the other scores 1 to the left and add the new scores on the end'
numberOfScores = 0 'will need to be reset if the user carries on using the program'
FileOpen(1, "BTTscores.txt", OpenMode.Input) 'need to bubble sort values'
Dim line As String
Do Until EOF(1)
line = LineInput(1)
If line <> "" Then
numberOfScores = numberOfScores + 1
scores(numberOfScores) = line 'copies the line to the array'
End If
Loop
If numberOfScores = 10 Then 'if one needs to be updated, need to read all but the first line into the array'
updating = True
scores(11) = scoreValue
Else 'if there are less than 10 scores, the user's current score just needs to be added on the end'
updating = False
scores(numberOfScores + 1) = scoreValue
End If
FileClose(1)
writefile()
End Sub
Private Sub EnterButton_Click(sender As Object, e As EventArgs) Handles EnterButton.Click
userAnswer = Answer.Text
If actualAnswer.Replace(" ", "") = userAnswer.Replace(" ", "") Then
UpdateScore()
End If
Score.Text = ("Score: " & scoreValue)
Answer.Text = ""
InitialSetup()
End Sub
Sub UpdateScore()
Select Case difficulty
Case "Easy"
scoreValue = scoreValue + 10
Case "Medium"
scoreValue = scoreValue + 15
Case "Hard"
scoreValue = scoreValue + 20
End Select
End Sub
Private Sub StartButton_Click(sender As Object, e As EventArgs) Handles StartButton.Click
scoreValue = 0
Initialisation()
InitialSetup()
myTime = 60
Timer1.Enabled = True
End Sub
Sub InitialSetup()
Dim currentNode As Integer = 1 'will be root node'
actualAnswer = ""
GetEquation()
newLabel = ""
selectedTraversal = traversal(CInt(Math.Floor((3 - 1 + 1) * Rnd())) + 1) 'will choose a random traversal'
newLabel = "Traversal: " + selectedTraversal
Label1.Text = newLabel
If selectedTraversal = "Prefix" Then
PrefixConversion(currentNode)
ElseIf selectedTraversal = "Infix" Then
InfixConversion()
Else
RPConversion()
End If
DrawTree()
End Sub
Sub Initialisation()
operators(1) = "("
operators(2) = "-"
operators(3) = "+"
operators(4) = "*"
operators(5) = "/"
operators(6) = ")"
traversal(1) = "Prefix"
traversal(2) = "Infix"
traversal(3) = "Postfix"
End Sub
Sub GetEquation()
Select Case difficulty
'RANDOM NUMBER FORMAT: CInt(Math.Floor((upperbound - lowerbound + 1) * Rnd())) + lowerbound'
Case "Easy"
'FORMAT: 17 * 4'
treeCounter = 3
ogEquation(1) = CInt(Math.Floor((20 - 1 + 1) * Rnd())) + 1
ogEquation(2) = operators(CInt(Math.Floor((5 - 2 + 1) * Rnd())) + 2)
ogEquation(3) = CInt(Math.Floor((20 - 1 + 1) * Rnd())) + 1
'initialising the binary tree iteration'
TreeNode(1).name = ogEquation(2) 'operator is the root'
TreeNode(1).left = 2
TreeNode(1).right = 3
TreeNode(2).name = ogEquation(1)
TreeNode(3).name = ogEquation(3)
'EG: * 17 4
Case "Medium"
treeCounter = 5
'FORMAT: 15 * (17 + 4)'
ogEquation(1) = CInt(Math.Floor((50 - 1 + 1) * Rnd())) + 1
ogEquation(2) = operators(CInt(Math.Floor((5 - 2 + 1) * Rnd())) + 2)
ogEquation(3) = operators(1)
ogEquation(4) = CInt(Math.Floor((50 - 1 + 1) * Rnd())) + 1
ogEquation(5) = operators(CInt(Math.Floor((5 - 2 + 1) * Rnd())) + 2)
ogEquation(6) = CInt(Math.Floor((50 - 1 + 1) * Rnd())) + 1
ogEquation(7) = operators(6)
'initialising the binary tree iteration'
TreeNode(1).name = ogEquation(2) 'root node'
TreeNode(1).left = 2
TreeNode(1).right = 3
TreeNode(2).name = ogEquation(1)
TreeNode(3).name = ogEquation(5)
TreeNode(3).left = 4
TreeNode(3).right = 5
TreeNode(4).name = ogEquation(4)
TreeNode(5).name = ogEquation(6)
'EG: * 15 + 17 4
Case "Hard"
'FORMAT: (17 + 4) * (20 / 10), random numbers are 1-150'
treeCounter = 7
ogEquation(1) = operators(1)
ogEquation(2) = CInt(Math.Floor((150 - 1 + 1) * Rnd())) + 1
ogEquation(3) = operators(CInt(Math.Floor((5 - 2 + 1) * Rnd())) + 2)
ogEquation(4) = CInt(Math.Floor((150 - 1 + 1) * Rnd())) + 1
ogEquation(5) = operators(6)
ogEquation(6) = operators(CInt(Math.Floor((5 - 2 + 1) * Rnd())) + 2)
ogEquation(7) = operators(1)
ogEquation(8) = CInt(Math.Floor((150 - 1 + 1) * Rnd())) + 1
ogEquation(9) = operators(CInt(Math.Floor((5 - 2 + 1) * Rnd())) + 2)
ogEquation(10) = CInt(Math.Floor((150 - 1 + 1) * Rnd())) + 1
ogEquation(11) = operators(6)
'initialising the binary tree iteration'
TreeNode(1).name = ogEquation(6) 'root node'
TreeNode(1).left = 2
TreeNode(1).right = 5
TreeNode(2).name = ogEquation(3)
TreeNode(2).left = 3
TreeNode(2).right = 4
TreeNode(3).name = ogEquation(2)
TreeNode(4).name = ogEquation(4)
TreeNode(5).name = ogEquation(9)
TreeNode(5).left = 6
TreeNode(5).right = 7
TreeNode(6).name = ogEquation(8)
TreeNode(7).name = ogEquation(10)
'EG: * + 17 4 / 20 10
End Select
End Sub
'Traversal Solutions'
'Postfix Conversion'
Sub RPConversion()
Dim myStack As New Stack(15)
Dim empty As Boolean = True
Dim temp As String 'used to store the current part of the original equation'
Dim operatorNum As Integer
Dim peekNum As Integer
Dim stoploop As Boolean = True
For i = 1 To ogEquation.Count - 1 'will iterate through the total number of elements in the array ogEquation'
If myStack.Count = 0 Then empty = True
temp = ogEquation(i)
MatchTempOperation(myStack, temp, operatorNum)
If operatorNum > 1 And operatorNum < 6 Then 'if the value is an operator'
If myStack.Count <> 0 Then 'if the stack contains a value'
CheckPeek(myStack, peekNum)
If operatorNum > peekNum Then
myStack.Push(temp)
ElseIf operatorNum = peekNum Then
actualAnswer = actualAnswer + myStack.Pop()
myStack.Push(temp)
Else 'operatorNum < peekNum'
actualAnswer = actualAnswer + myStack.Pop()
Do
stoploop = True
CheckPeek(myStack, peekNum)
If operatorNum > peekNum Then
myStack.Push(temp)
ElseIf operatorNum = peekNum Then
actualAnswer = actualAnswer + myStack.Pop()
myStack.Push(temp)
Else
actualAnswer = actualAnswer + myStack.Pop()
stoploop = False
End If
Loop Until stoploop Or myStack.Count = 0
End If
Else
myStack.Push(temp)
End If
ElseIf temp = "(" Then
myStack.Push(temp)
ElseIf temp = ")" Then
Do
actualAnswer = actualAnswer + myStack.Pop()
Loop Until myStack.Peek() = "("
myStack.Pop()
Else
actualAnswer = actualAnswer + temp
End If
operatorNum = 0
Next
If myStack.Count > 0 Then
For i = 1 To myStack.Count
actualAnswer = actualAnswer + myStack.Pop()
Next
End If
End Sub
Sub CheckPeek(ByVal myStack As Stack, ByRef peekNum As Integer) 'does the same as MatchTempOperation but for the top of the stack'
For i = 2 To 5 'skip one and six because we know it isn't a left or right bracket'
If myStack.Peek() = operators(i) Then
peekNum = i
End If
Next
End Sub
Sub MatchTempOperation(ByVal myStack As Stack, ByVal temp As String, ByRef operatorNum As Integer) 'wants to look at the stack but not be able to change it'
For i = 1 To 6
If temp = operators(i) Then
operatorNum = i
End If
Next
End Sub
'Infix'
Sub InfixConversion()
For i = 1 To 11
'check each element for empty spaces / brackets'
If ogEquation(i) <> "" And ogEquation(i) <> "(" And ogEquation(i) <> ")" Then
actualAnswer = actualAnswer + ogEquation(i)
End If
Next
End Sub
'Prefix'
Sub PrefixConversion(ByRef currentNode As Integer)
actualAnswer = actualAnswer + TreeNode(currentNode).name
If TreeNode(currentNode).left <> 0 Then
PrefixConversion(TreeNode(currentNode).left)
End If
If TreeNode(currentNode).right <> 0 Then
PrefixConversion(TreeNode(currentNode).right)
End If
End Sub
Private Sub ExitButton_Click(sender As Object, e As EventArgs) Handles ExitButton.Click
Me.Hide()
End Sub
End Class
Apologies for it's inefficiency, please also note that the "difficulty" variable is Public and stored outside of this form. Thanks :)
OUTPUT:
enter image description here
As you can see, the root node is far off centre in the bottom left.
I have a problem with my blackjack game in vb.net. This code I have will add the player's score perfectly, but when it comes to the dealer's score, it will not. It only takes the second card that the dealer has.
It is called with this:
addScore("p") 'add player's score
addScore("d") 'add dealer's score
And this is "addScore()":
Public Function card(player As String, index As Integer) As Label
Try
If player = "p" Then
Return GroupBox1.Controls.OfType(Of Label).Where(Function(l) l.Name = "YouCard" & index.ToString()).Single()
ElseIf player = "d" Then
Return GroupBox1.Controls.OfType(Of Label).Where(Function(l) l.Name = "DealerCard" & index.ToString()).Single()
End If
Catch
Return Nothing
End Try
End Function
Public Sub addScore(ByVal player As String)
Dim currScore As Integer
Dim result As Integer = 0
'Add Score
For value As Integer = 1 To 7
If card(player, value).Text = "A" AndAlso (currScore + 11) <= 21 Then
result = currScore + 11
ElseIf card(player, value).Text = "A" AndAlso (currScore + 1) <= 22 Then
result = currScore + 1
ElseIf IsNumeric(card(player, value).Text) Then
result = currScore + CInt(card(player, value).Text)
ElseIf card(player, value).Text = "" Then
result = result
Else
result = currScore + 10
End If
If player = "p" Then
YouScore.Text = result
Else
DealerScore.Text = result
End If
Next
End Sub
currScore shouldn't be there. Replace it with result
Public Sub addScore(ByVal player As String)
Dim result As Integer = 0
'Add Score
For value As Integer = 1 To 7
If card(player, value).Text = "A" AndAlso (result + 11) <= 21 Then
result = result + 11
ElseIf card(player, value).Text = "A" AndAlso (result + 1) <= 22 Then
result = result + 1
ElseIf IsNumeric(card(player, value).Text) Then
result = result + CInt(card(player, value).Text)
ElseIf card(player, value).Text = "" Then
result = result
Else
result = result + 10
End If
If player = "p" Then
YouScore.Text = result
Else
DealerScore.Text = result
End If
Next
End Sub
I am working on a project for my Visual Basic class, which is to create a digital check. The assignment requires us to input a check amount, which translates into words. In example, $1,200.00 needs to output "One thousand two hundred dollars"
For the most part, my code works. I'm using a switch statement. The original assignment was to have our check go up to a 9,999 value, but as we continue to build, we need to be able to convert up to 99,999.
As I said, I've been using a series of case statements, but realize that this is a very "hard code" way of doing this and would like create a method that can check these type of things for me, however I'm still new to Visual Basic and don't really have a good idea where to start or what is applicable in this scenario (we don't really have an example to go by.)
Here is my WriteCheck method that does the assigning/converting for the most part.
'Convert check value from a text field to a double'
Try
checkValue = checkInput.Text
Catch ex As InvalidCastException
MessageBox.Show("You must enter a numbers to write a check.")
End Try
'Operation to convert number to String'
thousands = checkValue \ 1000
hundreds = checkValue Mod 1000
hundreds = hundreds \ 100
tens = checkValue Mod 100
tens = tens \ 10
ones = checkValue Mod 10
ones = ones \ 1
'Case for thousands'
Select Case thousands & hundreds & tens
Case 1
tempStringT = "One"
Case 2
tempStringT = "Two"
Case 3
tempStringT = "Three"
Case 4
tempStringT = "Four"
Case 5
tempStringT = "Five"
Case 6
tempStringT = "Six"
Case 7
tempStringT = "Seven"
Case 8
tempStringT = "Eight"
Case 9
tempStringT = "Nine"
End Select
'Case for hundreds'
Select Case hundreds
Case 1
tempStringH = "one"
Case 2
tempStringH = "two"
Case 3
tempStringH = "three"
Case 4
tempStringH = "four"
Case 5
tempStringH = "five"
Case 6
tempStringH = "six"
Case 7
tempStringH = "seven"
Case 8
tempStringH = "eight"
Case 9
tempStringH = "nine"
End Select
'Case for tens'
Select Case tens Or ones
Case 1
tempStringTens = "one"
Case 2
tempStringTens = "twenty"
Case 3
tempStringTens = "thirty"
Case 4
tempStringTens = "fourty"
Case 5
tempStringTens = "fifty"
Case 6
tempStringTens = "sixty"
Case 7
tempStringTens = "seventy"
Case 8
tempStringTens = "eighty"
Case 9
tempStringTens = "ninety"
End Select
If tempStringTens <> "one" Then
'Case for ones'
Select Case ones
Case 1
tempStringO = "one"
Case 2
tempStringO = "two"
Case 3
tempStringO = "three"
Case 4
tempStringO = "four"
Case 5
tempStringO = "five"
Case 6
tempStringO = "six"
Case 7
tempStringO = "seven"
Case 8
tempStringO = "eight"
Case 9
tempStringO = "nine"
End Select
lblConverted.Text = tempStringT & " thousand " & tempStringH & " hundred " & tempStringTens & " " & tempStringO & " dollars " & change & "/100"
End If
If tempStringTens = "one" Then
Select Case ones
Case 1
tempStringO = "eleven"
Case 2
tempStringO = "twelve"
Case 3
tempStringO = "thirteen"
Case 4
tempStringO = "fourteen"
Case 5
tempStringO = "fifteen"
Case 6
tempStringO = "sixteen"
Case 7
tempStringO = "seventeen"
Case 8
tempStringO = "eighteen"
Case 9
tempStringO = "nineteen"
End Select
lblConverted.Text = tempStringT & " thousand " & tempStringH & " hundred " & tempStringO & " dollars"
End If
End Sub
This is my approach to the problem. The solution can be easily scaled up or down by adding or removing items in BigNumbers and upping the scope of num beyond Long if necessary. (As written, it will work for numbers up to 999,999,999,999,999.)
Public Function NumberToText(ByVal num As Long) As String
Dim BigNumbers() As String = {"", " Thousand", " Million", " Billion", " Trillion"}
Dim TextParts() As String = {}
If num < 0 Then
Return "Checks cannot be written for negative amounts."
ElseIf num >= 10 ^ ((BigNumbers.Length) * 3) Then
Return "This number exceeds the current maximum value of " & NumberToText(10 ^ ((BigNumbers.Length) * 3) - 1) & "."
End If
Dim LoopCount As Integer = 0
While num >= 1000
ReDim Preserve TextParts(TextParts.Length)
If num Mod 1000 > 0 Then
TextParts(TextParts.GetUpperBound(0)) = ThreeDigitText(num Mod 1000) & BigNumbers(LoopCount)
End If
num = num \ 1000
LoopCount += 1
End While
ReDim Preserve TextParts(TextParts.Length)
TextParts(TextParts.GetUpperBound(0)) = ThreeDigitText(num) & BigNumbers(LoopCount)
If Array.IndexOf(TextParts, "Error") > -1 Then
Return "An unknown error occurred while converting this number to text."
Else
Array.Reverse(TextParts)
Return Join(TextParts)
End If
End Function
Private Function ThreeDigitText(ByVal num As Integer) As String
If num > 999 Or num < 0 Then
Return "Error"
Else
Dim h As Integer = num \ 100 'Hundreds place
Dim tempText As String = ""
If h > 0 Then
tempText = OneDigitText(h) & " Hundred"
End If
num -= h * 100
If num > 0 And Not tempText = "" Then
tempText &= " "
End If
If num > 9 And num < 20 Then
Dim DoubleDigits() As String = {"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}
Return tempText & DoubleDigits(num - 10)
Else
Dim TensPlace() As String = {"Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}
Dim t As Integer = num \ 10 'Tens place
num -= t * 10
If t > 1 Then
tempText &= TensPlace(t - 2)
If num > 0 Then
tempText &= " "
End If
End If
If num > 0 Then
tempText &= OneDigitText(num)
End If
Return tempText
End If
End If
End Function
Private Function OneDigitText(ByVal num As Integer) As String
If num > 9 Or Num < 0 Then
Return "Error"
Else
Dim SingleDigits() As String = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"}
Return SingleDigits(num)
End If
End Function
Since this is for school, you will probably want to adapt parts of my code to your own rather than copy the whole thing. (Teachers can usually tell when you get code off the internet, especially if you can't explain every line.) If you have any questions about it, send them to the email listed in my profile.
I have the formula to check 9 integers,
First digit(d1) must be: 1, 2, 5, 6, 8 or 9
Last digit(d9) must be: 0 or 9
9xd1+8xd2+7xd3+6xd4+5xd5+4xd6+3xd7+2xd8+d9 mod 11 = 0
I can "validate" the key, but how can I generate more of this, knowing the conditions for it to be right?
How can I generate 9 different integers from 0 to 9 and check them under this formula?
Thanks for helping!
Generate the first 7 digits randomly, calculating the formula for those digits.
Set the 9th digit's value to 9, and add it to the formula.
Calculate a value for the 8th digit based on the mod of the result of the formula that causes the result of the formula to be mod 11 = 0.
For the exception case where attempting to do this causes mod 11 = 9, set the 9th digit to 0.
Implementation:
Private randGen As New Random()
Function GenNum() As Integer
Dim digits(0 To 8) As Integer
GenNum = 0
Dim checkSum As Integer
digits(0) = randGen.Next(6) + 1
If digits(0) >= 3 Then digits(0) += 2
If digits(0) >= 7 Then digits(0) += 1
checkSum += digits(0) * 9
For d As Integer = 1 To 6
digits(d) = randGen.Next(10)
checkSum += digits(d) * (9 - d)
Next
digits(8) = 9
checkSum += digits(8)
If (checkSum Mod 11) Mod 2 = 1 Then
digits(7) = (11 - (checkSum Mod 11)) \ 2
Else
digits(7) = ((12 - (checkSum Mod 11)) \ 2 + 4) Mod 10
End If
checkSum += digits(7) * 2
If checkSum Mod 11 = 9 Then digits(8) = 0
Dim pow10 As Integer = 1
For d As Integer = 8 To 0 Step -1
GenNum += pow10 * digits(d)
pow10 *= 10
Next
End Function
I can help you to generate integers from 0 to 9.
here is how your form should look like:
and here is the code:
Public Class Form1
Dim NumRandom As Random = New Random
Dim X, Y, Z As Integer
Private Sub GenerateBUT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GenerateBUT.Click
Dim a(9), i, j, RN As Integer
Dim flag As Boolean
flag = False
i = 1
a(j) = 1
Do While i <= 9
Randomize()
RN = CInt(Int(9 * Rnd()) + 1)
For j = 1 To i
If (a(j)) = RN Then
flag = True
Exit For
End If
Next
If flag = True Then
flag = False
Else
a(i) = RN
i = i + 1S
End If
Loop
Label1.Text = a(1)
Label2.Text = a(2)
Label3.Text = a(3)
Label4.Text = a(4)
Label5.Text = a(5)
Label6.Text = a(6)
Label7.Text = a(7)
Label8.Text = a(8)
Label9.Text = a(9)
Z = Label4.Text
Y = Label5.Text
X = Z + Y
X = X - Label3.Text
If X > 1 And X < 10 Then
X = NumRandom.Next(1, 7)
If X = 1 Then
Label1.Text = "0"
ElseIf X = 2 Then
Label2.Text = "0"
ElseIf X = 3 Then
Label3.Text = "0"
ElseIf X = 4 Then
Label4.Text = "0"
ElseIf X = 5 Then
Label5.Text = "0"
ElseIf X = 6 Then
Label6.Text = "0"
ElseIf X = 7 Then
Label7.Text = "0"
End If
End If
End Sub
End Class