Discussion:
How to show TextCtrl selection when it isn't focused?
Nathan McCorkle
2014-10-06 23:57:42 UTC
Permalink
I just implemented FindReplaceDialog for my TextCtrl, but if the Find
dialog has focus, you can't see the text selected with SetSelection. I
would think the selection would change from blue to grey or something,
rather than just not appear.

Code altered from the demo:

def OnSearch(self, event):
self.findData = wx.FindReplaceData()
self.foundIndex = None
self.lastString = None
dlg = wx.FindReplaceDialog(self, self.findData, "Find")
self.BindFindEvents(dlg)
dlg.Show(True)

def BindFindEvents(self, win):
win.Bind(wx.EVT_FIND, self.OnFind)
win.Bind(wx.EVT_FIND_NEXT, self.OnFind)
win.Bind(wx.EVT_FIND_REPLACE, self.OnFind)
win.Bind(wx.EVT_FIND_REPLACE_ALL, self.OnFind)
win.Bind(wx.EVT_FIND_CLOSE, self.OnFindClose)

def OnFind(self, evt):
#print type(evt)
#print dir(evt)
#print repr(evt.GetFindString()), repr(self.findData.GetFindString())
eventMap = {
wx.wxEVT_COMMAND_FIND : "FIND",
wx.wxEVT_COMMAND_FIND_NEXT : "FIND_NEXT",
wx.wxEVT_COMMAND_FIND_REPLACE : "REPLACE",
wx.wxEVT_COMMAND_FIND_REPLACE_ALL : "REPLACE_ALL",
}

et = evt.GetEventType()

if et in eventMap:
evtType = eventMap[et]
else:
evtType = "**Unknown Event Type**"

if et in [wx.wxEVT_COMMAND_FIND_REPLACE, wx.wxEVT_COMMAND_FIND_REPLACE_ALL]:
replaceTxt = "Replace text: %s" % evt.GetReplaceString()
else:
replaceTxt = ""

#print ("-i- %s -- Find text: %s %s Flags: %d \n" %
# (evtType, evt.GetFindString(), replaceTxt, evt.GetFlags()))

#if evtType == 'FIND':
if self.foundIndex == None or self.lastString != evt.GetString():
self.foundIndex = self.findFirstTime(evt.GetString())
else: #an index had already been found, so we need to start searching from
that point
foundIndex = self.findAgain(evt.GetString(), self.foundIndex+1)
#only set a new self.foundIndex if foundIndex isn't None
if foundIndex:
self.foundIndex = foundIndex

if self.foundIndex:
self.lastString = evt.GetString()
self.cmdLogTextBox.ShowPosition(self.foundIndex)
self.cmdLogTextBox.SetSelection(self.foundIndex, self.foundIndex +
len(evt.GetString()))
self.cmdLogTextBox.SetFocus()

def OnFindClose(self, evt):
#print "-i- FindReplaceDialog closing...\n"
evt.GetDialog().Destroy()

def findFirstTime(self, stringToFind):
try:
return self.cmdLogTextBox.GetValue().index(stringToFind)
except ValueError:
return None

def findAgain(self, stringToFind, indexToStartSearchingAt):
try:
return self.cmdLogTextBox.GetValue().index(stringToFind,
indexToStartSearchingAt)
except ValueError:
return None
--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...