Results 1 to 6 of 6

Thread: RichTextBox: Loop Line by Line Looking for Criteria and Replace Certain Line

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 1999
    Posts
    106

    Question RichTextBox: Loop Line by Line Looking for Criteria and Replace Certain Line

    Hi everyone,

    Need some guidance on best way to loop through a RichTextBox line by line, and if the line meets a certain criteria format like “[PatientRecord###], then replace that line’s text with new [PatientRecord###].”
    The issue now is that several Patient Record numbers are the SAME – for example, we have three [PatientRecord497]. Therefore, that limits the usefulness of an GetPrivateProfile API call which will grab only the first matching entry or using a generic Replace$ on the RichTextBox that again will only replace the first matching entry. So basically, thinking of maybe using almost a brute force approach that sets up a counter, starts at the beginning of the RichTextBox, and when it matches a “[PatientRecord###]” format force the first match to be “[PatientRecord001]”, increment the counter, and then the next match to be “[PatientRecord002]”, etc. Any sample code or logic advice greatly appreciated!

  2. #2
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,144

    Re: RichTextBox: Loop Line by Line Looking for Criteria and Replace Certain Line

    You could always split your RTB text into an array (of patientrecords--I'm assuming there must be some sort of delimiter, even if it is a vbLf) , and then loop through the array to remove duplicates.

  3. #3
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: RichTextBox: Loop Line by Line Looking for Criteria and Replace Certain Line

    This smells like somebody is desperately trying to use an INI file in place of a database. This is a very poor idea.

    Even then I'm not sure where a RichTextBox makes sense as part of a proposed solution. That sounds really off-the-wall.

    However given these requirements (assuming an INI-format file is really involved here) this probably isn't as simple as removing duplicates. It sounds like the goal is to find duplicated numbers and assign new unique numbers to those "patients."


    I ginned up a dummy file to work with and did this using a RichTextBox. About the only value added by a RichTextBox was the ability to colorize things to see what is going on.


    Name:  sshot1.png
Views: 627
Size:  5.9 KB

    Lines with duplicate Patient numbers in red


    Name:  sshot2.png
Views: 505
Size:  6.1 KB

    First of each duplicate in blue, renumbered duplicates in green


    No attempt is made to sort the results. This kind of thing would be far better accomplished using a database instead of an INI file. But maybe I'm just making assumptions about the data that aren't true. We haven't been told much, after all.
    Attached Files Attached Files

  4. #4
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,144

    Re: RichTextBox: Loop Line by Line Looking for Criteria and Replace Certain Line

    If I recall, dile....you like cats,....so I won't say there are many ways to ski......well, you know. Yeh, most of the time first OPS are difficult to determine what one wants, what one has done, and what one knows.

  5. #5
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: RichTextBox: Loop Line by Line Looking for Criteria and Replace Certain Line

    Even if you used a String array 90% of what is required is the same.

    He didn't say he wanted to discard duplicates, he wanted them renumbered uniquely as far as I could tell. And if these are INI files you have to avoid stepping on the "string" values within each Patient "section" of the INI.

    But INI files are not meant to be updated very often or ever get very large. That's just very poor practice.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Oct 1999
    Posts
    106

    Re: RichTextBox: Loop Line by Line Looking for Criteria and Replace Certain Line

    Gents, sorry for the delay and thanks very much for the replies. Unfortunately, I don’t have control over the design or why the original developer used .INI which is why I specifically scoped my question to the RichTextbox and .INI files. Dilettante, as usual you are always extremely helpful in the most thoughtful and practical ways. I tried your solution but oddly enough it didn’t work out for me but really appreciated the strawman to test out. Sam, although familiar with the split array method as you proposed - I didn’t think I’d end up using that. But it did end up that way. I basically resolved by extracting the data from the RTB into an array and then in fact looping through the array replacing elements as needed. Thanks for all the input, all set gents!

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