Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NGUI
#6
An example of ComboBox and ListBox, two ways of letting the user select stuff from lists.

Code:
' NGUI - ComboBox and ListBox
' ---------------------------

#win32

' This library is really large, so including it increases the compile time quite a bit.
include "ngui.n7"

' These widgets will be created later.
visible fruitLabel, stateLabel, languageLabel

' Create a resizable window.
set window "NGUI - ComboBox and ListBox", 640, 480, false, 0
set redraw off


' Ngui uses containers to arrange widgets in different ways. The containers are widgets too.
' Every window needs a root widget, and it's usually a container. Here I create a hbox (horizontal
' box), that aranges its children horizontally. This will be the root widget of the window.
windowRoot = HBox(SIZE_EXPAND, SIZE_EXPAND)
windowRoot.SetPadding(4) ' Some distance to the window border.
windowRoot.SetSpacing(8) ' Spacing between children.


' Create a vbox for a combo box and a label.
vbox = VBox(SIZE_EXPAND, SIZE_EXPAND)
windowRoot.Add(vbox)
vbox.SetHalign(ALIGN_CENTER)
vbox.SetSpacing(16)
' Create a combo box. For an unknown reason its width is the first parameter. And for some other
' unknown reason (probably I was in a rush) SIZE_EXPAND doesn't work, so let's use a fixed with of
' 88 pixels. The second parameter is the content that will be shown for selection in a drop down
' menu. The third parameter is a callback function defined later.
cb = ComboBox(88, ["Apple", "Banana", "Pear", "Blue car"], ComboBoxCallback)
' If nothing has been selected, the default text is "Select". We can change the default text with
' SetDefaultText.
cb.SetDefaultText("Select fruit")
vbox.Add(cb)
' Create a label that will be modified in ComboBoxCallback.
fruitLabel = Label("", SIZE_AUTO, SIZE_AUTO)
vbox.Add(fruitLabel)


' Create a vbox for a list box with a scrollbar and a label.
vbox = VBox(SIZE_EXPAND, SIZE_EXPAND)
windowRoot.Add(vbox)
vbox.SetHalign(ALIGN_CENTER)
vbox.SetSpacing(16)
' We can connect a vertical scrollbar to a list box. Therefor we create a hbox for both these
' widgets.
hbox = HBox(SIZE_AUTO, SIZE_EXPAND)
vbox.Add(hbox)
' A list of items for our list box.
items = ["Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut",
        "Delaware", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa",
        "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan",
        "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire",
        "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio",
        "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota",
        "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia",
        "Wisconsin", "Wyoming"]
' The first parameter to a list box is its content, and the fourth is a callback function.
lb = ListBox(items, SIZE_AUTO, SIZE_EXPAND, ListBoxCallback)
hbox.Add(lb)
' Create and add a scrollbar to the hbox. The first parameter to VerticalScrollbar is the widget to
' which we want to connect it. A widget needs to actually support a scrollbar for this to work, and
' list boxes do.
hbox.Add(VerticalScrollbar(lb, SIZE_AUTO, SIZE_EXPAND))
' Create a label that will be modified in ListBoxCallback.
stateLabel = Label("", SIZE_AUTO, SIZE_AUTO)
vbox.Add(stateLabel)


' Create a vbox for another combo box and a label.
vbox = VBox(SIZE_EXPAND, SIZE_AUTO)
windowRoot.Add(vbox)
vbox.SetHalign(ALIGN_CENTER)
vbox.SetSpacing(16)
' The list use for this combobox will be one of tables.
languages = [
    [name: "N7", author: "Marcus"],
    [name: "micro(A)", author: "Aurel"],
    [name: "Brainfuck", author: "Urban"],
    [name: "FLOW-MATIC", author: "Grace"]]
' Create the combobox and put it in the vbox-   
cb = ComboBox(88, languages, SecondComboBoxCallback)
vbox.Add(cb)
' When dealing with a list of tables we have to tell the combo box which field to display in the
' list.
cb.SetDisplayField("name")
' Create a label that will be modified in SecondComboBoxCallback.
languageLabel = Label("", SIZE_AUTO, SIZE_AUTO)
vbox.Add(languageLabel)
languageLabel.SetHalign(ALIGN_CENTER)


' Enter main loop with windowRoot as root widget. From here on all action goes through the callback
' functions.
EnterMainLoop(windowRoot)


' Callback for first combo box.
function ComboBoxCallback(comboBox, selectedIndex)
    fruitLabel.SetText(comboBox.GetSelectedItem())
endfunc

' Callback for list box.
function ListBoxCallback(listBox, selectedIndexes)
    ' A list box actually supports selection of multiple items. If enabled, selectedIndexes will be
    ' an array of indexes, but now it's just a number.
    stateLabel.SetText(listBox.GetItems()[selectedIndexes])
endfunc

' Callback for second combo box.
function SecondComboBoxCallback(comboBox, selectedIndex)
    item = comboBox.GetSelectedItem()
    languageLabel.SetText(item.name + chr(10) + "by" + chr(10) + item.author)
endfunc


Attached Files
.n7   combobox_and_listbox.n7 (Size: 4.92 KB / Downloads: 5)
Reply


Messages In This Thread
NGUI - by Marcus - 11-19-2022, 07:01 AM
RE: NGUI - by Marcus - 11-19-2022, 08:27 AM
RE: NGUI - by Marcus - 11-19-2022, 11:09 AM
RE: NGUI - by Marcus - 11-19-2022, 01:52 PM
RE: NGUI - by johnno1956 - 11-20-2022, 03:48 AM
RE: NGUI - by Marcus - 11-26-2022, 08:24 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)