Select All for NSSearchFieldMBS

I have been using NSSearchFieldMBS in the toolbar for years:

  SearchField = New NSToolbarSearchField(self.getWindowName)
  SearchFieldItem = New NSToolbarItemMBS(kSearch)
  SearchFieldItem.paletteLabel = kSearch
  SearchFieldItem.view = SearchField
  
  dim theToolbar as NSToolbarMBS = self.NSToolbarMBS
  dim items() as NSToolbarItemMBS = theToolbar.items
  
  for each currentItem as NSToolbarItemMBS in items
    if currentItem.label = "Search" then
      currentItem.label = kSearch
      currentItem.view = SearchField
      currentItem.maxSize = NSMakeSizeMBS(300, SearchFieldExtras.MainToolbarNSSearchFieldHeight)
      currentItem.minSize = NSMakeSizeMBS(150, SearchFieldExtras.MainToolbarNSSearchFieldHeight)
    end if
  next

How do I get the search field to react to “Select All”? I’ve added a menu handler for EditSelectAll. But the menu handler doesn’t fire. I have some other "Select All"s where I use a trick of getting self.focus so that I know which control should react. But for the search field the focus is always nil.

MacOS Ventura 13.3, Xojo 2022r3 and MBS 23.0.

try this… I hope it works

dim items() as NSToolbarItemMBS = self.NSToolbarMBS.items

for each currentItem as NSToolbarItemMBS in items
  if currentItem isa NSToolbarItemMBS and currentItem.ItemIdentifier = "searchfield" then
    dim textField as NSTextFieldMBS = NSTextFieldMBS(currentItem.view)
    if textField <> nil then 
      textField.stringValue = "New text"
      textField.selectText
    end
  end
next

Thanks, I’ll try that. But I don’t know when the searchfield has the focus.

better approach :smiley:

dim items() as NSToolbarItemMBS = self.NSToolbarMBS.items
dim textField as NSTextFieldMBS
dim textFieldFocused as Boolean

for each currentItem as NSToolbarItemMBS in Items
  if currentItem isa NSToolbarItemMBS and currentItem.ItemIdentifier = "searchfield" then
    textField = NSTextFieldMBS(currentItem.view)
    if textField <> nil then
      textFieldFocused = self.NSWindowMBS.firstResponder = textField.currentEditor
    end
  end
next

if textField <> nil and textFieldFocused then textField.selectText
1 Like