cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Check out the JMP® Marketplace featured Capability Explorer add-in
Choose Language Hide Translation Bar
GgGgGg2024
Level I

If function when checkBox is checked and unchecked

 
//Define Function1
Function1 = Function( {}, // Code for function 1 goes here Print("Checkbox is checked! Function 1 executed."); ); // Define function 2 Function2 = Function( {}, // Code for function 2 goes here Print("Checkbox is unchecked! Function 2 executed."); ); // Create a new window nw = New Window( "Checkbox Example", << Modal, // Add a checkbox to the window cb = Checkbox( "Toggle Function", Value( 0 ) ) ); // Add an OK button to the window nw << Append( Button Box( "OK", // Function to execute when OK button is clicked <<Set Function( Function( {this}, If( cb << Get Selected(), Function1(); // Call function 1 if checkbox is checked , Function2(); // Call function 2 if checkbox is unchecked ); // Close the window after execution Close( this ); ) ) ));




Hi, The code above is similar to what I am trying to do. I have a check box, and if I select it, and then click ok, function 1 should be displayed; and if i do not select it and click ok, function 2 should be displayed. 

1 ACCEPTED SOLUTION

Accepted Solutions
jthi
Super User

Re: If function when checkBox is checked and unchecked

Very easy to test by just trying it

Names Default To Here(1);

a = function({}, {Default Local},
	Show("a");
);

b = function({}, {Default Local},
	Show("b");
);

func1 = function({input_value = 1}, {Default Local},
	Match(input_value,
		1, a(),
		2, b()
	);
);


func2 = function({}, {Default Local},
	Show(2)
);

func3 = function({}, {Default Local},
	If(window:cb << get,
		func1()
	,
		func2()
	);	
);

nw = New Window("",
	window:cb = Check Box({""}),
	Button Box("OK",
		func3();
	)
)

I would start testing something like this without the check box and new window as those just add extra layer of complexity. You have to first be able to solve how functions work together before you start adding them to check boxes and so on.

-Jarmo

View solution in original post

3 REPLIES 3
jthi
Super User

Re: If function when checkBox is checked and unchecked

That script looks like it has been created by ChatGPT or something similar and it does have few issues. Below is example which should work

Names Default To Here(1);

func1 = function({}, {Default Local},
	Show(1)
);


func2 = function({}, {Default Local},
	Show(2)
);

nw = New Window("",
	window:cb = Check Box({""}),
	Button Box("OK",
		If(window:cb << get,
			func1()
		,
			func2()
		);
	)
)
-Jarmo
GgGgGg2024
Level I

Re: If function when checkBox is checked and unchecked

You are absolutely correct. 

 

Can i nest a function? (write a function within a function)

CheckBoxHandler = Function ({cb},
	If ( cb << Get,
		//Call function with apple/banana if checkbox is checked	
		checkedbox ()
		,
		//Else
		//Call function with mango/watermelon if checkbox is not checked 
		uncheckedbox ()
	   ),
	);
//Function 1 - box is checked
checkedbox = Function ({},
	Match ( x, 
			"a", callapple,  
			"b", callbanana
		)
);

//Function 2 - box is not checked
uncheckedbox = Function ({},
	Match (x, 
			"a", callmango,  
			"b", callwatermelon	
		)
);

I need to add a match function in the [ Button Box ("OK",  ] command

jthi
Super User

Re: If function when checkBox is checked and unchecked

Very easy to test by just trying it

Names Default To Here(1);

a = function({}, {Default Local},
	Show("a");
);

b = function({}, {Default Local},
	Show("b");
);

func1 = function({input_value = 1}, {Default Local},
	Match(input_value,
		1, a(),
		2, b()
	);
);


func2 = function({}, {Default Local},
	Show(2)
);

func3 = function({}, {Default Local},
	If(window:cb << get,
		func1()
	,
		func2()
	);	
);

nw = New Window("",
	window:cb = Check Box({""}),
	Button Box("OK",
		func3();
	)
)

I would start testing something like this without the check box and new window as those just add extra layer of complexity. You have to first be able to solve how functions work together before you start adding them to check boxes and so on.

-Jarmo