cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
BayesRabbit7133
Level III

Create different number of text boxes based on variable

Dear JMPer,

 

I try to create several text boxes in a new window,  the number of text boxes is a variable

Then I have a slider box , and I need to set text for those tex boxes I just created as I move the bar

I guess a for loop should be the solution, but I just cannot get it done

 

colname = {"Name", "Height", "Weight"}; //it's a list of column names from previous code, number of items is not fixed
min = 0;
max = 10;
Value = 0;

eval(evalexpr(
	nw = New Window( "parameter setting",
		 Panel Box( "parameter setting",
			tb1 = text box(char(colname[1]) || ": " || char(:expr(nameexpr(ascolumn(colname[1])))[1])),
			tb2 = text box(char(colname[2]) || ": " || char(:expr(nameexpr(ascolumn(colname[2])))[1])),
			tb3 = text box(char(colname[3]) || ": " || char(:expr(nameexpr(ascolumn(colname[3])))[1])),
			//.
			//.
			//.
			//.
			//.
			//.etc
			//lines for tb should determined by N itmes(colname)
			Valuetb = Text Box( "Value: " || Char( Value )),
			sb = Slider Box( 
				min, max, Value, 
				Valuetb << set text( "Value: " || Char( Value ));
				:Parameter A << set each value(Value);
				tb1 << set text(char(colname[1]) || ": " || char(:expr(nameexpr(ascolumn(colname[1])))[1]));
				tb2 << set text(char(colname[2]) || ": " || char(:expr(nameexpr(ascolumn(colname[2])))[1]));
				tb3 << set text(char(colname[3]) || ": " || char(:expr(nameexpr(ascolumn(colname[3])))[1]))			
			) // when I move the slider box, values of all columns in colname will be changed, so I do set text for all columns in colname 
		)	
	)		
))

 

1 ACCEPTED SOLUTION

Accepted Solutions
jthi
Super User

Re: Create different number of text boxes based on variable

Store references to list, build the "collector" for text boxes outside new window and I would also move script for Slider Box outside the slider box into separate function/expression.

 

Is this what you need?

Names Default To Here(1);

dt = Open("$SAMPLE_DATA/Big Class.jmp");

colnames = {"name", "height", "weight"}; //it's a list of column names from previous code, number of items is not fixed

min = 0;
max = 10;
Value = 0;

lub =  Lineup Box(N Col(1));
tbs = {};
For Each({colname}, colnames,
	text_to_add = Eval Insert("^colname^: ^dt[1, colname]^");
	lub << Append(tb = Text Box(text_to_add));
	Insert into(tbs, tb);
);

If(N Items(tbs) != N Items(colnames),
	Throw("!Fatal error");
);

update_tbs = Expr(
	For Each({colname, idx}, colnames,
		text_to_set = Eval Insert("^colname^: ^dt[1, colname]^");
		tbs[idx] << Set Text(text_to_set);
	);
);

nw = New Window( "parameter setting",
	 Panel Box( "parameter setting",
		lub,
		Valuetb = Text Box( "Value: " || Char( Value )),
		sb = Slider Box(
			min, max, Value,
			Valuetb << set text( "Value: " || Char( Value ));
			:age << set each value(Value);
			update_tbs;
		)
	);
);
-Jarmo

View solution in original post

6 REPLIES 6
txnelson
Super User

Re: Create different number of text boxes based on variable

I am not sure this is what you want.  My modifications to your script changes the values to the text boxes in the display window, as you move the slider

names default to here(1);
dt = open("$SAMPLE_DATA/big class.jmp");
colname = {"Name", "Height", "Weight"}; //it's a list of column names from previous code, number of items is not fixed
min = 1;
max = 10;
Value = 1;

Eval(
	Eval Expr(
		nw = New Window( "parameter setting",
			Panel Box( "parameter setting",
				tb1 = Text Box(
					Char( colname[1] ) || ": " || Char( :expr( Name Expr( As Column( colname[1] ) ) )[1] )
				),
				tb2 = Text Box(
					Char( colname[2] ) || ": " || Char( :expr( Name Expr( As Column( colname[2] ) ) )[1] )
				),
				tb3 = Text Box(
					Char( colname[3] ) || ": " || Char( :expr( Name Expr( As Column( colname[3] ) ) )[1] )
				), 
			//.
				//.
				//.
				//.
				//.
				//.etc
				//lines for tb should determined by N itmes(colname)
				Valuetb = Text Box( "Value: " || Char( Value ) ),
				sb = Slider Box(
					min,
					max,
					Value,
					Valuetb << set text( "Value: " || Char( Value ) );
					//:Parameter A << set each value( Value );
					tb1 << set text(
						Char( colname[1] ) || ": " ||char(column( colname[1] )[value] ));
					tb2 << set text(
						Char( colname[2] ) || ": " ||char(column( colname[2] )[value] ));
					tb3 << set text(
						Char( colname[3] ) || ": " ||char(column( colname[3] )[value] ));
				) // when I move the slider box, values of all columns in colname will be changed, so I do set text for all columns in colname 
			)
		)
	)
);
Jim
BayesRabbit7133
Level III

Re: Create different number of text boxes based on variable

Thank you Jim, but this is not what I need, no need to replace (colname[1]) [1] to value

 

What I need is that when the colname list has more items in it, for example: 5, the script can do :

tb1 = 

tb2 = 

tb3 =

tb4 = 

tb5 = 

 

when it has 10 items, then do tb1 ~ tb10

 

jthi
Super User

Re: Create different number of text boxes based on variable

Store references to list, build the "collector" for text boxes outside new window and I would also move script for Slider Box outside the slider box into separate function/expression.

 

Is this what you need?

Names Default To Here(1);

dt = Open("$SAMPLE_DATA/Big Class.jmp");

colnames = {"name", "height", "weight"}; //it's a list of column names from previous code, number of items is not fixed

min = 0;
max = 10;
Value = 0;

lub =  Lineup Box(N Col(1));
tbs = {};
For Each({colname}, colnames,
	text_to_add = Eval Insert("^colname^: ^dt[1, colname]^");
	lub << Append(tb = Text Box(text_to_add));
	Insert into(tbs, tb);
);

If(N Items(tbs) != N Items(colnames),
	Throw("!Fatal error");
);

update_tbs = Expr(
	For Each({colname, idx}, colnames,
		text_to_set = Eval Insert("^colname^: ^dt[1, colname]^");
		tbs[idx] << Set Text(text_to_set);
	);
);

nw = New Window( "parameter setting",
	 Panel Box( "parameter setting",
		lub,
		Valuetb = Text Box( "Value: " || Char( Value )),
		sb = Slider Box(
			min, max, Value,
			Valuetb << set text( "Value: " || Char( Value ));
			:age << set each value(Value);
			update_tbs;
		)
	);
);
-Jarmo
BayesRabbit7133
Level III

Re: Create different number of text boxes based on variable

Thank you Jarmo, this is exactly what I need

May I ask why you would prefer to move script for Slider Box outside the slider box ?

jthi
Super User

Re: Create different number of text boxes based on variable

It gives additional (and fairly easy) layer of separation to your script and it is easier to modify these small blocks of code than the massive new window (you don't have large new window yet but they can quickly get very lengthy). Sometimes it could cause some issues by "losing" the references but I think it is still worth it.

-Jarmo
BayesRabbit7133
Level III

Re: Create different number of text boxes based on variable

Thank you, I got it.

Also I learned that you use eval insert to replace my original long long functions. 

Always appreciate for your kind sharing