How do you set a Checkbox in form to be checked via code?

Feb 23, 2018
14
0
1
#1
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:
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.
 

jungletek

Reality Bytes
Oct 17, 2017
158
6
18
#2
[quote="Reclaimer Shawn" post_id=39223 time=1521947779 user_id=13209]
EDIT: I ended up finding out on my own. Please delete this thread.
[/quote]

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". :lol:
 
Feb 23, 2018
14
0
1
#3
jungletek post_id=39244 time=1521963182 user_id=9475 said:
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". :lol:
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:
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:
    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:
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.
 

TheyCallMeTim13

Wiki Monster
Talents
Fearless Donors
Mar 3, 2017
428
47
28
#4
[quote="Reclaimer Shawn" post_id=39390 time=1522025358 user_id=13209]
jungletek post_id=39244 time=1521963182 user_id=9475 said:
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". :lol:
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:
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:
    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:
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.
[/quote]


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:
SomeForm.SomeCheckbox.State = 1
Code:
if SomeForm.SomeCheckbox.State == 1 then print('I'm checked Dude') end
Code:
if SomeForm.SomeCheckbox.State == cbChecked then print('I'm checked Dude') end
Code:
SomeForm.SomeCheckbox.Checked = true
Code:
SomeForm.SomeCheckbox.OnChange = function(sender) print('I Have The Power!') end
Code:
SomeForm.SomeCheckbox.OnChange(SomeForm.SomeCheckbox)
-- Not real sure about this one but I think it works with "OnChange" and not "onChange"
Code:
SomeForm.SomeCheckbox.onChange(function(sender) print('I Have The Power!') end)
Code:
local function someSuperSweetStuffHere(sender)
    print('I Have The Power!')
end
SomeForm.SomeCheckbox.onChange(someSuperSweetStuffHere)

From "celua.txt" (in the CE directory):
Code:
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)
 
Top Bottom