Skip to content

Splitting Textbox Text in VB 2008

Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
    Posts
  • #187406
    Admin
    Administrator

    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

            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 🙂

    #187419
    Admin
    Administrator

    Ah, the solved it in the vbforums.com forums ehhehe here the working code 8)

    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
    #187418
    autopilot
    Member

    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)}

    #187417
    AhFox
    Member

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

    Test it with timestamps on / off.

    #187416
    Admin
    Administrator

    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

    #187415
    AhFox
    Member

    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.

    #187414
    autopilot
    Member

    @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

    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:

    Dim iSplit As Integer = RichTextBox2.Text.IndexOf(":")
    TextBox1.Text = RichTextBox2.Text.Substring(0, iSplit).Trim
    TextBox2.Text = RichTextBox2.Text.Substring(iSplit + 1).Trim
    #187413
    autopilot
    Member

    you may even want to create 2 functions like this:

        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:

            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.

    #187412
    Admin
    Administrator

    Better way 🙄 umm like looping to find the first ” : ” then split that 😆

    #187411
    autopilot
    Member

    @Admin wrote:

    Better way 🙄 umm like looping to find the first ” : ” then split that 😆

    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.

    #187410
    Admin
    Administrator

    🙂 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:

    #187409
    Admin
    Administrator

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

    #187408
    AhFox
    Member

    @autopilot wrote:

    but there is a better way to do it

    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:

    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.

    #187407
    Chike
    Member

    @Admin 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

Viewing 14 posts - 1 through 14 (of 14 total)
  • You must be logged in to reply to this topic.