GuiCreateButton: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
mNo edit summary
Line 27: Line 27:
This example creates an edit box alongside an "Output!" button.  When the button is clicked, it will output the message in the edit box into the Chat Box.
This example creates an edit box alongside an "Output!" button.  When the button is clicked, it will output the message in the edit box into the Chat Box.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
--create our button
-- Create a simple button
button = guiCreateButton( 0.7, 0.1, 0.2, 0.1, "Output!", true )
local myButton = guiCreateButton(0.30, 0.15, 0.05, 0.03, "Click me", true)
--Create an edit box and define it as "editBox".
editBox = guiCreateEdit( 0.3, 0.1, 0.4, 0.1, "Type your message here!", true )
-- and attach our button to the outputEditBox function
addEventHandler ( "onClientGUIClick", editBox, outputEditBox )
guiEditSetMaxLength ( editBox, 128 ) --the max chatbox length is 128, so force this


--setup our function to output the message to the chatbox
-- Attach a click event to the button to change its text
function outputEditBox ()
addEventHandler("onClientGUIClick", myButton, function()
        local text = guiGetText ( editBox )--get the text from the edit box
    guiSetText(myButton, "Clicked!")
         outputChatBox ( text ) --output that text
end)
end
</syntaxhighlight>
addEventHandler ( "onClientGUIClick", button, outputEditBox )
 
<syntaxhighlight lang="lua">
-- Chat example
local myButton = guiCreateButton(0.30, 0.20, 0.05, 0.03, "Click me", true)
local myInput = guiCreateEdit(0.36, 0.20, 0.13, 0.03, "", true)
 
addEventHandler("onClientGUIClick", myButton, function()
    local myText = guiGetText(myInput, "") -- Grab the input value
    if myText ~= "" then -- Make sure the input not empty
         outputChatBox(myText)
        guiSetText(myInput, "") -- Clear the input
    end
end)
</syntaxhighlight>
</syntaxhighlight>



Revision as of 09:04, 26 November 2025

This function allows creation of a GUI Button, which is a clickable item as part of GUI.

Syntax

element guiCreateButton ( float x, float y, float width, float height, string text, [ bool relative = false, gui-element parent = nil ] )

OOP Syntax Help! I don't understand this!

Method: GuiButton(...)


Required Arguments

GUI Test Button
  • x: A float of the 2D x position of the GUI button on a player's screen. This is affected by the relative argument.
  • y: A float of the 2D y position of the GUI button on a player's screen. This is affected by the relative argument.
  • width: A float of the width of the GUI button. This is affected by the relative argument.
  • height: A float of the height of the GUI button. This is affected by the relative argument.
  • text: A string of the text that will be displayed as a label on the button.

Optional Arguments

NOTE: When using optional arguments, you might need to supply all arguments before the one you wish to use. For more information on optional arguments, see optional arguments.

  • relative: This is whether sizes and positioning are relative. If this is true, then all x, y, width and height floats must be between 0 and 1, representing sizes relative to the parent.
  • parent: This is the parent that the gui button is attached to. If the relative argument is true, sizes and positioning will be made relative to this parent. If the relative argument is false, positioning will be the number of offset pixels from the parent's origin. If no parent is passed, the parent will become the screen - causing positioning and sizing according to screen positioning.

Returns

Returns an element of the created button if it was successfully created, false otherwise.

Example

This example creates an edit box alongside an "Output!" button. When the button is clicked, it will output the message in the edit box into the Chat Box.

-- Create a simple button
local myButton = guiCreateButton(0.30, 0.15, 0.05, 0.03, "Click me", true)

-- Attach a click event to the button to change its text
addEventHandler("onClientGUIClick", myButton, function()
    guiSetText(myButton, "Clicked!")
end)
-- Chat example
local myButton = guiCreateButton(0.30, 0.20, 0.05, 0.03, "Click me", true)
local myInput = guiCreateEdit(0.36, 0.20, 0.13, 0.03, "", true)

addEventHandler("onClientGUIClick", myButton, function()
    local myText = guiGetText(myInput, "") -- Grab the input value
    if myText ~= "" then -- Make sure the input not empty
        outputChatBox(myText)
        guiSetText(myInput, "") -- Clear the input
    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