Discussion Closed This discussion was created more than 6 months ago and has been closed. To start a new discussion with a link back to this one, click here.

How to create a "toggle" in application builder

Please login with a confirmed email address before reporting spam

Hi there,

I am building an application and I would like to include a control to toggle the state of filters in plots. I have created the methods using code similar to the following:

model.result("pg5").feature("surf1").feature("filt1").active(true);
model.result("pg3").feature("arws1").feature("filt1").active(true);
model.result("pg3").feature("surf1").feature("filt1").active(true);
and
model.result("pg5").feature("surf1").feature("filt1").active(false);
model.result("pg3").feature("arws1").feature("filt1").active(false);
model.result("pg3").feature("surf1").feature("filt1").active(false);

I can assign buttons to each of these methods and it does work as expected, but this is not a nice UI for the end user. I would much rather have a radio button or some other control that toggled between the filters being enabled and disabled. Even better would be if the button image changed depending on the current state of the filters.

I am pretty sure it would involve an "if" statement to determine the current state of the filters and then depending on the state, the button that reflects the "other" option would be made visible. i.e. if the filters were currently disabled, a button would show to enable the filters and the button to disable the filters would be hidden. (I am thinking these 2 buttons could sit directly on top of eachother in the UI, is this feasible)?

I am thinking this would be an easy thing for someone familiar with coding... sadly, I am not an experienced coder so would welcome any pointers! I can think of several situations where a toggle for a certain setting would be useful, so hopefully the feedback will help in other sutuations too.

Thanks in advance for any feedback, I can post my model if it would help.

Mark

4 Replies Last Post 11 févr. 2016, 03:57 UTC−5
Edgar J. Kaiser Certified Consultant

Please login with a confirmed email address before reporting spam

Posted: 8 years ago 10 févr. 2016, 11:59 UTC−5
Mark,

I think you can indeed use a radio button and put the code lines you already worked out into a local method (local to the radio button).
You would indeed use the newValue that gets passed to the local method to control some if..else or case coding.
You won't get around acquiring some coding experience if you want to realize such stuff in applications, but it is worth the effort.

Cheers
Edgar


--
Edgar J. Kaiser
emPhys Physical Technology
www.emphys.com
Mark, I think you can indeed use a radio button and put the code lines you already worked out into a local method (local to the radio button). You would indeed use the newValue that gets passed to the local method to control some if..else or case coding. You won't get around acquiring some coding experience if you want to realize such stuff in applications, but it is worth the effort. Cheers Edgar -- Edgar J. Kaiser emPhys Physical Technology http://www.emphys.com

Please login with a confirmed email address before reporting spam

Posted: 8 years ago 10 févr. 2016, 12:15 UTC−5
Hi Edgar,

Many thanks for the response.

As is typical, as soon as I posted the problem, I had a new idea how to achieve my desired result. Having tested this out, it seems to work.

Basically I created a new string variable called "filter_state" with a default value of "enabled".


I modified the methods I posted above to set a value for the "filter_state" string as seen below for the "disable_filters" method:

model.result("pg5").feature("surf1").feature("filt1").active(false);
model.result("pg3").feature("arws1").feature("filt1").active(false);
model.result("pg3").feature("surf1").feature("filt1").active(false);
filter_state = "disabled";

and below for the "enable_filters" method:

model.result("pg5").feature("surf1").feature("filt1").active(true);
model.result("pg3").feature("arws1").feature("filt1").active(true);
model.result("pg3").feature("surf1").feature("filt1").active(true);
filter_state = "enabled";

I then create a new method called "toggle_filters" as below:

if (filter_state.equals("enabled")) {
disable_filters();
}
else {
enable_filters();
}

I then created my button which simply invokes the "toggle_filters" method above. Right next to this button in the UI, I added an information card control which reads the string variable "filter_state" and displays some text and an icon that changes depending on the value of the "filter_state" string variable.

This pretty much achieves what I set out to and seems to work pretty well. The only thing is that the button itself does not change to reflect the "filter_state", but it is clear from the neighbouring info card what the state is. I will endeavour to develop the code such that the button image/text itself changes... just to satisfy my own pursuit of perfection!

Thanks again for your input...

Mark
Hi Edgar, Many thanks for the response. As is typical, as soon as I posted the problem, I had a new idea how to achieve my desired result. Having tested this out, it seems to work. Basically I created a new string variable called "filter_state" with a default value of "enabled". I modified the methods I posted above to set a value for the "filter_state" string as seen below for the "disable_filters" method: model.result("pg5").feature("surf1").feature("filt1").active(false); model.result("pg3").feature("arws1").feature("filt1").active(false); model.result("pg3").feature("surf1").feature("filt1").active(false); filter_state = "disabled"; and below for the "enable_filters" method: model.result("pg5").feature("surf1").feature("filt1").active(true); model.result("pg3").feature("arws1").feature("filt1").active(true); model.result("pg3").feature("surf1").feature("filt1").active(true); filter_state = "enabled"; I then create a new method called "toggle_filters" as below: if (filter_state.equals("enabled")) { disable_filters(); } else { enable_filters(); } I then created my button which simply invokes the "toggle_filters" method above. Right next to this button in the UI, I added an information card control which reads the string variable "filter_state" and displays some text and an icon that changes depending on the value of the "filter_state" string variable. This pretty much achieves what I set out to and seems to work pretty well. The only thing is that the button itself does not change to reflect the "filter_state", but it is clear from the neighbouring info card what the state is. I will endeavour to develop the code such that the button image/text itself changes... just to satisfy my own pursuit of perfection! Thanks again for your input... Mark

Magnus Ringh COMSOL Employee

Please login with a confirmed email address before reporting spam

Posted: 8 years ago 11 févr. 2016, 03:55 UTC−5
Hi,

Just to add to this thread, for your information. From version 5.2, there is a Toggle button form object that you can use for this purpose. It can be connected to either a Boolean or a String declaration variable, which will be automatically toggled when the button is selected or deselected. For Strings, the variable will switch between the strings "on" and "off". Or you can use a Boolean variable with the values true and false.

Best regards,
Magnus Ringh, COMSOL
Hi, Just to add to this thread, for your information. From version 5.2, there is a Toggle button form object that you can use for this purpose. It can be connected to either a Boolean or a String declaration variable, which will be automatically toggled when the button is selected or deselected. For Strings, the variable will switch between the strings "on" and "off". Or you can use a Boolean variable with the values true and false. Best regards, Magnus Ringh, COMSOL

Please login with a confirmed email address before reporting spam

Posted: 8 years ago 11 févr. 2016, 03:57 UTC−5
Aaah... another reason to update to 5.2, will have to get our IT department to update my machine...

Thanks for the feedback!

Mark
Aaah... another reason to update to 5.2, will have to get our IT department to update my machine... Thanks for the feedback! Mark

Note that while COMSOL employees may participate in the discussion forum, COMSOL® software users who are on-subscription should submit their questions via the Support Center for a more comprehensive response from the Technical Support team.