Platforms to show: All Mac Windows Linux Cross-Platform
/MacControls/TokenField/NSTokenFieldControl
Required plugins for this example: MBS MacBase Plugin, MBS MacCocoa Plugin, MBS MacControls Plugin, MBS Main Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /MacControls/TokenField/NSTokenFieldControl
This example is the version from Fri, 22th Oct 2015.
Project "NSTokenFieldControl.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class TestWindow Inherits Window
Control Listbox1 Inherits Listbox
ControlInstance Listbox1 Inherits Listbox
End Control
Control BevelButton1 Inherits BevelButton
ControlInstance BevelButton1 Inherits BevelButton
EventHandler Sub Action()
MsgBox n.StringValue
End EventHandler
End Control
Control BevelButton2 Inherits BevelButton
ControlInstance BevelButton2 Inherits BevelButton
EventHandler Sub Action()
dim v() as Variant = n.objects
dim a() as string
for each vv as Variant in v
a.Append vv
next
MsgBox Join(a,EndOfLine)
End EventHandler
End Control
Control BevelButton3 Inherits BevelButton
ControlInstance BevelButton3 Inherits BevelButton
EventHandler Sub Action()
dim v() as Variant
v.Append 2
v.Append 4
n.setObjects v
End EventHandler
End Control
Control BevelButton4 Inherits BevelButton
ControlInstance BevelButton4 Inherits BevelButton
EventHandler Sub Action()
n.StringValue = ""
End EventHandler
End Control
Control tokenfield Inherits MyNSTokenField
ControlInstance tokenfield Inherits MyNSTokenField
EventHandler Sub Open()
n = me.View
n.tokenStyle = n.NSRoundedTokenStyle
// tell it how the resize is going
n.autoresizingMask=n.NSViewHeightSizable+n.NSViewWidthSizable
End EventHandler
End Control
EventHandler Sub Open()
if not TargetCocoa then
MsgBox "This example requires Mac OS X 10.5 and Cocoa."
quit
end if
End EventHandler
Property n As NSTokenFieldMBS
End Class
MenuBar MenuBar1
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem UntitledMenu0 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
Class MyNSTokenField Inherits NSTokenFieldControlMBS
EventHandler Function completionsForSubstring(substring as string, tokenIndex as integer, byref selectedIndex as integer) As variant()
dim answers() as Variant
dim list as listbox = TestWindow.Listbox1
dim u as integer = list.ListCount-1
for i as integer = 0 to u
if instr(list.Cell(i,1), substring)>0 or instr(list.Cell(i,2), substring)>0 then
answers.Append list.Cell(i,2)
end if
next
Return answers
End EventHandler
EventHandler Function displayStringForRepresentedObject(representedObject as variant) As string
dim id as string = representedObject
dim list as listbox = TestWindow.Listbox1
dim u as integer = list.ListCount-1
for i as integer = 0 to u
if list.Cell(i,0) = id then
Return list.Cell(i,1)
end if
next
End EventHandler
EventHandler Function editingStringForRepresentedObject(representedObject as variant) As string
dim id as string = representedObject
dim list as listbox = TestWindow.Listbox1
dim u as integer = list.ListCount-1
for i as integer = 0 to u
if list.Cell(i,0) = id then
Return list.Cell(i,2)
end if
next
End EventHandler
EventHandler Function hasMenuForRepresentedObject(representedObject as variant) As boolean
Return representedObject.IntegerValue = 1
End EventHandler
EventHandler Function menuForRepresentedObject(representedObject as variant) As NSMenuMBS
if representedObject.IntegerValue = 1 then
LastMenu = new NSMenuMBS
LastMenuItem = new MyNSMenuItemMBS
LastMenuItem.CreateMenuItem "Hello World"
LastMenu.addItem LastMenuItem
Return LastMenu
end if
End EventHandler
EventHandler Function representedObjectForEditingString(editingString as string) As variant
dim list as listbox = TestWindow.Listbox1
dim u as integer = list.ListCount-1
for i as integer = 0 to u
if list.Cell(i,1) = editingString or list.Cell(i,2) = editingString then
Return list.Cell(i,0)
end if
next
End EventHandler
EventHandler Function shouldAddObjects(tokens() as variant, index as Integer) As variant()
// add all
Return tokens
End EventHandler
EventHandler Function textShouldBeginEditing(fieldEditor as NSTextMBS) As boolean
// this event does nothing in NSTokenFieldMBS
End EventHandler
EventHandler Function textShouldEndEditing(fieldEditor as NSTextMBS) As boolean
// this event does nothing in NSTokenFieldMBS
End EventHandler
EventHandler Sub tokenFieldAction()
System.DebugLog CurrentMethodName
End EventHandler
EventHandler Function tokenFieldTextShouldBeginEditing(fieldEditor as NSTextMBS) As boolean
System.DebugLog CurrentMethodName
Return true
End EventHandler
EventHandler Function tokenFieldTextShouldEndEditing(fieldEditor as NSTextMBS) As boolean
System.DebugLog CurrentMethodName
Return true
End EventHandler
Property LastMenu As NSMenuMBS
Property LastMenuItem As MyNSMenuItemMBS
End Class
Class MyNSMenuItemMBS Inherits NSMenuItemMBS
EventHandler Sub Action()
MsgBox "You selected menu item."
End EventHandler
End Class
End Project
The items on this page are in the following plugins: MBS MacControls Plugin.