Question Appending text at the end of a line within a textbox

lawsonmorton

New member
Joined
Dec 8, 2011
Messages
4
Programming Experience
Beginner
The user starts out by pasting text into a textbox:
1.Phototypesetting was superior to automated typesetting because it was
(A)Easier
(B)Faster
(C)More flexible
*(D)All of the above

What I want to do is to tag the sentence and format it as follows:

MC Phototypesetting was superior to automated typesetting because it was (A)Easier incorrect (B)Faster incorrect (C)More flexible incorrect (D)All of the above correct

Using the following code I can easily replace the number (1) with MC:
TextBox1.Text = Replace(TextBox1.Text, ("1."), "MC ")

But for the life of me I cannot figure out how to tag each choice with correct or incorrect. The correct tag will go to the end of the choice which starts with * eg *(D) All of the above correct, while incorrect should go at the end of all other choices.

Can anyone please help. I haven't programmed in VB for a while.
 
Last edited:
Process each line and check if line starts with for example *. TextBox.Lines property will give an array of all the lines.
 
Give it a try and post back if you encounter difficulties with some part. It may help if you first write a description of the steps you think you need to code, then start translation those instructions to actual code. This is called pseudo code.
 
I tried the following code but it is not working

DimLines() AsString = TextBox1.Text.Split(Environment.NewLine)
For i AsInteger = 0 To TextBox1.Lines.Length > 1
If TextBox1.Lines(i).StartsWith("(A)") Then
Lines(i) = TextBox1.Text.Replace(vbCrLf, " incorrect")
EndIf


This code works in adding incorrect at the end of the line:
TextBox1.Text = TextBox1.Text.Replace(vbCrLf, " incorrect")

But it will just that for every single end of line.


 
One way to do this is to get the Lines array from the control, process the lines in that array, then assign the new lines to back to control.
One reason is that is is much easier to work with a single line of text than it is the whole multiline text.
You can think of description above as high level pseudo code, so start with the first "get the Lines array from the control", in code that would translate to:
Dim lines() As String = TextBox1.Lines

On to the next step, process the lines array using a loop, here you will only work with the lines variable. Give it a try.

Once done "assign the new lines to back to control" is almost reverse of first code:
TextBox1.Lines = lines
 
Back
Top