PDA

View Full Version : disabling a button with a checkbox


TheRispo
10-22-2009, 03:31 AM
Hi everyone.. I finally gathered some courage and tackled UI building in MEL.. The issue here is:
I want a couple of intFields & textFields to be disabled until the user clicks on a checkbox. This way the user might add its own values instead of using the default ones...

My guess is that this might be achieved using the -onCommand and -offComand on the checkbox.. But being the total n00b I am, I can't figure it out how to make this thing turn on/off the -enable flag on the textfield.

I'll be thankfull if you guys can point me in the right direction.

Buexe
10-22-2009, 06:22 AM
you can use the "-enable" flag to turn controls on or off. Just make sure you also use the "-edit" flag when you want to change the enable state of an existing control.

Pyrokinesis
10-22-2009, 07:01 AM
Here is a super simple example:


global proc openWindow(){

// If the window exists, delete it
if (`window -exists toggleWin`)
deleteUI toggleWin;

// Declare the window and a couple options
window -title "Toggle Window" -widthHeight 220 310 -maximizeButton off toggleWin;

// Declare a layout for us to put a couple of controls
columnLayout mainColumn;

// Create our textField and checkbox
// Give them variable names which we can use to query their attributes later
// Tell the checkBox to do something when we change it's status
textField -text "This is text" -width 150 myTxtField;
checkBox -label "Toggle" -changeCommand toggleTxtEnable myCheckBox;

// Show our Window
showWindow toggleWin;
}

// Global Proc to toggle the enable state of the text field
global proc toggleTxtEnable(){

// Query the enable state of the textField, notce how we use its name
// If it "Is" enabled set enable to 0, else set enable to 1
if(`textField -q -enable myTxtField`){
textField -edit -enable 0 myTxtField;
}
else{
textField -edit -enable 1 myTxtField;
}
}

// Initiate the Global Proc to launch our window
openWindow();


As with everything in Maya there is always multiple ways to achieve the same result.
It's important not to think of one way as the magic bullet solution.

Good Luck.

TheRispo
10-22-2009, 02:12 PM
Thanks for the help guys! it works great... I guess I was barking at the wrong tree there with the -onCommand and -offCommand.
Thanks again

efecto
10-22-2009, 08:07 PM
Thanks for the help guys! it works great... I guess I was barking at the wrong tree there with the -onCommand and -offCommand.
Thanks again

You can use those flags too.

eg)
-onCommand textOn
-offCommand textOff

proc textOn {
// turn on the text, text -e -en 1
}

proc textOff {
// turn off text, text -e -en 0, there should be a flag to control visibility too
}

TheRispo
10-22-2009, 08:44 PM
Thanks for the reply efecto, Its great to learn diferent ways to achieve the same thing.

ewerybody
10-23-2009, 08:53 AM
here is one more magic bullet:

window;
columnLayout;
button -label "Ok!" theButton;
checkBox -v 1 -cc "button -e -en #1 theButton";
showWindow;


Use the #1-placeholder which stands for the current value of the control. Of course this is more pain in the a if you try to stuff more elements into the -changeCommand (-cc) but you could also en-/disable a layout which holds all the controls you want to switch.

TheRispo
10-23-2009, 05:01 PM
Great.. thanks for that!!

I didnt know that #1 would have such a function... Does a more in depth explanation of that comes in the docs?? or is it kind of basic coding knowledge?? sorry if its a question to basic, but my only coding knowledge comes from particle expresions and simple "proceduraly-build-your-own-stairs" scripts... nothing UI related.

Thanks

ewerybody
10-24-2009, 07:36 AM
I doubt that you find much more about that in the docs.. Its actually very simple. #1,#2 or #3 .. in a command at a control is replaced with the current value of the control. At controls with multiple inputs there is #2,#3 for the other ones..

instead you could also write:

window;
columnLayout;
button -label "Ok!" theButton;
checkBox -v 1 -cc "button -e -en (`checkBox -q -v theCheckBox`) theButton" theCheckBox;
showWindow;

So the controls asks itself what value it currently has. You need to name it then and the string is much longer. But wurks as well. One issue I experienced: if you use #1 at textFields entered quote signs are not properly passed.

TheRispo
10-24-2009, 04:00 PM
thanks for the explanation ewerybody, I think its much clearer now.
Thanks for the help

CGTalk Moderation
10-24-2009, 04:00 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.