Results 1 to 24 of 24

Thread: Replace Text

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2009
    Posts
    15

    Replace Text

    OK... here is what I want to do...

    If the user enters some text "abc", I want to have the app replace that text with something else during their data entry..

    What I imagine is this:

    Private Sub TextBox5_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox5.KeyDown

    Dim cursorposition As String

    Dim currentlinetext As String


    If e.KeyCode = Keys.Space Then
    cursorposition = TextBox5.SelectionStart()

    currentlinetext = TextBox5.Lines(TextBox5.GetLineFromCharIndex(TextBox5.SelectionStart))

    TextBox5.Text = TextBox5.Text.Replace(" 2w ", " 2 weeks ")

    TextBox5.SelectionStart = cursorposition


    However, the function only saves the cursor position from the original text length, so it does not return the cursor to the end of the replaced string.

    One more thing... i do not necessarily was the cursor to go to the end of the textbox

    Can anyone help???

  2. #2
    Junior Member
    Join Date
    Mar 2007
    Location
    New York
    Posts
    30

    Re: Replace Text

    I'd imagine you would have to reduce or increase the cursor position based on the number of chars you removed or added. You most likely don't want to hard code the number because you will most likely be replacing different strings. So something like.

    Dim replace1 as string = " 2w "
    Dim replace2 as string = " 2 weeks "

    if replace1.length > replace2.length then
    cursorposition = cursorposition + (replace1.length - replace2.length)
    elseif replace2.length > replace1.length then
    cursorposition = cursorposition - (replace2.length - replace1.length)
    end if

    I have no idea if that code is right, but its just a theory. You also don't have to do anything when they are the same. Hense no else.
    Last edited by Darkwanderer; Mar 18th, 2009 at 01:50 PM. Reason: Sometimes I wonder how English is my only language...

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2009
    Posts
    15

    Re: Replace Text

    hmmm... interesting, i am going to play with that for a minute... thank you for the quick response

    I have two concerns:

    1. This code will be working on the spacebar keydown event - so it works as the person types

    2. I have over 1000 "shortcuts" that will need to be replaced.

    or if there is another way to do this -

    sort of like ms word macros using an auto-replace
    Last edited by cmalloy; Mar 18th, 2009 at 02:07 PM.

  4. #4
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Replace Text

    Use Select Case on Text.Chars(Text.Length-2). Then, put all of your shortcuts in Case blocks for replacing text after Text.Length - 2. At the end of all of these blocks, add:

    TextBox.Select(Text.Length,0)

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2009
    Posts
    15

    Re: Replace Text

    Sorry.. I do not follow

    Will I have to write out each one?

    Dim cursorposition As String
    Dim cursorposition1 As String

    If e.KeyCode = Keys.Space Then

    If TextBox5.Text.Contains(" 2w ") Then
    cursorposition1 = TextBox5.SelectionStart()
    TextBox5.Text = TextBox5.Text.Replace(" 2w ", " 2 weeks")
    cursorposition = cursorposition1 + 4
    TextBox5.SelectionStart = cursorposition
    End If

    End If

    is there a way to read this information from central source in order to input it? how about a table? so it can pull the shortcut phrase, the replacement text, and the text length to redefine the cursor position?

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Replace Text

    lets say i type in 2 w and the textbox then displays
    2 weeks and then i place the cursor between the w and e of weeks and press space. will i end up with
    2 weekseeks
    or
    2 weeks eeks

    i hope you are thinking this all the way through. if it were me i would start by having a second textbox with the decoded result in that.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7

    Thread Starter
    New Member
    Join Date
    Mar 2009
    Posts
    15

    Re: Replace Text

    how would the second textbox work?


    Quote Originally Posted by dbasnett View Post
    lets say i type in 2 w and the textbox then displays
    2 weeks and then i place the cursor between the w and e of weeks and press space. will i end up with
    2 weekseeks
    or
    2 weeks eeks

    i hope you are thinking this all the way through. if it were me i would start by having a second textbox with the decoded result in that.

  8. #8
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Talking Re: Replace Text

    No, that's not what I meant. I meant do something like this:
    (assuming that your textbox is called TextBox1)

    vb.net Code:
    1. 'When the user presses space or whatever
    2. Dim abbrlen As Integer = 2 'Set this to the length of your abbreviation
    3. Select Case Me.TextBox1.Substring(Me.TextBox1.Text.Length-(abbrlen+1),abbrlen)
    4.         Case "sb"
    5.                 Me.TextBox1.Text = Me.TextBox1.Text.Remove(Me.TextBox1.Text.Length-(abbrlen+1),abbrlen)
    6.                 Me.TextBox1.Text &= "somebody"
    7.                 Me.TextBox1.Select(Me.TextBox1.Text.Length,0)
    8.         'etc.
    9. End Select

    You could also use a Sub to do all of this for you, just one line each Case.
    You could read from a file, too, using a recursive Sub, but I'll let you figure that one out (unless you're really stuck.)

    Enjoy!! (not sure why I said that)
    minitech

  9. #9
    Junior Member
    Join Date
    Mar 2007
    Location
    New York
    Posts
    30

    Re: Replace Text

    Me.TextBox1.Select(Me.TextBox1.Text.Length,0)

    Was exactly what he was trying to avoid. What if they are typing in the middle of a 3 page essay. That's going to zoom them to the end of their text instead of where they were typing.

  10. #10

    Thread Starter
    New Member
    Join Date
    Mar 2009
    Posts
    15

    Re: Replace Text

    I hate to say it but I am really stuck...

    will this work for different lengths of shortcuts?



    Quote Originally Posted by minitech View Post
    No, that's not what I meant. I meant do something like this:
    (assuming that your textbox is called TextBox1)

    vb.net Code:
    1. 'When the user presses space or whatever
    2. Dim abbrlen As Integer = 2 'Set this to the length of your abbreviation
    3. Select Case Me.TextBox1.Substring(Me.TextBox1.Text.Length-(abbrlen+1),abbrlen)
    4.         Case "sb"
    5.                 Me.TextBox1.Text = Me.TextBox1.Text.Remove(Me.TextBox1.Text.Length-(abbrlen+1),abbrlen)
    6.                 Me.TextBox1.Text &= "somebody"
    7.                 Me.TextBox1.Select(Me.TextBox1.Text.Length,0)
    8.         'etc.
    9. End Select

    You could also use a Sub to do all of this for you, just one line each Case.
    You could read from a file, too, using a recursive Sub, but I'll let you figure that one out (unless you're really stuck.)

    Enjoy!! (not sure why I said that)
    minitech

  11. #11

    Thread Starter
    New Member
    Join Date
    Mar 2009
    Posts
    15

    Re: Replace Text

    yeah, that's where it gets tough...


    Quote Originally Posted by Darkwanderer View Post
    Me.TextBox1.Select(Me.TextBox1.Text.Length,0)

    Was exactly what he was trying to avoid. What if they are typing in the middle of a 3 page essay. That's going to zoom them to the end of their text instead of where they were typing.

  12. #12
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Replace Text

    Oh! You're right, my code only works if the user is typing at the end. Ok, then, I'll submit the proper code in a minute.

    And yes. To fix all your problems, put my code in a Sub, and call it for each line in a file, with the proper length. But like I said, it only works if the user is typing at the end.

  13. #13

    Thread Starter
    New Member
    Join Date
    Mar 2009
    Posts
    15

    Re: Replace Text

    Thanks so much for your help so far..

    I should let you know I am getting this error from your previous code

    'Substring' is not a member of 'System.Windows.Forms.TextBox'.

    Quote Originally Posted by minitech View Post
    Oh! You're right, my code only works if the user is typing at the end. Ok, then, I'll submit the proper code in a minute.

    And yes. To fix all your problems, put my code in a Sub, and call it for each line in a file, with the proper length. But like I said, it only works if the user is typing at the end.

  14. #14
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Talking Re: Replace Text

    Ok, here it is, the working code:

    vb.net Code:
    1. Public Class Form1
    2.     Structure Abbr
    3.         Dim Abbreviation As String
    4.         Dim Extended As String
    5.     End Structure
    6.     Dim abbrs As List(Of Abbr) = New List(Of Abbr)
    7.     Private Function GetAbbr(ByVal abbrS As String) As Abbr
    8.         Dim rt As Abbr
    9.         Dim cchr As Char
    10.         Dim i As Integer = 0
    11.         rt.Abbreviation = ""
    12.         While i < abbrS.Length
    13.             cchr = abbrS.Chars(i)
    14.             If cchr = " "c Then
    15.                 rt.Extended = abbrS.Substring(i + 1, abbrS.Length)
    16.                 If rt.Abbreviation = "" Then Return Nothing
    17.                 Return rt
    18.             End If
    19.             rt.Abbreviation &= cchr
    20.             i += 1
    21.         End While
    22.         Return Nothing
    23.     End Function
    24.     Private Sub txtMain_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMain.TextChanged
    25.         If Me.txtMain.Text.Chars(Me.txtMain.Text.Length - 1) <> " "c Then Exit Sub
    26.         Dim abbr As Abbr
    27.         For Each abbr In abbrs
    28.             ReplaceAbbr(abbr)
    29.         Next
    30.     End Sub
    31.     Private Sub ReplaceAbbr(ByVal abbr As Abbr)
    32.         Dim original As String = Me.txtMain.Text
    33.         Me.txtMain.Text = Me.txtMain.Text.Replace(abbr.Abbreviation, abbr.Extended)
    34.         If Not (Me.txtMain.Text = original) Then
    35.             Me.txtMain.Select(original.LastIndexOf(abbr.Abbreviation) + (abbr.Extended.Length - abbr.Abbreviation.Length), 0)
    36.         End If
    37.     End Sub
    38.     Private Sub LoadData()
    39.         Try
    40.             Dim fi As New IO.FileInfo("abbr_info.dat")
    41.             If Not fi.Exists Then Throw New IO.FileNotFoundException()
    42.             Dim fs As New IO.FileStream("abbr_info.dat", IO.FileMode.Open, IO.FileAccess.Read)
    43.             Dim rf As New IO.StreamReader(fs)
    44.             While rf.Peek() > -1
    45.                 Dim tmp As Abbr = GetAbbr(rf.ReadLine())
    46.                 If Not Object.Equals(tmp, Nothing) Then
    47.                     abbrs.Add(tmp)
    48.                 End If
    49.             End While
    50.             rf.Close()
    51.             fs.Close()
    52.         Catch ex As Exception
    53.             MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error!")
    54.         End Try
    55.     End Sub
    56.  
    57.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    58.         LoadData()
    59.     End Sub
    60. End Class
    Last edited by minitech; Mar 20th, 2009 at 08:32 PM.

  15. #15

    Thread Starter
    New Member
    Join Date
    Mar 2009
    Posts
    15

    Re: Replace Text

    This is great - how do the strings get saved in the file?

    ie. how does it know " acx " = " acromioclavicular "?




    QUOTE=minitech;3473993]Ok, here it is, the working code:

    vb.net Code:
    1. Public Class Form1
    2.     Structure Abbr
    3.         Dim Abbreviation As String
    4.         Dim Extended As String
    5.     End Structure
    6.     Dim abbrs As List(Of Abbr) = New List(Of Abbr)
    7.     Private Function GetAbbr(ByVal abbrS As String) As Abbr
    8.         Dim rt As Abbr
    9.         Dim cchr As Char
    10.         Dim i As Integer = 0
    11.         rt.Abbreviation = ""
    12.         While i < abbrS.Length
    13.             cchr = abbrS.Chars(i)
    14.             If cchr = " "c Then
    15.                 rt.Extended = abbrS.Substring(i + 1, abbrS.Length)
    16.                 If rt.Abbreviation = "" Then Return Nothing
    17.                 Return rt
    18.             End If
    19.             rt.Abbreviation &= cchr
    20.             i += 1
    21.         End While
    22.         Return Nothing
    23.     End Function
    24.     Private Sub txtMain_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMain.TextChanged
    25.         If Me.txtMain.Text.Chars(Me.txtMain.Text.Length - 1) <> " "c Then Exit Sub
    26.         Dim abbr As Abbr
    27.         For Each abbr In abbrs
    28.             ReplaceAbbr(abbr)
    29.         Next
    30.     End Sub
    31.     Private Sub ReplaceAbbr(ByVal abbr As Abbr)
    32.         Dim original As String = Me.txtMain.Text
    33.         Me.txtMain.Text = Me.txtMain.Text.Replace(abbr.Abbreviation, abbr.Extended)
    34.         If Not (Me.txtMain.Text = original) Then
    35.             Me.txtMain.Select(original.LastIndexOf(abbr.Abbreviation) + (abbr.Extended.Length - abbr.Abbreviation.Length), 0)
    36.         End If
    37.     End Sub
    38.     Private Sub LoadData()
    39.         Try
    40.             Dim fi As New IO.FileInfo("abbr_info.dat")
    41.             If Not fi.Exists Then Throw New IO.FileNotFoundException()
    42.             Dim fs As New IO.FileStream("abbr_info.dat", IO.FileMode.Open, IO.FileAccess.Read)
    43.             Dim rf As New IO.StreamReader(fs)
    44.             While rf.Peek() > -1
    45.                 Dim tmp As Abbr = GetAbbr(rf.ReadLine())
    46.                 If Not Object.Equals(tmp, Nothing) Then
    47.                     abbrs.Add(tmp)
    48.                 End If
    49.             End While
    50.             rf.Close()
    51.             fs.Close()
    52.         Catch ex As Exception
    53.             MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error!")
    54.         End Try
    55.     End Sub
    56.  
    57.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    58.         LoadData()
    59.     End Sub
    60. End Class
    [/QUOTE]

  16. #16
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Talking Re: Replace Text

    And a line-by-line:

    vb.net Code:
    1. Public Class Form1 'The class
    2.     Structure Abbr 'A structure, used to hold one abbreviation
    3.         Dim Abbreviation As String
    4.         Dim Extended As String
    5.     End Structure
    6.     Dim abbrs As List(Of Abbr) = New List(Of Abbr) 'A list of Abbr structures that holds all abbreviations
    7.     Private Function GetAbbr(ByVal abbrS As String) As Abbr 'Retrieves an Abbr object from a string by splitting it
    8.         Dim rt As Abbr 'The ReTurn value (an abbreviation object)
    9.         Dim cchr As Char 'Current CHaRacter
    10.         Dim i As Integer = 0 'Current Index
    11.         rt.Abbreviation = "" 'Set the abbreviation to "", just in case something goes wrong
    12.         While i < abbrS.Length 'While loop, loops through all chars inside passed string
    13.             cchr = abbrS.Chars(i) 'set current char
    14.             If cchr = " "c Then 'If it's a space...
    15.                 rt.Extended = abbrS.Substring(i + 1, abbrS.Length) 'Set the extended version to the rest of the string
    16.                 If rt.Abbreviation = "" Then Return Nothing 'If the space is the first char, return nothing
    17.                 Return rt 'otherwise, return the completed abbreviation.
    18.             End If
    19.             rt.Abbreviation &= cchr 'add the cchr to the current abbreviation
    20.             i += 1 'increment i
    21.         End While
    22.         Return Nothing 'If there was no space, return Nothing (it's the error value)
    23.     End Function
    24.     Private Sub txtMain_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMain.TextChanged
    25.         'Text changed
    26.         If Me.txtMain.Text.Chars(Me.txtMain.Text.Length - 1) <> " "c Then Exit Sub 'If they didn't just type a space, exit.
    27.         Dim abbr As Abbr 'Create another abbr object
    28.         For Each abbr In abbrs 'For Each loop; get all abbreviations in the list
    29.             ReplaceAbbr(abbr) 'Call ReplaceAbbr sub to replace the abbreviation with the extended version.
    30.         Next
    31.     End Sub
    32.     Private Sub ReplaceAbbr(ByVal abbr As Abbr)
    33.         Dim original As String = Me.txtMain.Text 'The original
    34.         Me.txtMain.Text = Me.txtMain.Text.Replace(abbr.Abbreviation, abbr.Extended) 'Replace all abbreviations
    35.         If Not (Me.txtMain.Text = original) Then 'If they're different then select the end of it
    36.             Me.txtMain.Select(original.LastIndexOf(abbr.Abbreviation) + (abbr.Extended.Length - abbr.Abbreviation.Length), 0)
    37.         End If
    38.     End Sub
    39.     Private Sub LoadData()
    40.         Try
    41.             Dim fi As New IO.FileInfo("abbr_info.dat")
    42.             If Not fi.Exists Then Throw New IO.FileNotFoundException() 'If it doesn't exist, tell the user.
    43.             Dim fs As New IO.FileStream("abbr_info.dat", IO.FileMode.Open, IO.FileAccess.Read) 'Create a file stream for reading
    44.             Dim rf As New IO.StreamReader(fs) 'set up an object to read from that stream
    45.             While rf.Peek() > -1 'While there's still text in the file
    46.                 Dim tmp As Abbr = GetAbbr(rf.ReadLine()) 'Get the abbreviation
    47.                 If Not Object.Equals(tmp, Nothing) Then 'If there wasn't an error...
    48.                     abbrs.Add(tmp) 'add the abbreviation to the list.
    49.                 End If
    50.             End While
    51.             rf.Close() 'Close the streams
    52.             fs.Close()
    53.         Catch ex As Exception 'If there was an error.
    54.             MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error!")
    55.         End Try
    56.     End Sub
    57.  
    58.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    59.         LoadData() 'Load the data
    60.     End Sub
    61. End Class

    Oh, and I just realized... if they're in the middle of the text, then it doesn't register a space. Just change the sub to handle a KeyPress, then execute the instructions if it was a space.

    Hope this helped!!!

    ___________________________________
    MINITECH, Beginning VB Programmer
    Remember to rate helpful posts.

  17. #17
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Replace Text

    And for your last question...
    If you don't want to type it out ,use a FileStream with mode set to "Append" with a StreamWriter, like this:
    Code:
    Dim fs As New IO.FileStream("abbr_info.dat",IO.FileMode.Append,IO.FileAccess.Write)
    Dim sw As New IO.StreamWriter(fs)
    sw.WriteLine()
    sw.Write(abbreviation & " ")
    sw.Write(extended)
    sw.Close()
    fs.Close()
    Attached is an example file. (ext. is .txt, change to .dat.)
    Attached Files Attached Files
    Last edited by minitech; Mar 18th, 2009 at 04:00 PM.

  18. #18

    Thread Starter
    New Member
    Join Date
    Mar 2009
    Posts
    15

    Re: Replace Text

    so the structure of the .dat file is parsed with a , or tab or space?

    Quote Originally Posted by minitech View Post
    And for your last question...
    If you don't want to type it out ,use a FileStream with mode set to "Append" with a StreamWriter, like this:
    Code:
    Dim fs As New IO.FileStream("abbr_info.dat",IO.FileMode.Append,IO.FileAccess.Write)
    Dim sw As New IO.StreamWriter(fs)
    sw.WriteLine()
    sw.Write(abbreviation & " ")
    sw.Write(extended)
    sw.Close()
    fs.Close()

  19. #19
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Replace Text

    No, look at my attachment on my last post (just edited at the same time as you posted.)
    It can only be separated with a space.

  20. #20

    Thread Starter
    New Member
    Join Date
    Mar 2009
    Posts
    15

    Re: Replace Text

    Hmm. Error - Index and length must refer to a location within the string. Parameter name: Length

    Did you come across that one?


    Quote Originally Posted by minitech View Post
    No, look at my attachment on my last post (just edited at the same time as you posted.)
    It can only be separated with a space.

  21. #21

    Thread Starter
    New Member
    Join Date
    Mar 2009
    Posts
    15

    Re: Replace Text

    I am at a loss right now, maybe I will come back later with fresh eyes and see if it will work - thanks for all of your help and effort. it is truly appreciated!!!!!!!!!

    Quote Originally Posted by cmalloy View Post
    Hmm. Error - Index and length must refer to a location within the string. Parameter name: Length

    Did you come across that one?

  22. #22
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Talking Re: Replace Text

    Sorry, I've got some updated code that fixes that error. (It also fixes another one.)
    vb.net Code:
    1. Public Class Form1
    2.     Structure Abbr
    3.         Dim Abbreviation As String
    4.         Dim Extended As String
    5.     End Structure
    6.     Dim abbrs As List(Of Abbr) = New List(Of Abbr)
    7.     Private Function GetAbbr(ByVal abbrS As String) As Abbr
    8.         Dim rt As Abbr
    9.         Dim cchr As Char
    10.         Dim i As Integer = 0
    11.         rt.Abbreviation = ""
    12.         While i < abbrS.Length
    13.             cchr = abbrS.Chars(i)
    14.             If cchr = " "c Then
    15.                 rt.Extended = abbrS.Substring(i + 1, abbrS.Length - (i + 1))
    16.                 If rt.Abbreviation = "" Then Return Nothing
    17.                 Return rt
    18.             End If
    19.             rt.Abbreviation &= cchr
    20.             i += 1
    21.         End While
    22.         Return Nothing
    23.     End Function
    24.     Private Sub txtMain_KeyPress(ByVal sender As System.Object, ByVal e As KeyPressEventArgs) Handles txtMain.KeyPress
    25.         If e.KeyChar <> " "c Then Exit Sub
    26.         Dim abbr As Abbr
    27.         For Each abbr In abbrs
    28.             ReplaceAbbr(abbr)
    29.         Next
    30.     End Sub
    31.     Private Sub ReplaceAbbr(ByVal abbr As Abbr)
    32.         Dim original As String = Me.txtMain.Text
    33.         Me.txtMain.Text = Me.txtMain.Text.Replace(abbr.Abbreviation, abbr.Extended)
    34.         If Not (Me.txtMain.Text = original) Then
    35.             Me.txtMain.Select(original.LastIndexOf(abbr.Abbreviation) + abbr.Extended.Length + IIf(Me.txtMain.Text.Chars(original.LastIndexOf(abbr.Abbreviation) + abbr.Extended.Length) = " "c, 1, 0), 0)
    36.         End If
    37.     End Sub
    38.     Private Sub LoadData()
    39.         Try
    40.             Dim fi As New IO.FileInfo("abbr_info.dat")
    41.             If Not fi.Exists Then Throw New IO.FileNotFoundException()
    42.             Dim fs As New IO.FileStream("abbr_info.dat", IO.FileMode.Open, IO.FileAccess.Read)
    43.             Dim rf As New IO.StreamReader(fs)
    44.             While rf.Peek() > -1
    45.                 Dim tmp As Abbr = GetAbbr(rf.ReadLine())
    46.                 If Not Object.Equals(tmp, Nothing) Then
    47.                     abbrs.Add(tmp)
    48.                 End If
    49.             End While
    50.             rf.Close()
    51.             fs.Close()
    52.         Catch ex As Exception
    53.             MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error!")
    54.         End Try
    55.     End Sub
    56.  
    57.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    58.         LoadData()
    59.     End Sub
    60. End Class
    Last edited by minitech; Mar 20th, 2009 at 08:39 PM. Reason: Better logic req.

  23. #23

    Thread Starter
    New Member
    Join Date
    Mar 2009
    Posts
    15

    Resolved Re: Replace Text

    Works like a charm! Thanks!


    Quote Originally Posted by minitech View Post
    Sorry, I've got some updated code that fixes that error. (It also fixes another one.)
    vb.net Code:
    1. Public Class Form1
    2.     Structure Abbr
    3.         Dim Abbreviation As String
    4.         Dim Extended As String
    5.     End Structure
    6.     Dim abbrs As List(Of Abbr) = New List(Of Abbr)
    7.     Private Function GetAbbr(ByVal abbrS As String) As Abbr
    8.         Dim rt As Abbr
    9.         Dim cchr As Char
    10.         Dim i As Integer = 0
    11.         rt.Abbreviation = ""
    12.         While i < abbrS.Length
    13.             cchr = abbrS.Chars(i)
    14.             If cchr = " "c Then
    15.                 rt.Extended = abbrS.Substring(i + 1, abbrS.Length - (i + 1))
    16.                 If rt.Abbreviation = "" Then Return Nothing
    17.                 Return rt
    18.             End If
    19.             rt.Abbreviation &= cchr
    20.             i += 1
    21.         End While
    22.         Return Nothing
    23.     End Function
    24.     Private Sub txtMain_KeyPress(ByVal sender As System.Object, ByVal e As KeyPressEventArgs) Handles txtMain.KeyPress
    25.         If e.KeyChar <> " "c Then Exit Sub
    26.         Dim abbr As Abbr
    27.         For Each abbr In abbrs
    28.             ReplaceAbbr(abbr)
    29.         Next
    30.     End Sub
    31.     Private Sub ReplaceAbbr(ByVal abbr As Abbr)
    32.         Dim original As String = Me.txtMain.Text
    33.         Me.txtMain.Text = Me.txtMain.Text.Replace(abbr.Abbreviation, abbr.Extended)
    34.         If Not (Me.txtMain.Text = original) Then
    35.             Me.txtMain.Select(original.LastIndexOf(abbr.Abbreviation) + abbr.Extended.Length + IIf(Me.txtMain.Text.Chars(original.LastIndexOf(abbr.Abbreviation) + abbr.Extended.Length) = " "c, 1, 0), 0)
    36.         End If
    37.     End Sub
    38.     Private Sub LoadData()
    39.         Try
    40.             Dim fi As New IO.FileInfo("abbr_info.dat")
    41.             If Not fi.Exists Then Throw New IO.FileNotFoundException()
    42.             Dim fs As New IO.FileStream("abbr_info.dat", IO.FileMode.Open, IO.FileAccess.Read)
    43.             Dim rf As New IO.StreamReader(fs)
    44.             While rf.Peek() > -1
    45.                 Dim tmp As Abbr = GetAbbr(rf.ReadLine())
    46.                 If Not Object.Equals(tmp, Nothing) Then
    47.                     abbrs.Add(tmp)
    48.                 End If
    49.             End While
    50.             rf.Close()
    51.             fs.Close()
    52.         Catch ex As Exception
    53.             MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error!")
    54.         End Try
    55.     End Sub
    56.  
    57.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    58.         LoadData()
    59.     End Sub
    60. End Class

  24. #24
    New Member
    Join Date
    Jun 2009
    Posts
    5

    Re: Replace Text

    So is it resolved

Tags for this Thread

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