List Boxes - Elements - Hag GUI for Cobra
List boxes are large rectangular elements containing a list of options. Each option can be individually styled or disabled.
As options can extend beyond the height of the box, Hag list box elements automatically create and manage a scrollbar element.
Listboxes expose the following read-only variables:
listbox.x: Integer - Listbox x position
listbox.y: Integer - Listbox y position
listbox.w: Integer - Listbox width
listbox.h: Integer - Listbox height
listbox.enabled: Boolean - If listbox is enabled
listbox.visible: Boolean - If listbox is visible
listbox.gotUpdated: Boolean - If listbox got updated
For a ready-to-run example, see Example_Listbox in the Examples download.
Example:
Uses
hagC2D,
cobra2D,
keyset
Var
frmMain: ^forms
lbxMain: ^listBoxes
btnBold, btnAdd, btnClear, btnClose: ^buttons
lopHandle: ^listOptions
background: Element
i: Integer = 1
Begin
SetAppName("ListBox example")
OpenScreen(320, 240, 32, FALSE, COB_SHOWBORDER+COB_SHOWCLOSE)
background = CreateSprite(320,240)
Cls(ToRGBA(235,233,237),background) ; Flip
HagBaseDir("hag\")
HagInit(320, 240, 1)
HagEnableDebugLog()
HagLoadGuiTheme("xp")
frmMain = CreateForm()
lbxMain = CreateListBox(frmMain, 20, 20, 280, 180)
btnAdd = CreateButton(frmMain, 130, 207, 50, "Add")
btnClear = CreateButton(frmMain, 190, 207, 50, "Clear")
btnClose = CreateButton(frmMain, 250, 207, 50, "Close")
btnBold = CreateButton(frmMain, 70, 207, 50, "Bold")
ListBoxAddOption(lbxMain, "Click on an option to select it")
ListBoxAddOption(lbxMain, "Use 'Bold' button to bold selected option")
lopHandle = ListBoxAddOption(lbxMain, "This option has a custom style & color")
ListOptionFontStyle(lbxMain, lopHandle, 2) // Itallic
ListOptionFontColor(lbxMain, lopHandle, ToRGBA(0,0,255))
lopHandle = ListBoxAddOption(lbxMain, "This option is disabled")
EnableListOption(lbxMain, lopHandle, FALSE)
ListBoxAddOption(lbxMain, "Click 'Add' to add some more options")
ListBoxAddOption(lbxMain, "Click 'Clear' to remove all options")
ListBoxAddOption(lbxMain, "This is another listbox option")
ListBoxAddOption(lbxMain, "This is yet another listbox option")
ListBoxAddOption(lbxMain, "This is a further listbox option")
ListBoxAddOption(lbxMain, "This is another listbox option")
ListBoxAddOption(lbxMain, "This is an additional listbox option")
ListBoxAddOption(lbxMain, "This is another listbox option")
ListBoxAddOption(lbxMain, "This is one more listbox option")
FocusButton(btnClose)
HagUpdateOnce()
While ExitRequested = FALSE
HagUpdateAll_AutoKeys()
If ButtonClicked(btnBold) then
If ListBoxSelectionExists(lbxMain) then
lopHandle = ListBoxSelectionHandle(lbxMain)
ListOptionFontStyle(lbxMain, lopHandle, 1)
Else
MessageBox("No option is selected - can't bold.", "Notice")
Endif
Endif
If ButtonClicked(btnAdd) then
ListBoxAddOption(lbxMain, "Another listbox option ("+i+")")
Inc(i)
Endif
If ButtonClicked(btnClear) then
ListBoxClearOptions(lbxMain)
i = 1
Endif
If ButtonClicked(btnClose) then RequestExit
If KeyHits(VK_ESCAPE) > 0 then RequestExit
Flip
Pause(1)
Wend
HagFreeAll()
CloseScreen()
End
Stay Subscribed
RSS feed or get updates by e-mail.
, subscribe to theYou can also contact me directly - I make an effort to reply to every e-mail.