Results 1 to 19 of 19

Thread: String Functions Not Working?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    78

    Exclamation String Functions Not Working?

    Hi, I'm trying to make code that inserts something into a textbox. However, it appears the insert and remove code isn't working. Here's the code:

    Code:
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            Dim a As Integer = TextBox2.SelectionStart
            Dim b As Integer = TextBox2.SelectionStart + TextBox2.SelectionLength
            TextBox2.Text.Insert(b, "[/i]")
            TextBox2.Text.Insert(a, "[i]")
        End Sub
    Whenever I click Button 3 nothing happens. Anyone know what's wrong?

    Ty in advance

  2. #2
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,148

    Re: String Functions Not Working?

    You must assign the value returned by Insert function back to the textbox text

    TextBox2.Text = TextBox2.Text.Insert(...)

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    78

    Re: String Functions Not Working?

    tyvm lol silly me

    anyways, now my problem is that whenever I do the insert code, it takes me to the top of the textbox.

    any way to keep the position?
    I googled it but the few results didn't work when I put it in

    And also, I have the same problem selecting text in a textbox - anyone know how i get it to select a specific part of text in a textbox? (i have stored the start and legnth as a variable) nothing happens when I press the button to run the code

    ty

  4. #4
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: String Functions Not Working?

    anyways, now my problem is that whenever I do the insert code, it takes me to the top of the textbox.
    Try using the ScrollToCaret method after you edit the text, e.g:

    vb Code:
    1. TextBox2.Text = TextBox2.Text.Insert(a, "[i]")
    2. TextBox2.ScrollToCaret()

    And also, I have the same problem selecting text in a textbox - anyone know how i get it to select a specific part of text in a textbox?
    Use the Select method, like so:
    vb Code:
    1. TextBox2.Select(0, 10) 'This example would select the first 10 characters obviously
    Just be aware that if you are doing this from a button click or something else that takes focus away from the textbox then you will not actually see the text as being selected. Just like you wouldn't if you manually selected some text and then clicked another control.
    Last edited by chris128; Sep 28th, 2010 at 05:31 PM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    78

    Re: String Functions Not Working?

    Ok ty - now I have a third problem lol:

    this is my code

    Code:
    Dim a As Integer = TextBox2.SelectionStart
            Dim b As Integer
            If TextBox2.SelectedText = "" Then
                b = 0
            Else
                b = TextBox2.SelectionStart + TextBox2.SelectionLength
            End If
            MsgBox(b)
            MsgBox(a)
            TextBox2.Text = TextBox2.Text.Insert(a + b, "[/b]")
            TextBox2.Text = TextBox2.Text.Insert(a, "[b]")
            TextBox2.Select(a + 3, b)
            TextBox2.ScrollToCaret()
    now whenever I highlight a single "+" sign in my textbox, I get an error

    anyone know why? i looked at the error and couldn't figure it out

    i msgboxed the two integers a and b and both came out normal

    ty

  6. #6
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: String Functions Not Working?

    Maybe if you tell us what the error is and show us the code where you try to highlight a + then we might have a clue what the problem is
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  7. #7

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    78

    Re: String Functions Not Working?

    Ok lol yah - idk why I'm back and the problem is gone.

    Anyone, to clarify, I want to know the best way to insert text into a textbox when a button is clicked, without changing the scrollbar. I've tried tons of things even copy and paste (paste code pastes my clipbord three times?) and the closest I can get is using the scrolltocaret... but even then it changes the screen a bit.

    can anyone give me any insight on a better way to do this?

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: String Functions Not Working?

    Whenever you set the Text property of the TextBox you are basically clearing the current contents and replacing it with new contents. The old contents is completely gone so the old caret position, etc, is completely gone.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    78

    Re: String Functions Not Working?

    Lol yah I know - so is there a way to insert the text without replacing everything? Or keep the exact position?

  10. #10
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: String Functions Not Working?

    Before changing the text, save the old selection information. After replacing the text, restore the old selection information.

    Code:
    Dim oldSelectionStart As Integer = txtText.SelectionStart
    Dim oldSelectionLength As Integer = txtText.SelectionLength
    
    ' perform replacements
    
    txtText.SelectionStart = oldSelectionStart
    txtText.SelectionLength = oldSelectionLength
    This will only restore the index and length; it won't necessarily have the same words selected in the text. If you want to leave the same words highlighted, you'll have to do more work to figure out what words were highlighted, then try to find them in the new text so you can highlight them. Since you're replacing text, there's no guarantee the words will still be there when you highlight again.

    I just tried a few text editors out and it looks like doing a replace usually kills your selection; likely for this reason. Visual Studio keeps the same selection, but it has a more intimate knowledge of the things you have highlighted and parsing code is a different matter entirely from parsing normal text.

  11. #11
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: String Functions Not Working?

    Or maybe he could just highlight the newly inserted text to show the user what was inserted.

    Just a suggestion
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    78

    Re: String Functions Not Working?

    Well here's my problem: I'm making a BBCode editor. So there are buttons to add bold and color and etc. codes around the selected text or wherever the cursor is it. That's fine. However, inserting it, as someone said, is like replacing the whole text. At the moment, I have code that selects the original text, and navigates to it. This is better, as I get back to a general location, but it is awkward since scrolltocaret makes the selected text at the bottom of the textbox. So this is annoying. Is there any way to keep the exact position? Or inserting text without moving it.

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: String Functions Not Working?

    I'm ashamed that I didn't think of this earlier but, if you want to insert text, you should probably set the SelectionStart and SelectionLength properties and then set the SelectedText property, e.g.
    vb.net Code:
    1. Dim selectionLength As Integer = Me.TextBox1.SelectionLength
    2.  
    3. If selectionLength > 0 Then
    4.     Dim startIndex As Integer = Me.TextBox1.SelectionStart
    5.     Dim endIndex As Integer = startIndex + selectionLength
    6.  
    7.     'Insert the closing tag.
    8.     Me.TextBox1.SelectionStart = endIndex
    9.     Me.TextBox1.SelectionLength = 0
    10.     Me.TextBox1.SelectedText = "[/b]"
    11.  
    12.     'Insert the opening tag.
    13.     Me.TextBox1.SelectionStart = startIndex
    14.     Me.TextBox1.SelectionLength = 0
    15.     Me.TextBox1.SelectedText = "[b]"
    16.  
    17.     'Reselect the original text.
    18.     Me.TextBox1.SelectionStart = startIndex + "[b]".Length
    19.     Me.TextBox1.SelectionLength = selectionLength
    20. End If

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    78

    Re: String Functions Not Working?

    Lol tyvm it worked great

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    78

    Re: String Functions Not Working?

    Hmm there seems to be one really random and annoying problem.

    I used your code, for bold, underline, and italics (except i changed the to of course). I also changed If selectionLength > 0 Then to If selectionLength >= 0 Then

    I don't know why, the code works perfectly for bold and underline, but for the italics code, if there is nothing selected, it adds like 10 spaces between the two tags. It's identical code, except for the tags it inserts (which are the same character length anyways), and the other two have no problems. Anyone know why?

    Code:
    Dim selectionLength As Integer = Me.TextBox2.SelectionLength
            If selectionLength >= 0 Then
                Dim startIndex As Integer = Me.TextBox2.SelectionStart
                Dim endIndex As Integer = startIndex + selectionLength
    
                'Insert the closing tag.
                Me.TextBox2.SelectionStart = endIndex
                Me.TextBox2.SelectionLength = 0
                Me.TextBox2.SelectedText = "
    Code:
    "
                'Insert the opening tag.
                Me.TextBox2.SelectionStart = startIndex
                Me.TextBox2.SelectionLength = 0
                Me.TextBox2.SelectedText = "[b]"
                'Reselect the original text.
                Me.TextBox2.SelectionStart = startIndex + "[b]".Length
                Me.TextBox2.SelectionLength = selectionLength
    end if
    Code:
            Dim selectionLength As Integer = Me.TextBox2.SelectionLength
            If selectionLength >= 0 Then
                Dim startIndex As Integer = Me.TextBox2.SelectionStart
                Dim endIndex As Integer = startIndex + selectionLength
    
                'Insert the closing tag.
                Me.TextBox2.SelectionStart = endIndex
                Me.TextBox2.SelectionLength = 0
                Me.TextBox2.SelectedText = "
    Code:
    "
                'Insert the opening tag.
                Me.TextBox2.SelectionStart = startIndex
                Me.TextBox2.SelectionLength = 0
                Me.TextBox2.SelectedText = "[i]"
                'Reselect the original text.
                Me.TextBox2.SelectionStart = startIndex + "[i]".Length
                Me.TextBox2.SelectionLength = selectionLength
            End If

  16. #16
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: String Functions Not Working?

    Isn't this all the code you need?

    Code:
    TextBox1.SelectedText = "[I]" & TextBox1.SelectedText & "[/I]"
    TextBox1.Select()
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  17. #17
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: String Functions Not Working?

    If you want to keep the original text selected while putting the tags then, save the current position and selection length and reapply them after replacing the text.
    vb.net Code:
    1. Dim ss, sl As Integer
    2. ss = TextBox1.SelectionStart
    3. sl = TextBox1.SelectionLength
    4. TextBox1.SelectedText = "[I]" & TextBox1.SelectedText & "[/I]"
    5. TextBox1.SelectionStart = ss + 3            '<--  +3 is length of "[I]" tags
    6. TextBox1.SelectionLength = sl
    7. TextBox1.Select()
    Last edited by Pradeep1210; Oct 9th, 2010 at 04:59 PM.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  18. #18

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    78

    Re: String Functions Not Working?

    No, I tried that and it doesn't keep the exact position.

    I don't know why, but the code tags got split in two (two codes made it 4)

    But essentially, why does the identical code produce different results? I tried copying the exact code for bold into italics, and the italics button still makes everything wonky :S

  19. #19
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: String Functions Not Working?

    I'm not sure I got exactly what you are trying to say.
    But this works perfectly ok as expected for me:
    vb.net Code:
    1. Private Sub InsertBBTags(ByVal tag As String)
    2.     Dim ss, sl As Integer
    3.     ss = TextBox1.SelectionStart
    4.     sl = TextBox1.SelectionLength
    5.     TextBox1.SelectedText = "[" & tag & "]" & TextBox1.SelectedText & "[/" & tag & "]"
    6.     TextBox1.SelectionStart = ss + tag.Length + 2  
    7.     TextBox1.SelectionLength = sl
    8.     TextBox1.Select()
    9. End Sub

    We can call it like this:
    vb.net Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.     'to surround with [I]...[/I]
    3.     InsertBBTags("I")
    4.  
    5.     'to surround with [CODE]...[/CODE]
    6.     InsertBBTags("CODE")
    7. End Sub
    Since some characters are being inserted, a minor change in the scrollbar position should be ok. But the content under selection should still be in view.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width