Splitting Textbox Text in VB 2008

You can talk about VB programming here

Splitting Textbox Text in VB 2008

Postby locohacker » Sun Mar 29, 2009 2:36 pm


Aigh so I am spitting the textbox text with this code.
Where the division would be made in the " : "
Ya know like in paltalk nicks messages are like this
nick : my message here the code
Code: Select all
        Dim s As String = Me.RichTextBox2.Text
        Dim parts() As String = s.Split(":"c)
        Me.TextBox1.Text = (parts(0))
        Me.TextBox2.Text = (parts(1)).Trim

It works okie, but now and then I get this damn error
{"Index was outside the bounds of the array."}
wtf is that :)
User avatar
locohacker
Site Admin
Site Admin
 
Posts: 4364
Joined: Fri Dec 31, 2004 6:59 pm

Re: Splitting Textbox Text in VB 2008

Postby locohacker » Sun Mar 29, 2009 2:59 pm

Ah, the solved it in the vbforums.com forums ehhehe here the working code 8)
Code: Select all
Dim s As String = Me.RichTextBox2.Text
        Dim parts() As String = s.Split(":"c)
        If parts.Length >= 2 Then
            Me.TextBox1.Text = (parts(0))
            Me.TextBox2.Text = (parts(1)).Trim
        End If
User avatar
locohacker
Site Admin
Site Admin
 
Posts: 4364
Joined: Fri Dec 31, 2004 6:59 pm

Re: Splitting Textbox Text in VB 2008

Postby autopilot » Mon Mar 30, 2009 2:49 pm

yes what was happening was the line of text did not start with a nick so it did not have :
so parts would only have length of 1 {parts(0)}
User avatar
autopilot
Forum Moderator
Forum Moderator
 
Posts: 358
Joined: Sat Sep 23, 2006 7:19 pm

Re: Splitting Textbox Text in VB 2008

Postby NVYE » Mon Mar 30, 2009 6:02 pm

if you have the timestamps on .. then thw whole shit is going bad

Test it with timestamps on / off.
Add-on Bots for educational and amusement purposes:
http://www.vnfox.com

Website:
http://www.asktechguy.com
User avatar
NVYE
imFiles Senior
imFiles Senior
 
Posts: 234
Joined: Fri Jun 10, 2005 11:29 pm

Re: Splitting Textbox Text in VB 2008

Postby locohacker » Tue Mar 31, 2009 9:18 am

Crap, yeps timestamp messes it up :) so wtf I gonna do now lol double split lol man you guys betta stop using time stamp lol
User avatar
locohacker
Site Admin
Site Admin
 
Posts: 4364
Joined: Fri Dec 31, 2004 6:59 pm

Re: Splitting Textbox Text in VB 2008

Postby NVYE » Tue Mar 31, 2009 12:54 pm

hehe ... a lot of programs out there did not check for timestamps ... and most of the time the bot don't work if you turn it on.

First check for timestamps .. then do the do the split

If timestamps on Remove all text before nickname, then do the split ...

I don't feel good about this method splitting man ... think of something else ... it might be a way to do it ... but might not be the correct way :)

What if I enter,
:::wow::: cool ...

Then you're missing the text :)

Hope this helps.
Add-on Bots for educational and amusement purposes:
http://www.vnfox.com

Website:
http://www.asktechguy.com
User avatar
NVYE
imFiles Senior
imFiles Senior
 
Posts: 234
Joined: Fri Jun 10, 2005 11:29 pm

Re: Splitting Textbox Text in VB 2008

Postby autopilot » Tue Mar 31, 2009 2:26 pm

NVYE wrote:What if I enter,
:::wow::: cool ...

Then you're missing the text :)

if he wants to use the split method, he can always go with parts(0) contains the nic and then if the length of parts() > 2 then loop through parts(i) to reconstruct chat and then trim chat after it is reconstructed.

but there is a better way to do it
Code: Select all
Dim sLineIn As String = Me.RichTextBox2.Text
Dim iSplit As Integer = sLineIn.IndexOf(":")
Dim sNic As String = sLineIn.Substring(0, iSplit)
Dim sChat As String = sLineIn.Substring(iSplit + 1)
TextBox1.Text = sNic.Trim
TextBox2.Text = sChat.Trim


and once you understand what it is doing, it can be shortened to:
Code: Select all
Dim iSplit As Integer = RichTextBox2.Text.IndexOf(":")
TextBox1.Text = RichTextBox2.Text.Substring(0, iSplit).Trim
TextBox2.Text = RichTextBox2.Text.Substring(iSplit + 1).Trim
User avatar
autopilot
Forum Moderator
Forum Moderator
 
Posts: 358
Joined: Sat Sep 23, 2006 7:19 pm

Re: Splitting Textbox Text in VB 2008

Postby autopilot » Tue Mar 31, 2009 3:06 pm

you may even want to create 2 functions like this:
Code: Select all
    Private Function sNic(ByVal sLineIn As String) As String
        Dim iSplit As Integer = sLineIn.IndexOf(":")
        Return sLineIn.Substring(0, iSplit).Trim
    End Function

    Private Function sChat(ByVal sLineIn As String) As String
        Dim iSplit As Integer = sLineIn.IndexOf(":")
        Return sLineIn.Substring(iSplit + 1).Trim
    End Function


and then call them like this:
Code: Select all
        TextBox1.Text = sNic(RichTextBox2.Text)
        TextBox2.Text = sChat(RichTextBox2.Text)


if you have a task that is done in more then 1 place in the app, it is usually best to make it into its own sub/function so you dont have repeating code all over your app to write/update.

this also helps for troubleshooting. you do not have to troubleshoot a large piece of code, you can troubleshoot just a small portion at a time. Once that is working, move on to the next piece.
User avatar
autopilot
Forum Moderator
Forum Moderator
 
Posts: 358
Joined: Sat Sep 23, 2006 7:19 pm

Re: Splitting Textbox Text in VB 2008

Postby locohacker » Tue Mar 31, 2009 3:10 pm

Better way :roll: umm like looping to find the first " : " then split that :lol:
User avatar
locohacker
Site Admin
Site Admin
 
Posts: 4364
Joined: Fri Dec 31, 2004 6:59 pm

Re: Splitting Textbox Text in VB 2008

Postby autopilot » Tue Mar 31, 2009 3:27 pm

locohacker wrote:Better way :roll: umm like looping to find the first " : " then split that :lol:

how much code would it take you to loop through to : and how long will it take to process? I have it boiled down to 3 lines if you dont use the functions & 2 lines per function if you do use the functions. and without looping, you will not be burning up processor power for the task.
User avatar
autopilot
Forum Moderator
Forum Moderator
 
Posts: 358
Joined: Sat Sep 23, 2006 7:19 pm

Re: Splitting Textbox Text in VB 2008

Postby locohacker » Tue Mar 31, 2009 3:45 pm

:) man I don't know, hey auto but when I use the code I still got the problem with time stamp, man why people like using time stamp :mrgreen:
User avatar
locohacker
Site Admin
Site Admin
 
Posts: 4364
Joined: Fri Dec 31, 2004 6:59 pm

Re: Splitting Textbox Text in VB 2008

Postby locohacker » Tue Mar 31, 2009 3:49 pm

Got and idea, why not check if timestamp is on and then get the message to the right of "PM)" or "AM) " :swift:
User avatar
locohacker
Site Admin
Site Admin
 
Posts: 4364
Joined: Fri Dec 31, 2004 6:59 pm

Re: Splitting Textbox Text in VB 2008

Postby NVYE » Tue Mar 31, 2009 4:39 pm

autopilot wrote:
but there is a better way to do it
Code: Select all
Dim sLineIn As String = Me.RichTextBox2.Text
Dim iSplit As Integer = sLineIn.IndexOf(":")
Dim sNic As String = sLineIn.Substring(0, iSplit)
Dim sChat As String = sLineIn.Substring(iSplit + 1)
TextBox1.Text = sNic.Trim
TextBox2.Text = sChat.Trim


and once you understand what it is doing, it can be shortened to:
Code: Select all
Dim iSplit As Integer = RichTextBox2.Text.IndexOf(":")
TextBox1.Text = RichTextBox2.Text.Substring(0, iSplit).Trim
TextBox2.Text = RichTextBox2.Text.Substring(iSplit + 1).Trim


This looks like a good solution.
Add-on Bots for educational and amusement purposes:
http://www.vnfox.com

Website:
http://www.asktechguy.com
User avatar
NVYE
imFiles Senior
imFiles Senior
 
Posts: 234
Joined: Fri Jun 10, 2005 11:29 pm

Re: Splitting Textbox Text in VB 2008

Postby Chike » Tue Mar 31, 2009 6:43 pm

locohacker wrote:Got and idea, why not check if timestamp is on and then get the message to the right of "PM)" or "AM) " :swift:

First look if timstamp item in settings menu is checked, then you positively know if it's on or not. I once posted code to find commands at runtime, you can make it more simple by hard coding the submenu and item number.
Then look if the line begins with space, only notification lines begin with space, and process accordingly.
if timestamp is on look for ") " before the line, wether user or notofication.
User names ends with ": ", find the index and split like autopilot, but is iSplit + 2, no need to trim.
of course you need to check for whispers, and there is no realy reliable way to tell if the line is a whole line or part of line that broke because it's long, unless you gonna subclass the room text control.

Regardin processor usage, it's not only how many lines of "code" you have.
Split cause creation of 2 new string and copy from the original, Trim cause another creation of string and copy. But it's nothing compared to what you still gonna do.
Worry less about cpu and more on how to get it right first. Then if there's a problem you can look into optimizations
Image
Chike
imFiles Master
imFiles Master
 
Posts: 583
Joined: Sun May 13, 2007 6:20 pm


Return to Visual Basic Programming

 


  • Related topics
    Replies
    Views
    Last post

Who is online

Users browsing this forum: No registered users and 0 guests