Adding text after a particular text or word in textbox vb.net

I have a combo box and textbox.

The combo box will have options for user to chose.

Textbox1 has text:- "Troubleshooting Steps:" set in properties of the textbox.

If user Selects: "Rebooted PC" from combo box list, then presses a button it will add the text after"Troubleshooting Steps:".

Example: "Troubleshooting Steps: Rebooted PC". what i want to be able to do is add multiple selects one after other.

Example: Troubleshooting Steps: Rebooted PC- Problem returned after reboot- Replaced part

Id some googling and found this code which ive changed to do part of it.

But the problem i have it adds the last text selected "Troubleshooting Steps:" pushing the first text selected to the end.

Whats Happening Example: Troubleshooting Steps: (3rd)Replaced part- (2nd)Problem returned after reboot- (1st)Rebooted PC

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

Dim text As String = TextBox1.Text

Dim index As Integer = text.IndexOf("Troubleshooting Steps:")

Dim countChars As Integer countChars = "Troubleshooting Steps:".Length

    If index >= 0 Then
        text = text.Insert(index + countChars, ComboBox1.Text)
        TextBox1.Text = text
    End If
End Sub
Ken808Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dustockCommented:
The code you have is looking at the index of "Troubleshooting Steps:" which is 0, then you are adding the length of "Troubleshooting Steps:" which is 22, so every time you add text it's always adding at index 22.  Try this code, it looks at the length of the text box each time you click the button and adds to the end of text box.

        Dim text As String = TextBox6.Text

        Dim index As Integer = text.Length

        If index >= 0 Then
            text = text.Insert(index, ComboBox1.Text)
            TextBox6.Text = text
        End If

Open in new window

Ken808Author Commented:
Hey dustock, thats very interesting... but i need to have add the text after "Troubleshooting Steps:"

There will be other text in the box. "Troubleshooting Steps:" will be half way down the textbox but is liable to move down the box, depending on the text above.  

Any thoughts?
mohd_haqCommented:
Hi,

Did you tried this:

TextBox6.Text = TextBox6.Text & '-' & ComboBox1.Text
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Ken808Author Commented:
hi mohd_haq, that will not add the text after "Troubleshooting Steps:" .
dustockCommented:
Ken808,

Can you provide a screen shot of how you expect this to look?  Based on how I interprited your question the code I provided is adding the text after troubleshooting steps.  As you can see in the screen shot below I added "Rebooted PC" first and "Replaced Part" second.  If I can better understand how you want this to look I might be able to assist you better.  

Also I'm not sure where the dashes are coming from so I modified line 7 of the code I provided above to be like this.

TextBox6.Text = text + " - "

Open in new window


Troubleshooting steps
Ken808Author Commented:
Hi dustock

please see below, this is a part of call logging tool for techs ...

So they will type the current status after the "Status:" in the textbox.

Then I want them to use the combo box for generic text, so they dont have to type the same things over and over for multiple cases.

Reason i want one text boxes it to keep it neat and it fits well in my over all form.

Untitled.jpg
Ken808Author Commented:
it can also look like this... once its after Troubleshooting

Im not worried about the - that was just to make it pretty...


Untitled1.jpg:
Ken808Author Commented:
Hi dustock,

I do apologies your code is working for me now... i don't know what went wrong when i pasted in earlier ...

To add on to this question what if had another field below it, that i want to add to with second button.

Status:

Troubleshooting Steps:

Follow Up: (button 2 would add text here?)

thanks for you help so far.
dustockCommented:
Glad you got it working.  

The follow up would be within the same multi line text box?  Or would you have a second multi line text box that you'd put the follow up in?
Ken808Author Commented:
Ideally it would be in the same textbox.
dustockCommented:
Ok, having it the same text box will require a change to the logic I used above because I am appending text to the end of the text box.  If you use separate text boxes then you can apply the same logic.  But we can play around with the logic a bit and get the result you want.
dustockCommented:
I'm sure there might be a better way to accomplish this, but this was my quick and dirty solution.  I created a function called InsertText that accepts two parameters.  strText and strType.  In each button you have call InsertText and supply the ComboBox value you are using for the first parameter and the type is either "troubleshoot" or "followup" depending on what button it is.  For my test I used only one ComboBox   I wasn't sure if you planned to add a second ComboBox for the follow up or not.

    Private Sub InsertText(strText As String, strType As String)
        Dim lines As New List(Of String)

        For Each Line As String In TextBox6.Text.Split(vbNewLine)
            lines.Add(Line)
        Next

        For i As Integer = 0 To lines.Count - 1
            If strType = "troubleshoot" Then
                If lines(i).Contains("Troubleshooting Steps:") Then
                    Dim index As Integer = lines(i).Length
                    lines(i) = lines(i).Insert(index, strText)
                End If
            End If
            If strType = "followup" Then
                If lines(i).Contains("Follow Up:") Then
                    Dim index As Integer = lines(i).Length
                    lines(i) = lines(i).Insert(index, strText)
                End If
            End If
        Next

        TextBox6.Text = ""

        For Each strLine As String In lines
            TextBox6.Text &= strLine & vbNewLine
        Next
    End Sub

Open in new window


Follow Up

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ken808Author Commented:
Dude your legend!!!! This is really getting me out of a hole!!!

This sounds really stupid but i missed the class on functions, how do you go about calling this function? And to be pain in ass but how would you add 2nd combo box?
dustockCommented:
Glad I could help.  Also, just an FYI, I made an assumption that your multiline textbox would have the following static text:

Status:

Troubleshooting:

Follow Up:

If this text isn't there, then you'll never get any Troubleshooting or Follow Up items appended.

As far as calling the method/function, in your button1_click event remove the code you have and make the call to the InsertText method like below

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
     InsertText(ComboBox1.Text, "troubleshoot")
End Sub

Open in new window


As far as adding a second combo box, just drag one on to your form, populate it with your followup items.  Add a second button and in your button 2 click event do this.

InsertText(ComboBox2.Text, "followup")

Open in new window

Ken808Author Commented:
Dustock was super helpful his explanations were very clear!
Ken808Author Commented:
Hey Dustock, just another stupid question :P. Ive been trying to work out how to add text below Troubleshooting.

Example:

Troubleshooting:
- Rebooted PC
- Shot the PC

Follow Up:
- Buy New PC
dustockCommented:
Not a stupid a question at all.  Again, we just need to tweak our logic to get the desired result.  Before we were looking to find out the length of the line in order to append the text to the end, and even though that line might have spanned across multiple lines in the text box there was never a carriage return or newline.  Now, we want each item to be on a new line so we'll forget about the length of the line and start looking at how many lines there are under each heading.

First you need to create 2 new global variables, these will go under the Public Class FormName heading, where FormName is the name of the form :)

    Dim trblLine As Integer = 1
    Dim folUp As Integer = 1

Open in new window


Next, we need to update the InsertText() method to start creating new lines instead of appending to the end of the line.  This is where the global variables will come in.  Each time we add an item from the ComboBox it will create a new line, insert the text and increment the respective global variable.

    Private Sub InsertText(strText As String, strType As String)
        Dim lines As New List(Of String)        

        For Each Line As String In TextBox6.Text.Split(vbNewLine)
            lines.Add(Line)
        Next

        For i As Integer = 0 To lines.Count - 1
            If strType = "troubleshoot" Then
                If lines(i).Contains("Troubleshooting Steps:") Then
                    lines(i + trblLine) = lines(i + trblLine).Insert(0, strText + vbNewLine)
                    trblLine += 1
                End If
            End If
            If strType = "followup" Then
                If lines(i).Contains("Follow Up:") Then
                    lines(i + folUp) = lines(i + folUp).Insert(0, strText + vbNewLine)
                    folUp += 1
                End If
            End If
        Next

        TextBox6.Text = ""

        For Each strLine As String In lines
            TextBox6.Text &= strLine & vbNewLine
        Next
    End Sub

Open in new window


A few things, now that your adding new lines you may want to enable the vertical scroll bar on the TextBox.  Also, I'm not sure how and where this is being used.  If this form comes up each time they want to enter items in, then it should work fine.  If this is part of another form and you don't create a new instance each time you use it, you may need to reset the two global variables back to 1 at some point.
Ken808Author Commented:
Hey dude your Legend!!!!!

Im have one problem when i copy and pasted out the results i get results in the image...

the other picture is how it will be used..
Capture.JPG
Capture1.JPG
dustockCommented:
Ya know, I had a bigger multiline text box and I didn't notice all those extra lines.  Its being generated form the vbNewLine command.  I'll have to play around and see what I can do to avoid those.  Will the user be writing text in this box or will it always be added via a drop down?
Ken808Author Commented:
Great will i create a second question get you more points?

Yeah they will mostly likely need to add extra text too. its a basic template for IT dept, the results get pasted in to a call logging database.  

thanks dude your great help...
dustockCommented:
If you want to post a new question maybe some other experts can take what I have done and make it better.  Otherwise I can reply to this thread, but it may be a few days before I post something back, not sure how big of a hurry your in.
Ken808Author Commented:
cool im in no rush thanks dude!!!
dustockCommented:
No problem
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.