cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
JMP is taking Discovery online, April 16 and 18. Register today and join us for interactive sessions featuring popular presentation topics, networking, and discussions with the experts.
Choose Language Hide Translation Bar

Modal windows - the "OK" button

Hi - I want to create a modal window that requests a response from the user, something like this:

teb = text edit box("");

ok = button box("OK");

nw = new window("My Modal Window", << modal(1),

    h list box(text box("Type here: "), teb),

    ok

);

How can I prevent the window from closing if the user clicks on “OK” without supplying a response?  I’ve tried adding assorted scripts to the “OK” button to see if teb contains a string of greater than zero length, and then only closing the window if it has, but I can’t seem to find the right syntax: the window always closes regardless.

I note that the window supplies an "OK" button even if I don't include it (presumably because it has to have one), so I'm wondering whether it is replacing my "OK" button with one of its own.  Is it possible that that's the problem?

Many thanks

1 ACCEPTED SOLUTION

Accepted Solutions
michaelhaslam_p
Level III

Re: Modal windows - the "OK" button

Dodo,

The OK button or its "equivalent" is required in a modal window.  The point is to construct your modal window so that the user makes choices and then clicks "OK" or "Cancel" to those choices.  The equivalent is "Yes" and "No".

Try the following:

New Window("Modal Demo", <<Modal,

  Textbox("Do you really want to do that?"),

  ButtonBox("Yes"),

  ButtonBox("No")

);

So, you must always construct your modal dialog to end in "Yes" or "No" or "OK" and "Cancel".

If you think about this, it makes sense.  You have two choices: accept or do not accept.

To be more specific to your question, you can disable the "OK" or "Yes" button until something is done.  Try the following:

Stuff Selected = "";
Entry Window = List(Button(-1));

Ask the User Expr = Expr (

Entry Window = New Window("Modal Demo", <<Modal,
  TextBox("Please enter some stuff?"),
  Stuff = TextEditBox("", <<Script(Stuff Selected = Stuff << Get Text; Yes Button << Enable(1))),
  Yes Button = ButtonBox("Yes", <<Enable(0)),
);

);

While(Stuff Selected == "" | Entry Window["Button"] == -1,

Ask the User Expr;

);

Michael

View solution in original post

11 REPLIES 11
stan_koprowski
Community Manager Community Manager

Re: Modal windows - the "OK" button

Hello,

You can try the following--

Change from modal to nonmodal and and add a "Cancel" button box.

I would suggest moving  your text edit box and  "OK" button box as well.

The messages will be printed to the log with each button that is selected.

Best,

Stan

!-

nw = new window("My Modal Window", << nonmodal,

    h list box(text box("Type here: "),teb = text edit box("") ),

    Button Box("OK", usertext = teb <<Get Text;

If(usertext == "", throw("!Error 001: (No Value)--Enter a value before proceeding"),

nw<<Close Window; Clear log(); Write("You entered the text = " || char(usertext))

)),

Button Box( "Cancel", nw<<Close Window; Clear Log(); Write("User Cancelled.") )

);


-!

Re: Modal windows - the "OK" button

On a related point to the one above, I'd prefer to retain the modal status of the window I've created below if at all possible, but change the name of the "OK" button to the word "Quit" - even though I'd like its inherent functionality (i.e. to close the window after doing anything else that's been specified within the button's associated script) to be unchanged.  Is that possible?

nw = new window( "My Window", << modal(1), button box("OK", nw << close window) );

If I simply change the name to anything else the window automatically creates a new "OK" button to replace the one that's been removed - and I don't want that to happen.  Two possible courses of action that occurred to me were that maybe I could a) make the "OK" button invisible and just create another button called "Quit" to close the window, or perhaps b) hide it behind another object in the window - but I can't see a way to do either of these.

Does anyone have any ideas?

michaelhaslam_p
Level III

Re: Modal windows - the "OK" button

Dodo,

The OK button or its "equivalent" is required in a modal window.  The point is to construct your modal window so that the user makes choices and then clicks "OK" or "Cancel" to those choices.  The equivalent is "Yes" and "No".

Try the following:

New Window("Modal Demo", <<Modal,

  Textbox("Do you really want to do that?"),

  ButtonBox("Yes"),

  ButtonBox("No")

);

So, you must always construct your modal dialog to end in "Yes" or "No" or "OK" and "Cancel".

If you think about this, it makes sense.  You have two choices: accept or do not accept.

To be more specific to your question, you can disable the "OK" or "Yes" button until something is done.  Try the following:

Stuff Selected = "";
Entry Window = List(Button(-1));

Ask the User Expr = Expr (

Entry Window = New Window("Modal Demo", <<Modal,
  TextBox("Please enter some stuff?"),
  Stuff = TextEditBox("", <<Script(Stuff Selected = Stuff << Get Text; Yes Button << Enable(1))),
  Yes Button = ButtonBox("Yes", <<Enable(0)),
);

);

While(Stuff Selected == "" | Entry Window["Button"] == -1,

Ask the User Expr;

);

Michael

Re: Modal windows - the "OK" button

Yes, I've got it now - that's exactly what I needed to know.  The original question arose because I wasn't actually aware of until now was that using "Yes" and/or "No" as in the above example don't result in the automatic generation of an unwanted "OK" button within the window (whereas for example using alternatives like "Accept" and "Reject" would), quite simply because I've never actually tried them - I'd previously assumed that calling the button "OK" was the only option available.  Now I know I've a choice of four, the problem evaporates.

Many thanks

Tina
Level III

Re: Modal windows - the "OK" button

Dear Michael,

thank you very much for this elegant solution.

I would like to use your code to not only check if somthing was entered into the Textbox but to check on the length of the character that was entered. Do you have a working solution to that? I did not succeed yet.

In the end my goal is to have three text boxes and to do this check on each of them, and only if all three checks are passed, the "OK" button would be enabled.

Best,

Tina

Re: Modal windows - the "OK" button

This example should illustrate one way:

teb1 state = teb2 state = 0; // set to false or off

dlg = New Window( "Dialog", << Modal,
	teb1 = Text Edit Box( "",
		<< Set Function(
			Function( { me }, { content },
				content = me << Get Text;
				teb1 state = Length( content );
				ok << Enabled( teb1 state & teb2 state );
			)
		)
	),
	teb2 = Text Edit Box( "",
		<< Set Function(
			Function( { me }, { content },
				content = me << Get Text;
				teb2 state = Length( content );
				ok << Enabled( teb1 state & teb2 state );
			)
		)
	),
	H List Box(
		ok = Button Box( "OK",
			text1 = teb1 << Get;
			text2 = teb2 << Get;,
			<< Enabled( 0 )
		),
		Button Box( "Cancel" )
	)
);
txnelson
Super User

Re: Modal windows - the "OK" button

Here is a simple example with one text box.

Names Default To Here( 1 );

nw = New Window( "text boxes", 
	modal,
	v list box(
	tb1 = Text Edit Box( "",
		<<set width( 200 ),
		<<set script(
			Show( tb1 << get text, Length( tb1 << get text ) );
			If( tb1 << get text != "" & Length( tb1 << get text ) > 5,
				bb << enable( 1 ),
				bb << enable( 0 )
			);
		)
	),
	bb = Button Box( "OK", nw << close window )),
	bb << enable( 0 );
);
Jim
pmroz
Super User

Re: Modal windows - the "OK" button

You could also do it with a while loop, and only exit when text has been entered.

 

Names Default To Here( 1 );

stay_in_loop = 1;
entered_text = "";
while(stay_in_loop,
	
	nw = New Window( "text boxes", modal,
		h list box(
			tb = Text Box( "Enter your text: "),
			teb = text edit box("", set width(200), set wrap(200)),
		),
		bb = Button Box( "OK", entered_text = teb << get text),
	);
	
	if (entered_text == "",
	// then
		errwin = new window("Error", << modal,
			textbox("You must enter some text.  Please try again")
		);
		,
	// else
		stay_in_loop = 0;
	);
);

print(entered_text);
Tina
Level III

Re: Modal windows - the "OK" button

I thank all of you for the quick and detailed reply.

The solution I use now is the following:

 

Names Default To Here( 1 );

nw = New Window( "Please insert up to three names",
modal,
v list box(

Text Box( "Name 1:" ),

tb1 = Text Edit Box( "",
<<set width( 200 ),
<<set script(
Show( tb1 << get text, Length( tb1 << get text ) );
)
),

Text Box( "Name 2:" ),

tb2 = Text Edit Box( "",
<<set width( 200 ),
<<set script(
Show( tb2 << get text, Length( tb2 << get text ) );
)
),

Text Box( "Name 3:" ),

tb3 = Text Edit Box( "",
<<set width( 200 ),
<<set script(
Show( tb3 << get text, Length( tb3 << get text ) );

If((tb1 << get text == "" | Length( tb1 << get text ) == 8) &
(tb2 << get text == "" | Length( tb2 << get text ) == 8) &
(tb3 << get text == "" | Length( tb3 << get text ) == 8) ,
bb << enable( 1 ),
bb << enable( 0 )
);

)
),

bb = Button Box( "OK", nw << close window )),
bb << enable( 0 );
);

 

So one should be able to press the "OK" button if for each name either a character string with length 8 or no character string is entered.

What still does not work as I hoped is if I enter only one name with length 8, and I do that for Name 3, then I can click the button. Otherwise (if I enter one name with length 8 for Name 1 or Name 2), it is not possible to click the button.

Any advice here? 

Best regards and thanks again,

Tina