Appending Text at the end of a line within a textbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lawson morton
    New Member
    • Dec 2011
    • 2

    Appending Text at the end of a line within a textbox

    The user starts out by pasting text into a textbox:
    1.Phototypesett ing 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 Phototypesettin g 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(TextBox 1.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.
  • pod
    Contributor
    • Sep 2007
    • 298

    #2
    Regular Expressions can definitely resolve your problem

    Comment

    • lawson morton
      New Member
      • Dec 2011
      • 2

      #3
      Thanks for the response pod, but can you show me a coded example.

      Comment

      • Crusader2010
        New Member
        • Nov 2011
        • 13

        #4
        I don't understand, does the user see the question(s) and the answers somewhere and has to paste into a textbox whatever he thinks is the correct one? After pasting one answer, is he shown all the answers as correct/incorrect? If yes:

        - basically you have to concatenate all of your possible answers into one string(use a stringbuilder for efficiency), with a space between each other.
        - check for whatever the correct answer was (like "(C)") from wherever you store these. Then split your stringbuilder string by spaces, then parse this vector and check whether it contains your correct answer. If yes, you replace it with (itself + " correct") else with (itself + " incorrect"). Now you have a vector with your correct strings and you can do whatever you want with them (like putting them in a textbox, on the same line, with spaces between eachother)... like textBox1.text = strVector(0) + " " + strvector(1) + ...

        Don't know if this is what you want, but it's the simplest thing i could come up with in a hurry right now

        Comment

        • pod
          Contributor
          • Sep 2007
          • 298

          #5
          Found a testing site at
          Test your Javascript and PCRE regular expressions online.


          Code:
          'don't forget this
          Imports System.Text.RegularExpressions
          
          
          'your code
                  Dim textboxstring As String = Me.TextBox1.Text
                  Dim incorrectpattern As String = "(^\([A-Z]\)[a-z A-Z]*)"
                  Dim correctpattern As String = "(^\*\([A-Z]\)[a-z A-Z]*)"
                  textboxstring = Regex.Replace(textboxstring, incorrectpattern, "$1 Incorrect", RegexOptions.Multiline)
                  textboxstring = Regex.Replace(textboxstring, correctpattern, "$1 Correct", RegexOptions.Multiline)
                  textboxstring = Regex.Replace(textboxstring, "(^[0-9]*\.)(.*)", "MC $2", RegexOptions.Multiline)
                  MsgBox(textboxstring)
                  Me.TextBox1.Text = textboxstring
          Last edited by pod; Dec 14 '11, 02:15 PM. Reason: adding one line of code

          Comment

          Working...