divide message length

villson7

Member
Joined
Dec 14, 2006
Messages
15
Programming Experience
Beginner
Hello, i m still begineer in vb.NET. recently i have develop a program that will count how many message in a textbox , if the character is over amount of 100 characters, it will be consider as 1 message, if 200 character it will be 2 message and other characters will count as the same method, how can i write this code?

Can i add in extra feature like limit the character when it exceed over 100 character, then the dialog box will prompt user to select whether continue to type more character while the message count still function or end the message typing.
 
can anybody help me on this problem, actually i want to use this function to count how many message does user type, if user type 100 character ,it consider 1 message, and 101 is consider 2 messae and so on, so no matter how many character usr has type, the function will still count the total number of messages according for total cahracter.

please help me...:confused:
 
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Message [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#800000]"...much text..."[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Const[/COLOR][/SIZE][SIZE=2] MessageLimit [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 100[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] NumberOfMessages [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = Math.Ceiling(Message.Length / MessageLimit)[/SIZE]
 
thanks john h , it works great, anyway how to i get the content of the message? example:
when i key in 101 character in the textbox, i want the 100 character display on the my other multiline textbox which is textbox2 and the next remain 1 character will display after the first 100 character which will seperate with a line of white space, can it be done?
 
Use the String.Substring function method.
 
Building off of JohnH's code...

VB.NET:
Dim Message As String = "...much text..."
Const MessageLimit As Integer = 100
Dim NumberOfMessages As Integer = Math.Ceiling(Message.Length / MessageLimit)

Dim Messages as new arraylist
for i as integer = 1 to NumberOfMessages
     Messages.add(Message.Substring(((i-1)*MessageLimit),MessageLimit))
next

'Messages now contains each 100-character message in a different entry.

'To view each extracted message:
For each Msg as string in Messages
     MsgBox(Msg)
Next

'To extract specific messages:
MsgBox(Messages(0))  'The first message
MsgBox(Messages(1))  'The second message, etc...
 
thanks B2Ben for ur code, anyway this problem occur when i try to run the code:
Index was out of range.Must be non-negative and less than the size of the collection.
Parameter name:index
(when i m not insert any character in the message)

Index and length must refer to location within the string. Parameter name: length
(when i insert character in the message)

What should i do, any idea?
 
You can check if message.length=0
It is very common in all programming situations to exclude the null/nothing situation from the processing code and instead check for this case before doing any processing on the object.
 
Back
Top