-
Dec 16th, 2013, 04:05 AM
#1
Thread Starter
Lively Member
SMS character counter
Hi!
One logical excercise...
I am developing a SMS sending program. I am struggling with character counting.
Regular SMS is 160 characters long (including whitespaces). So if the character count exeeds 160 chars then it automatically means that there are 2 SMS messages. If character count exeeds 320 chars then it would be 3 SMS messages and so on...all mobile phone users should be familiar with this.
This is the code I have so far in my textchanged event:
Code:
Dim str As String = TextBox1.Text
Dim cnt As Integer
Dim sms_cnt As Integer = 0 'variable that holds the number of sms messages
cnt = str.Length.ToString ' character count of textbox1
Label1.Text = cnt 'show how many characters are inserted to textbox1
'This should count how many sms messages are in textbox1
If cnt = 160 Then
sms_cnt = +1
Label2.Text = sms_cnt.ToString & " SMS"
cnt = 0
End If
I can't figure out how to reset cnt so it starts counting from 0 when 160 characters are exceeded. I understand that I should use something else instead of calculating the length of textbox1...but what?
Thanks in advance!
-
Dec 16th, 2013, 04:30 AM
#2
Re: SMS character counter
You can use the modulo and integer division operators: Mod and \.
Code:
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Const MESSAGE_LENGTH As Integer = 160
Dim totalCharacterCount = Me.TextBox1.TextLength
Dim messageNumber = (totalCharacterCount Mod MESSAGE_LENGTH) + 1
Dim messageCharacterCount = totalCharacterCount \ MESSAGE_LENGTH
Me.Label1.Text = String.Format("Message {0}, {1} characters of {2}.",
messageNumber,
messageCharacterCount,
MESSAGE_LENGTH)
End Sub
-
Dec 17th, 2013, 02:29 AM
#3
Thread Starter
Lively Member
Re: SMS character counter
Fantastic! Thank you! With some adjustments the code worked.
Totally forgot about modulus.
As I was being happy with the solution I came accross with another problem. The SMS service provider has a weird way of calculating SMS length: the first SMS is 160 characters long, the second SMS can contain only 145 characters and every next SMS 152 characters. Service provider said it is so because multipart messages take up some extra characters.
So jmcilhinney's code doesn't work in this situation because it is bound with the totalCharacterCount...
-
Dec 17th, 2013, 03:15 AM
#4
Re: SMS character counter
Originally Posted by lkallas
As I was being happy with the solution I came accross with another problem. The SMS service provider has a weird way of calculating SMS length: the first SMS is 160 characters long, the second SMS can contain only 145 characters and every next SMS 152 characters. Service provider said it is so because multipart messages take up some extra characters.
So jmcilhinney's code doesn't work in this situation because it is bound with the totalCharacterCount...
Then you're just going to have to do some basic arithmetic for the first two cases specifically and then you can use a variation of my code for the constant length of the messages thereafter. It's just a couple of If statements and a bit of addition and subtraction.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|