Referrencing Individual Checkboxes

daviiadams

Prince
Joined
Jul 7, 2011
Messages
369
Location
London
Hi all,

I've been playing with checkboxes, but haven't figured out how you referrence a particular one within Lua when you have several available to select from. No issues with the controls, but need to be able to do an If statement etc.

Code:
if (bIsChecked) then

     if (???) then

          <rest of statement>

I've attempted trying to referrence IDs and had a quick search, but not found anything :confused:
 
Wrong approach.

Don't try and find the checkbox to find it's state when you want to do something, but as the checkbox changes state (which you receive notification off in the callback handler) record that into a variable and then query the variable.

eg

Code:
local bIsMode24 = false
local bIsModeAMPM = false

function getClockText()
  local sFormat
  
  if (bIsMode24) then
    sFormat = Locale.ConvertTextKey("TXT_KEY_CLOCK_MODE_24")
  elseif (bIsModeAMPM) then
    sFormat = Locale.ConvertTextKey("TXT_KEY_CLOCK_MODE_12_AM")
  else
    sFormat = Locale.ConvertTextKey("TXT_KEY_CLOCK_MODE_12")
  end

  return os.date(sFormat)
end

function OnMode24HourChecked(bIsChecked)
  bIsMode24 = bIsChecked
end
Controls.Mode24Hour:RegisterCheckHandler(OnMode24HourChecked)

function OnModeAmPmChecked(bIsChecked)
  bIsModeAMPM = bIsChecked
end
Controls.ModeAmPm:RegisterCheckHandler(OnModeAmPmChecked)
 
Does that mean a control function for each checkbox then? I'd been trying to do something like this to pass through to the function handling the bIsChecked code:

Code:
function StatuteSelection()

     for i = 1, reformNumTable[1], 1 do
	statuteSelection = string.format("Statute%i", i);
     end

     Controls.statuteSelection:RegisterCheckHandler(OnStatuteSelected);

end

Obviously, the checkboxes are have IDs Statute1, Statute2 and so on.
 
Controls.statuteSelection will look for a control with an ID of "statuteSelection" - to use the value of the variable "statuteSelection" you would need to do Controls[statuteSelection]

Your code also only registers one check handler, and not one per control, which is what you will need, so the RegisterCheckHandler needs to be inside the loop

Code:
function StatuteSelection()
  for i = 1, reformNumTable[1], 1 do
    statuteSelection = string.format("Statute%i", i);
    Controls[statuteSelection]:RegisterCheckHandler(OnStatuteSelected);
  end
end

But then within the callback you have no way to ascertain which check box was ticked, but you can use closures to fix that

Code:
function StatuteSelection()
  for i = 1, reformNumTable[1], 1 do
    statuteSelection = string.format("Statute%i", i);
    Controls[statuteSelection]:RegisterCheckHandler(function () OnStatuteSelected(i) end);
  end
end

and then make the OnStatuteSelected method take a parameter to identify the control
 
Is this sort of thing similar (I'd initially started with something like this), albeit needing the OnStatuteSelected part amended as above?

Code:
function StatuteSelection()

     for i = 1, reformNumTable[1], 1 do
	statuteSelection = Controls[string.format("Statute%i", i)]:RegisterCheckHandler(OnStatuteSelected);
     end
end

I had a go shortly before grabbing some sleep earlier, so have to confess to not being all that with it :crazyeye:
 
No.

AFAIK RegisterCheckHandler() does not return a value so statuteSelection will always be nil (but that won't matter as you never use the value anyway), but again, when the OnStatuteSelected() method is called it has no way of being able to tell which one of the checkbox controls was clicked by the user.
 
No.

AFAIK RegisterCheckHandler() does not return a value so statuteSelection will always be nil (but that won't matter as you never use the value anyway), but again, when the OnStatuteSelected() method is called it has no way of being able to tell which one of the checkbox controls was clicked by the user.

Admittedly, I was referring to the similar loops you suggested when looking at Bottom Panel Info, but did think I'd only be getting away with it by chance if it did work :p

I've spent half the night trying to do something with this CheckHandler issue and have just got completely lost with what I'm supposed to do with the closure statement, or the function being referred to in it, other than "i" is being inhereted from the loop(?)
 
Back
Top Bottom