steensommer
asked on
Summation of textbox!
Hi
VB .Net windows project.
I have a textbox399 from where I wan't to make a summation of the digits in the box.
The text in the textbox could look like this: "2-2-3-1" (The result should be: "8")
Trying to sum the digits I have made a replace but what then?
Dim Antal As Integer = Replace(TextBox399.Text, "-", "+")
Regards Steen
VB .Net windows project.
I have a textbox399 from where I wan't to make a summation of the digits in the box.
The text in the textbox could look like this: "2-2-3-1" (The result should be: "8")
Trying to sum the digits I have made a replace but what then?
Dim Antal As Integer = Replace(TextBox399.Text, "-", "+")
Regards Steen
ASKER
Hi Fernando
...and if I wan't to use several textboxes say: Textbox399, textbox400 etc?
Steen
...and if I wan't to use several textboxes say: Textbox399, textbox400 etc?
Steen
Hi steensommer;
Put the code in a function and call it for each textbox. Then in you code you can add up the return values
Private Function Summation( ByVal val As String ) As Integer
If val = "" Then Return -1 ' Error return -1
Dim Total As Integer = 0
Dim digits() As String = val.Trim.Split("-"c)
For Each dig As String In digits
If IsNumeric(dig) Then
Total += CType(dig, Integer)
Else
MessageBox.Show("The character => " & dig & " is not a number")
Return -1
End If
Next
Return Total
End Function
Fernando
Put the code in a function and call it for each textbox. Then in you code you can add up the return values
Private Function Summation( ByVal val As String ) As Integer
If val = "" Then Return -1 ' Error return -1
Dim Total As Integer = 0
Dim digits() As String = val.Trim.Split("-"c)
For Each dig As String In digits
If IsNumeric(dig) Then
Total += CType(dig, Integer)
Else
MessageBox.Show("The character => " & dig & " is not a number")
Return -1
End If
Next
Return Total
End Function
Fernando
ASKER
Hi Fernando
Impressing programming - thank you.
I have a few question:
1) What does the little c in "val.Trim.Split("-"c) do?
2) If I want to sum several textboxes how do I return the sum in a messagebox?
Steen
Impressing programming - thank you.
I have a few question:
1) What does the little c in "val.Trim.Split("-"c) do?
2) If I want to sum several textboxes how do I return the sum in a messagebox?
Steen
Hi Steen;
To your question, "What does the little c in "val.Trim.Split("-"c) do?"
The String Split method takes as its parameter a Char array or a single Char. To identify the character between the quotation marks as a Char and not a String you place the c after the last quotation mark.
To your question, "If I want to sum several textboxes how do I return the sum in a messagebox?"
One way to do this is to place all the TextBox text values into one string. Then feed the string in to function and place the return value into the message box.
Dim GetTotal As String
GetTotal = TextBox1.Text.Trim & "-" & TextBox2.Text.trim & "-" & ... & "-" TextBoxN.Text.Trim
Dim TotalValue As Integer = Summation( GetTotal )
MessageBox.Show( TotalValue )
Fernando
To your question, "What does the little c in "val.Trim.Split("-"c) do?"
The String Split method takes as its parameter a Char array or a single Char. To identify the character between the quotation marks as a Char and not a String you place the c after the last quotation mark.
To your question, "If I want to sum several textboxes how do I return the sum in a messagebox?"
One way to do this is to place all the TextBox text values into one string. Then feed the string in to function and place the return value into the message box.
Dim GetTotal As String
GetTotal = TextBox1.Text.Trim & "-" & TextBox2.Text.trim & "-" & ... & "-" TextBoxN.Text.Trim
Dim TotalValue As Integer = Summation( GetTotal )
MessageBox.Show( TotalValue )
Fernando
ASKER
Hi Fernando
I'm still impressed :0)
If one (or more) textbox(es) in the array is empty ("") then the TotalValue returns "-1". Can this be changes so it ignores the empty boxes but still returns the correct value.
Steen
I'm still impressed :0)
If one (or more) textbox(es) in the array is empty ("") then the TotalValue returns "-1". Can this be changes so it ignores the empty boxes but still returns the correct value.
Steen
You can place the following line at the beginning of the Summation function: just after this line, If val = "" Then Return -1 ' Error return -1.
' Check for leading and trailing - and remove them
val = Regex.Replace(val, "(^-{1,}|-{1,}$)", "")
' Replace muiltiple - with a single -
val = Regex.Replace(val, "(-{2,})", "-")
Fernando
' Check for leading and trailing - and remove them
val = Regex.Replace(val, "(^-{1,}|-{1,}$)", "")
' Replace muiltiple - with a single -
val = Regex.Replace(val, "(-{2,})", "-")
Fernando
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Fernando
You rearly lost me there :0)
Thank you for your great expert help!!
Regards Steen
You rearly lost me there :0)
Thank you for your great expert help!!
Regards Steen
Just a little explanation of the Regex patterns I used here.
In the following statement:
Regex.Replace(val, "(^-{1,}|-{1,}$)", "")
The pattern "(^-{1,}|-{1,}$)" has the following meaning
( ... ) Everything between ( and ) is in a capture group marked by ( and )
^ Starting from the beginning of the string
- This has no special meaning and therefore matches it self in the string
{1,} This means that the previous character must repeat 1 or more times with out limits
| Is the Or operator so if what is on the left does not match see if on the right matches.
$ This means that the previous character must match at the end of the string.
So what this, "^-{1,}", is saying is that if the string starts with one or more - then remove those characters and replace them with an empty string or if we are at the end of the string and we have one or more - characters then also remove them and replace them with the empty. I say empty string because the replacement string is the third parameter and it is "".
The other Regex statement:
Regex.Replace(val, "(-{2,})", "-")
Is basically the same thing. The pattern is "((-{2,})", and the replacement string is "-" working on the string in the variable val.
( ... ) We have the same type of group as above
- Character that matches itself
{2,} Previous character must repeat 2 or more times
So when you have an empty textbox or multiple empty text box’s you will have two or more consecutive - in the string. The Regex statement will find all - that repeat themselves two or more times and replace them with just one.
Good luck;
Fernando
In the following statement:
Regex.Replace(val, "(^-{1,}|-{1,}$)", "")
The pattern "(^-{1,}|-{1,}$)" has the following meaning
( ... ) Everything between ( and ) is in a capture group marked by ( and )
^ Starting from the beginning of the string
- This has no special meaning and therefore matches it self in the string
{1,} This means that the previous character must repeat 1 or more times with out limits
| Is the Or operator so if what is on the left does not match see if on the right matches.
$ This means that the previous character must match at the end of the string.
So what this, "^-{1,}", is saying is that if the string starts with one or more - then remove those characters and replace them with an empty string or if we are at the end of the string and we have one or more - characters then also remove them and replace them with the empty. I say empty string because the replacement string is the third parameter and it is "".
The other Regex statement:
Regex.Replace(val, "(-{2,})", "-")
Is basically the same thing. The pattern is "((-{2,})", and the replacement string is "-" working on the string in the variable val.
( ... ) We have the same type of group as above
- Character that matches itself
{2,} Previous character must repeat 2 or more times
So when you have an empty textbox or multiple empty text box’s you will have two or more consecutive - in the string. The Regex statement will find all - that repeat themselves two or more times and replace them with just one.
Good luck;
Fernando
This should do what you need.
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
If TextBox399.Text = "" Then Return
Dim Total As Integer = 0
Dim digits() As String = TextBox399.Text.Trim.Split
For Each dig As String In digits
If IsNumeric(dig) Then
Total += CType(dig, Integer)
Else
MessageBox.Show("The character => " & dig & " is not a number")
Return
End If
Next
MessageBox.Show(Total.ToSt
End Sub
Fernando