I have a TextCtrl which contains various log data, I also have a EditText field whereby the user can search for a string to find, and then click the Find button to locate and highlight the word if it is found in the logs. Your standard Find/highlight in browser/Notepad etc.
The code I have does already work and successfully highlights the user's word, however there are a couple of bits missing that I would like to implement:
The ability to search the same word, and have the next word highlighted e.g a 'Find Next'EDIT: This was solved by adding in a 'Find Next' button with the below code. The count limits the next highlight to 1 word rather than all of them to the end of the log.- Un-highlight the current word when a new word is searched, be it the same word, or a new word
Start the position at 0 (top of the data) if searching a new wordEDIT: solved by resetting the startPos value inside thefindTxt
defdef findTxt(self,e): global wordPos newstring = self.logTxt.GetValue() for i in range(self.progressBox.GetNumberOfLines()): line = self.progressBox.GetLineText(i) if newstring in line: startPos = self.progressBox.GetValue().find(newstring) endPos = startPos + len(newstring) wordPos = endPos self.progressBox.Bind(wx.EVT_SET_FOCUS, self.highlightText(startPos, endPos)) startPos = 0 self.findNextBtn.Enable() def findNext(self,e): global wordPos newstring = self.logTxt.GetValue() count = 0 for i in range(self.progressBox.GetNumberOfLines()): if count == 0: line = self.progressBox.GetValue() if newstring in line: startPos = self.progressBox.GetValue().find(newstring, wordPos) endPos = startPos + len(newstring) wordPos = endPos self.progressBox.Bind(wx.EVT_SET_FOCUS, self.highlightText(startPos, endPos)) count = 1 def highlightText(self, pos, size): self.progressBox.SetStyle(pos, size, wx.TextAttr("black", "turquoise")) self.progressBox.SetInsertionPoint(pos)