GuiGridListGetSelectedItem: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
m (Changed syntax)
 
(5 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{Client function}}
{{Client function}}
__NOTOC__
__NOTOC__
This function returns the row and colum indexes of the selected item in a grid list.
This function returns the row and column indexes of the selected item in a grid list. First selected row and column is (0, 0).


==Syntax==  
==Syntax==  
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
int, int guiGridListGetSelectedItem ( element gridList )
int, int guiGridListGetSelectedItem ( element gridList )
</syntaxhighlight>  
</syntaxhighlight>
{{OOP||[[Element/GUI/Gridlist|GuiGridList]]:getSelectedItem}}


===Required Arguments===  
===Required Arguments===  
Line 15: Line 16:


==Example==
==Example==
<section class="client" name="Client" show="true">
This code creates a grid list and fills it with the names of the connected players. When the user selects an item, its text (the player name) will be output in the chat box.
This code creates a grid list and fills it with the names of the connected players. When the user selects an item, its text (the player name) will be output in the chat box.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function createPlayerList ()
addEventHandler('onClientResourceStart', resourceRoot, function()
        -- Create the grid list
    local playerList = guiCreateGridList(0.8, 0.1, 0.15, 0.6, true)
        playerList = guiCreateGridList ( 0.80, 0.10, 0.15, 0.60, true )
 
        -- Create a players column in the list
    -- Create a players column in the list
        local column = guiGridListAddColumn( playerList, "Player", 0.85 )
    local column = guiGridListAddColumn(playerList, 'Player', 0.85)
        if ( column ) then        -- If the column has been created, fill it with players
 
                for id, playeritem in ipairs(getElementsByType("player")) do
    -- If the column has been created, fill it with players
                        local row = guiGridListAddRow ( playerList )
    if not column then return end
                        guiGridListSetItemText ( playerList, row, column, getPlayerName ( playeritem ), false, false )
 
                end
    for id, playeritem in ipairs(getElementsByType('player')) do
                addEventHandler ( "onClientGUIClick", playerList, click )
        local row = guiGridListAddRow ( playerList )
        end
        guiGridListSetItemText(playerList, row, column, getPlayerName(playeritem), false, false)
end
    end
addEventHandler ( "onClientResourceStart", resourceRoot, createPlayerList )


function click ( button, state, sx, sy, x, y, z, elem, gui )
    addEventHandler('onClientGUIClick', playerList, function()
         -- if state is down ( not to trigger the function twice on mouse button up/down), clicked gui and the element is our player list
         local row, column = guiGridListGetSelectedItem(playerList)
         if ( ( state == "down" ) and ( gui == true ) and ( source == playerList ) ) then
         outputChatBox(guiGridListGetItemText(playerList, row, column))
                -- get the player name from the selected row, first column  
     end)
                local playerName = guiGridListGetItemText ( playerList, guiGridListGetSelectedItem ( playerList ), 1 )
                outputChatBox ( playerName )     -- output it to chat box
        end
end
end
</syntaxhighlight>
</syntaxhighlight>
</section>


==See Also==
==See Also==
{{GUI functions}}
{{GUI functions}}
{{GUI_events}}

Latest revision as of 19:05, 19 June 2024

This function returns the row and column indexes of the selected item in a grid list. First selected row and column is (0, 0).

Syntax

int, int guiGridListGetSelectedItem ( element gridList )

OOP Syntax Help! I don't understand this!

Method: GuiGridList:getSelectedItem(...)


Required Arguments

  • gridList: the grid list you want to know the selected row index of

Returns

Returns the row and column indexes of the selected item if the specified grid list is valid and has a selected item, (-1, -1) if no item is selected, false otherwise.

Example

Click to collapse [-]
Client

This code creates a grid list and fills it with the names of the connected players. When the user selects an item, its text (the player name) will be output in the chat box.

addEventHandler('onClientResourceStart', resourceRoot, function()
    local playerList = guiCreateGridList(0.8, 0.1, 0.15, 0.6, true)

    -- Create a players column in the list
    local column = guiGridListAddColumn(playerList, 'Player', 0.85)

    -- If the column has been created, fill it with players
    if not column then return end

    for id, playeritem in ipairs(getElementsByType('player')) do
        local row = guiGridListAddRow ( playerList )
        guiGridListSetItemText(playerList, row, column, getPlayerName(playeritem), false, false)
    end

    addEventHandler('onClientGUIClick', playerList, function()
        local row, column = guiGridListGetSelectedItem(playerList)
        outputChatBox(guiGridListGetItemText(playerList, row, column))
    end)
end

See Also

General functions

Browsers

Buttons

Checkboxes

Comboboxes

Edit Boxes

Gridlists

Memos

Progressbars

Radio Buttons

Scrollbars

Scrollpanes

Static Images

Tab Panels

Tabs

Text Labels

Windows

Input

GUI