Why doesn't my code disable my checkbox?

Hi guys,

Fairly straight forward question I am trying to disable a checkbox but am having some difficulties. My code looks like this…

[lua]

local checkbox1 = widget.newSwitch

{

    left = 10,

    top = 300,

    style = “checkbox”,

    id = “checkbox1”,

    onPress = onSwitchPress1

}

checkbox1.disabled = true

sceneGroup:insert( checkbox1 )

[/lua]

Any ideas?

Thanks again,

Matt.

Hi Matt,

The documentation will be your “best friend” if you seek out answers there first. There is no “.disabled” property for widget switches, as shown in the documentation, so that is why it’s not doing anything. You’re only setting a property that effectively does nothing for that object.

http://docs.coronalabs.com/api/library/widget/newSwitch.html

Take care,

Brent

Hi Brent,

I didn’t realise this, the reason i’m trying to disable this is that I have three checkboxes and I only want one of them to be checked at once rather than two or all of them being able to be checked. How do you suggest I combat this?

Thanks again,

Matt.

Hi Matt,

In this case, I suggest you use radio buttons instead. Those are actually widget “switches” too, just with the style “radio”, and to get them to relate with each other (only one radio button in a set turned on at any single time), just add them all to the same display group and Corona will handle the switching on/off for you. As usual, the documentation describes this entire process. :slight_smile:

Brent

Hi Brent,

That has worked much better than the last method.

Thanks again,

Matt.

Hi again Brent,

I cannot seam to be able to get my switch to set my variable in myData.lua am I doing something wrong on my button function here…

[lua]

function OnBtnNext( event )

    if radioButton1.isOn == true or radioButton2.isOn == true or radioButton3.isOn == true then

    composer.gotoScene( “riskAssessmentPg2”, { effect=“slideLeft”, time=800,} ) 

    

elseif radioButton1.isOn == false and radioButton2.isOn == false and radioButton3.isOn == false then

local alert = native.showAlert( “Warning”, “ERROR: No input in checkbox(s)”, { “OK” }, onComplete)

elseif radioButton1.isOn == true then

textCheck1 = myData.IRb1

else

print( myData.IRb1 )

end

end

[/lua]

On line 10 above, you are taking a value from myData.IRb1 and setting testCheck1 to that value.  If you want to set it in myData, perhaps you meant to do:

myData.IRb1 = testCheck1

Rob

Hi Rob,

That was in fact the issue I believe however, my print( myData.IRb1 ) function is no longer working.Any ideas on why this is?

Thanks again,

Matt.

Well that print is in the else clause of a fairly complex if-then-elseif-elseif statement.  Without tracing the values I can’t guess what values are controlling your logic.  You just have to test it and it’s possible that your app simply cannot get to that else.

Rob

Hi Rob,

I have removed the else print( myData.IRb1 ) and simple put it on the scene which my composer is changing to providing one of the checkboxes is checked, and sure enough it is printing. However, it seams as if my checkbox still isn’t sending the data across to myData.lua as I have set the default value in myData.lua to [lua]myData.IRb1 = “empty”[/lua] and this is what is getting printed. Any ideas here?

Thanks,

Matt.

Can you repost your latest code?

Hi Rob,

Yes no problem this is my latest code.

[lua]


– incidentReportingPg1.lua


local composer = require( “composer” )

local scene = composer.newScene()

local widget = require( “widget” )

local myData = require( “myData” )

function onComplete( event )

   if event.action == “clicked” then

        local i = event.index

        if i == 1 then

        end

    end

end

function scene:create( event )

local sceneGroup = self.view

–bg

local bg = display.newRect( 0, 0, display.contentWidth, display.contentHeight )

bg.anchorX = 0

bg.anchorY = 0

bg:setFillColor( 0.9, 0.9, 0.9 ) – white

sceneGroup:insert ( bg )

–titleHeader

local titleHeader = display.newImage( “titleHeader.png” )

titleHeader:translate( 160, -22 )

–title

local title = display.newText( “Incident Reporting”, 0, 0, native.systemFont, 32 )

title:setFillColor( 0 ) – black

title.x = display.contentWidth * 0.5

title.y = -25

–textPage

local textPage = display.newText(“Page 22”, 

280,130, display.contentWidth -25 , display.contentHeight * 0.5, native.systemFont, 15 )

textPage:setFillColor( 0, 0, 0 )

– Handle press events for the buttons

local function onSwitchPress( event )

    local switch = event.target

    print( “Switch with ID '”…switch.id…"’ is on: "…tostring(switch.isOn) )

end

– Image sheet options and declaration

local options = {

    width = 26,

    height = 26,

    numFrames = 2,

    sheetContentWidth = 26,

    sheetContentHeight = 52

}

local checkboxSheet = graphics.newImageSheet( “checkboxSheet.png”, options )

– Create a group for the radio button set

local radioGroup = display.newGroup()

– Create two associated radio buttons (inserted into the same display group)

local radioButton1 = widget.newSwitch

{

    left = 20,

    top = 100,

    style = “radio”,

    id = “RadioButton1”,

    width = 26,

    height = 26,

    onPress = onSwitchPress,

    sheet = checkboxSheet,

    frameOff = 1,

    frameOn = 2

}

radioGroup:insert( radioButton1 )

sceneGroup:insert( radioButton1 )

local radioButton2 = widget.newSwitch

{

    left = 20,

    top = 140,

    style = “radio”,

    id = “RadioButton2”,

    width = 26,

    height = 26,

    onPress = onSwitchPress,

    sheet = checkboxSheet,

    frameOff = 1,

    frameOn = 2

}

radioGroup:insert( radioButton2 )

sceneGroup:insert( radioButton2 )

local radioButton3 = widget.newSwitch

{

    left = 20,

    top = 180,

    style = “radio”,

    id = “RadioButton3”,

    width = 26,

    height = 26,

    onPress = onSwitchPress,

    sheet = checkboxSheet,

    frameOff = 1,

    frameOn = 2

}

radioGroup:insert( radioButton3 )

sceneGroup:insert( radioButton3 )

–textCheck1

local textCheck1 = display.newText(“textCheck1”, 

210, 225, display.contentWidth, display.contentHeight * 0.5, native.systemFont, 13 )

textCheck1:setFillColor( 0, 0, 0 )

sceneGroup:insert( textCheck1 )

–textCheck2

local textCheck2 = display.newText(“textCheck2”, 

210, 265, display.contentWidth, display.contentHeight * 0.5, native.systemFont, 13 )

textCheck2:setFillColor( 0, 0, 0 )

sceneGroup:insert( textCheck2 )

–textCheck3

local textCheck3 = display.newText(“textCheck3”, 

210, 305, display.contentWidth, display.contentHeight * 0.5, native.systemFont, 13 )

textCheck3:setFillColor( 0, 0, 0 )

sceneGroup:insert( textCheck3 )

function OnBtnNext( event )

    if radioButton1.isOn == true or radioButton2.isOn == true or radioButton3.isOn == true then

    composer.gotoScene( “riskAssessmentPg1”, { effect=“slideLeft”, time=800,} ) 

    

elseif radioButton1.isOn == false and radioButton2.isOn == false and radioButton3.isOn == false then

local alert = native.showAlert( “Warning”, “ERROR: No input in checkbox(s)”, { “OK” }, onComplete)

elseif radioButton1.isOn == true then

myData.IRb1 = textCheck1 

end

end

–btnNext

local btnNext = widget.newButton

{

   width = 150,

   height = 40,

   defaultFile = “btnNext.png”,

   onEvent = OnBtnNext

}

btnNext.x = 235

btnNext.y = 445

local function home( event )

    composer.gotoScene( “forms”, { effect=“slideRight”, time=800} )

end

local btnHome = widget.newButton

    {

        width = 320,

        height = 50,

        defaultFile = “home.png”,

        onEvent = home,

    }

    btnHome.x = 160

    btnHome.y = 499

–sceneGroup

sceneGroup:insert( titleHeader )

sceneGroup:insert( title )

sceneGroup:insert( textPage )

sceneGroup:insert( btnNext )

end 

function scene:show( event )

local sceneGroup = self.view

end

function scene:hide( event )

local sceneGroup = self.view

end

function scene:destroy( event )

local sceneGroup = self.view

end


– Listener setup

scene:addEventListener( “create”, scene )

scene:addEventListener( “show”, scene )

scene:addEventListener( “hide”, scene )

scene:addEventListener( “destroy”, scene )


return scene

[/lua]

Thanks again,

Matt.

function OnBtnNext( event )       if radioButton1.isOn == true or radioButton2.isOn == true or radioButton3.isOn == true then         composer.gotoScene( "riskAssessmentPg1", { effect="slideLeft", time=800,} )          elseif radioButton1.isOn == false and radioButton2.isOn == false and radioButton3.isOn == false then         local alert = native.showAlert( "Warning", "ERROR: No input in checkbox(s)", { "OK" }, onComplete)     elseif  radioButton1.isOn == true then         myData.IRb1 = textCheck1     end end

Your last elseif will never, every be reached.

If radioButton1.isOn is true, the first “if” clause will be true and you will goto the scene and never make it here.  If all three check boxes are off, your alert will fire and never make it to the third block.  You probably want something like this:

function OnBtnNext( event )     if  radioButton1.isOn == true then         myData.IRb1 = textCheck1     end     if radioButton1.isOn == true or radioButton2.isOn == true or radioButton3.isOn == true then         composer.gotoScene( "riskAssessmentPg1", { effect="slideLeft", time=800,} )          elseif radioButton1.isOn == false and radioButton2.isOn == false and radioButton3.isOn == false then         local alert = native.showAlert( "Warning", "ERROR: No input in checkbox(s)", { "OK" }, onComplete)     end end

Rob

Hi Brent,

This has worked perfectly now thank you for your assistances here.

Matt.

Hi Matt,

The documentation will be your “best friend” if you seek out answers there first. There is no “.disabled” property for widget switches, as shown in the documentation, so that is why it’s not doing anything. You’re only setting a property that effectively does nothing for that object.

http://docs.coronalabs.com/api/library/widget/newSwitch.html

Take care,

Brent

Hi Brent,

I didn’t realise this, the reason i’m trying to disable this is that I have three checkboxes and I only want one of them to be checked at once rather than two or all of them being able to be checked. How do you suggest I combat this?

Thanks again,

Matt.

Hi Matt,

In this case, I suggest you use radio buttons instead. Those are actually widget “switches” too, just with the style “radio”, and to get them to relate with each other (only one radio button in a set turned on at any single time), just add them all to the same display group and Corona will handle the switching on/off for you. As usual, the documentation describes this entire process. :slight_smile:

Brent

Hi Brent,

That has worked much better than the last method.

Thanks again,

Matt.

Hi again Brent,

I cannot seam to be able to get my switch to set my variable in myData.lua am I doing something wrong on my button function here…

[lua]

function OnBtnNext( event )

    if radioButton1.isOn == true or radioButton2.isOn == true or radioButton3.isOn == true then

    composer.gotoScene( “riskAssessmentPg2”, { effect=“slideLeft”, time=800,} ) 

    

elseif radioButton1.isOn == false and radioButton2.isOn == false and radioButton3.isOn == false then

local alert = native.showAlert( “Warning”, “ERROR: No input in checkbox(s)”, { “OK” }, onComplete)

elseif radioButton1.isOn == true then

textCheck1 = myData.IRb1

else

print( myData.IRb1 )

end

end

[/lua]

On line 10 above, you are taking a value from myData.IRb1 and setting testCheck1 to that value.  If you want to set it in myData, perhaps you meant to do:

myData.IRb1 = testCheck1

Rob