Page 1 of 1
How do you set a Checkbox in form to be checked via code?
Posted: Sun Mar 25, 2018 3:16 am
by Reclaimer Shawn
So, I want a certain function to be activated via Checkbox, and considering I think checkbox timers are a rather effective method of repeatedly calling a function, I wanted another function to check a checkbox automatically and run the code associated with it. How would I do this from another function?
This is the code I want to be executed outside of its local function:
Code: Select all
function CECheckbox7Change(sender)
if ((checkbox_getState(UDF1.CECheckbox7)) == 1) then
t=createTimer(nil)
timer_setInterval(t, 1)
timer_onTimer(t, xpfixer)
else
t.destroy();
t=nil
end
end
The reason I wanted to do it like this is because I wanted the checkbox to be invisible for the user but still play an active role in modifying in game values.
Re: How do you set a Checkbox in form to be checked via code?
Posted: Sun Mar 25, 2018 7:33 am
by jungletek
Reclaimer Shawn wrote: ↑Sun Mar 25, 2018 3:16 am
EDIT: I ended up finding out on my own. Please delete this thread.
You should consider posting it for other people's enlightenment.
Nothing worse than seeing a post that matches what you're trying to do, and then the inevitable "Edit: NVM, figured it out".
Re: How do you set a Checkbox in form to be checked via code?
Posted: Mon Mar 26, 2018 12:49 am
by Reclaimer Shawn
jungletek wrote: ↑Sun Mar 25, 2018 7:33 am
You should consider posting it for other people's enlightenment.
Nothing worse than seeing a post that matches what you're trying to do, and then the inevitable "Edit: NVM, figured it out".
I ended up solving my own issue. Let's say I want the function of one checkbox to check another checkbox and activate its function at the same time.
I could use this to activate the code above without even messing with the checkbox:
Code: Select all
function CECheckbox3Change(sender)
if ((checkbox_getState(UDF1.CECheckbox3)) == 1) then
t=createTimer(nil)
timer_setInterval(t, 1)
timer_onTimer(t, random)
else
t.destroy();
t=nil
end
checkbox_setState(UDF1.CECheckbox7, 1) //This sets the checkbox to be checked//
CECheckbox7Change(sender) //This calls the checkbox function, which then checks to see if the box is checked and then acts based on checked condition//
if ((checkbox_getState(UDF1.CECheckbox3)) == 0) then
checkbox_setState(UDF1.CECheckbox7, 0) //This sets the checkbox to be unchecked if it doesn't meet the condition of this box being checked//
CECheckbox7Change(sender) //This calls the checkbox function, which then checks to see if the box is checked and then acts based on checked condition//
end
end
So, if you want to set a checkbox via lua, you can do it via this:
Code: Select all
checkbox_setState(FormName.NameOfCheckbox, 0) //Sets Checkbox to Unchecked//
checkbox_setState(FormName.NameOfCheckbox, 1) //Sets Checkbox to Checked//
checkbox_setState(FormName.NameOfCheckbox, 2) //Sets Checkbox to Grayed if the Checkbox allows it//
If you then wanted to call the checkbox function outside of its function, you could do this:
Code: Select all
function SOMEOTHERFUNCTION()
YOUR CODE HERE
NameOfCheckboxChange(sender)
end
Make sure to call the function after everything you want to execute in SOMEOTHERFUNCTION, else that function will cease and the checkbox function will begin instead.
Re: How do you set a Checkbox in form to be checked via code?
Posted: Mon Mar 26, 2018 1:30 am
by TimFun13
Reclaimer Shawn wrote: ↑Mon Mar 26, 2018 12:49 am
jungletek wrote: ↑Sun Mar 25, 2018 7:33 am
You should consider posting it for other people's enlightenment.
Nothing worse than seeing a post that matches what you're trying to do, and then the inevitable "Edit: NVM, figured it out".
I ended up solving my own issue. Let's say I want the function of one checkbox to check another checkbox and activate its function at the same time.
I could use this to activate the code above without even messing with the checkbox:
Code: Select all
function CECheckbox3Change(sender)
if ((checkbox_getState(UDF1.CECheckbox3)) == 1) then
t=createTimer(nil)
timer_setInterval(t, 1)
timer_onTimer(t, random)
else
t.destroy();
t=nil
end
checkbox_setState(UDF1.CECheckbox7, 1) //This sets the checkbox to be checked//
CECheckbox7Change(sender) //This calls the checkbox function, which then checks to see if the box is checked and then acts based on checked condition//
if ((checkbox_getState(UDF1.CECheckbox3)) == 0) then
checkbox_setState(UDF1.CECheckbox7, 0) //This sets the checkbox to be unchecked if it doesn't meet the condition of this box being checked//
CECheckbox7Change(sender) //This calls the checkbox function, which then checks to see if the box is checked and then acts based on checked condition//
end
end
So, if you want to set a checkbox via lua, you can do it via this:
Code: Select all
checkbox_setState(FormName.NameOfCheckbox, 0) //Sets Checkbox to Unchecked//
checkbox_setState(FormName.NameOfCheckbox, 1) //Sets Checkbox to Checked//
checkbox_setState(FormName.NameOfCheckbox, 2) //Sets Checkbox to Grayed if the Checkbox allows it//
If you then wanted to call the checkbox function outside of its function, you could do this:
Code: Select all
function SOMEOTHERFUNCTION()
YOUR CODE HERE
NameOfCheckboxChange(sender)
end
Make sure to call the function after everything you want to execute in SOMEOTHERFUNCTION, else that function will cease and the checkbox function will begin instead.
DB added OOP code some time ago (but you will find a lot more examples using the older style). So you can also deal with it directly as an object.
Code: Select all
if SomeForm.SomeCheckbox.State == 1 then print('I'm checked Dude') end
Code: Select all
if SomeForm.SomeCheckbox.State == cbChecked then print('I'm checked Dude') end
Code: Select all
SomeForm.SomeCheckbox.Checked = true
Code: Select all
SomeForm.SomeCheckbox.OnChange = function(sender) print('I Have The Power!') end
Code: Select all
SomeForm.SomeCheckbox.OnChange(SomeForm.SomeCheckbox)
-- Not real sure about this one but I think it works with "OnChange" and not "onChange"
Code: Select all
SomeForm.SomeCheckbox.onChange(function(sender) print('I Have The Power!') end)
Code: Select all
local function someSuperSweetStuffHere(sender)
print('I Have The Power!')
end
SomeForm.SomeCheckbox.onChange(someSuperSweetStuffHere)
From "celua.txt" (in the CE directory):
Code: Select all
CheckBox Class: (Inheritance: ButtonControl->WinControl->Control->Component->Object)
createCheckBox(owner): Creates a CheckBox class object which belongs to the given owner. Owner can be any object inherited from WinControl
properties
Checked: boolean - True if checked
AllowGrayed: boolean - True if it can have 3 states. True/False/None
State: checkboxstate - The state. (cbUnchecked=0, cbChecked=1, cbGrayed=2)
OnChange: function - Function to call when the state it changed
methods
getAllowGrayed()
setAllowGrayed(boolean)
getState(): Returns a state for the checkbox. (cbUnchecked, cbChecked, cbGrayed)
setState(boolean): Sets the state of the checkbox
onChange(function)