I think, netizens who have done text processing must not be unfamiliar with the RichtextBox control. This is a editing display that encapsulates almost all RTF documents. We can write a powerful text editor based on this control. Maybe you will think that all the features of the RichtextBox control have long been implemented under M $ Word, and the people's Dongdong do so cool, why bother yourself to develop a text editor? In fact, we write procedures are not all for practicality, by doing a program, even just a simple calculator, you can learn a lot of knowledge. What's more, you can use the RichtextBox control to implement a word no function - put a specific text to make any changes to it before unlock ~ Oh, have you tried? You will be very satisfied with this feature, because RichtextBox can prevent users from making any changes to the locked text, and you don't need to add any excess code (at least most cases). But regardless of the function of the control, its IQ is still limited, such as when you try to find the replacement text function in your software, Richtextbox's stay in the brain is exposed - once attempted the locked text Replace, the file pointer will be forcibly returned on the previous unlocked character of the locked text, then continue the next step, continue to return again ... so your program is in this dead cycle ~ Oh, What is the urgency of the people ... It doesn't matter. In fact, we must only teach RichtextBox to let it know how to intelligently handle the replacement of locking text. Our goal is to allow RichtextBox's protected text to not be replaced and guarantee that the pointer is moving smoothly and notifies the user after the replacement is completed, and how many texts have been replaced and cannot be replaced. Here is the solution I have, you can refer to it, efficiency is not very high, but it is very intuitive.
If MyReplaceForm.ShowDialog = DialogResult.OK Then 'record these two variables to be replaced and the replacement text into text Dim FindText As String = MyReplaceForm.TextBoxFind.Text Dim ReplaceText As String = MyReplaceForm.TextBoxReplace.Text' This variable Marking Whether to find a locked text Dim protectedthistimefound as boolean 'This variable is used to mark the number of DIM Replacecount as integer' This variable is used to mark the amount of protected text. Dim protectedcount as integer 'is replaced in the document Moved pointer DIM NEXTPOS AS INTEGER DIM PROBEPOS AS INTEGER DIM I AS Integer with RichtextBox if .find (Findtext, FindStyle) = -1 Then MessageBox.show ("Did not find the content to be replaced!", "Replace Fail", _ MessageBoxButtons.OK, MessageBoxIcon.Exclamation) MyReplaceForm.TextBoxFind.Focus () Else If FindText = ReplaceText Then MessageBox.Show ( "doing the replacement makes no sense!", "refused to replace", _ MessageBoxButtons.OK, MessageBoxIcon.Exclamation ) MyReplaceForm.TextBoxReplace.focus () Else 'points pointer to the beginning nextpos = 0 Do 'is positioned to the first to be replaced,' FindStyle is a global variable of RichtextBoxFinds, which is recorded.
NextPos = .Find (FindText, NextPos, FindStyle) 'started using probe pointer, determines whether each character is eleven locked ProbePos = NextPos ProtectedThisTimeFound = False For i = 0 To FindText.Length - 1 ProbePos = .Find (FindText.Substring (i, 1), _ probepos, richtextboxfinds.none) if .selectionprotected = true kiln 'Look at whether the current lookup character is locked? If you find it, protectedthistimefound = true' is discovered! ProtectedCount = 1 'count if probepos findtext.length - i> _ richtextbox.textLength - 1 Then' If this string is the end of the document EXIT Do Else 'is not wrong nextpos = (FindText.length - i) EXIT for end if End If Next i If ProtectedThisTimeFound = False Then 'if the detection loop is not locked down the string found nextPos = .Find (FindText, nextPos, FindStyle) .SelectedText = ReplaceText ReplaceCount = 1' count End If loop Until .Find (FindText NextPOS, FindStyle = -1 if pro TECTEDCOUNT =