1
$\begingroup$

I really need some professional help here! :)

I have 2 buttons. They are working, but I am using a list and whenever there is a duplication in the list (same word) the button does not work properly (as the system does not realize that the second word is a different word, so that everything gets switched back to the first word).

Example:

list = {"time", "Snow", "mother", "queen", "stepmother", "magic", "day", "queen", "huntsman"}

I have a duplication of the word "queen" in the list. So that, when I am just on the second "queen" and I click "Next", I'll get "stepmother" and not "huntsman" because of the duplication of the word in the list.

Code:

 DynamicModule[{},

        Column[{

        InputField[Dynamic[f], String, MenuList -> list, 
         Alignment -> {Left, Center}], 

    Button["Previous", 
     Dynamic[f = 
       list[[Max@{Position[list, f][[1, 1]] + 1, 
          Length@list}]]], Alignment -> {Right, Center}, 
     ImageSize -> Automatic],

    Button["Next", 
     Dynamic[f = 
       list[[Min@{Position[list, f][[1, 1]] + 1, 
          Length@list}]]], Alignment -> {Right, Center}, 
     ImageSize -> Automatic]

    }]]

Do you know any solution for this problem?

Thank you very much,

Fanni

$\endgroup$
1
$\begingroup$

Some form of index is required. However it needs to not show in the MenuList. Using Interpretation does the trick. The input field can still be used to type in the input.

list = {"time", "Snow", "mother", "queen", "stepmother",
   "magic", "day", "queen", "huntsman"};

list2 = MapIndexed[Interpretation[#1, {First[#2], #1}] &, list];


DynamicModule[{},
 Column[
  {InputField[Dynamic[f,
     ({i, f} = If[StringQ[#], {0, #}, Last[#]]) &],
    String, MenuList -> list2, Alignment -> {Left, Center}],

   Button["Previous", If[i == 0,
     cases = Cases[Last /@ list2, {_, f}];
     If[cases != {},
      i = Max[1, cases[[1, 1]] - 1];
      f = Cases[Last /@ list2, {i, _}][[1, 2]]],
     i = Max[1, i - 1];
     f = Cases[Last /@ list2, {i, _}][[1, 2]]],
    Alignment -> {Right, Center}, ImageSize -> Automatic],

   Button["Next", If[i == 0,
     cases = Cases[Last /@ list2, {_, f}];
     If[cases != {},
      i = Min[Length[list2], cases[[1, 1]] + 1];
      f = Cases[Last /@ list2, {i, _}][[1, 2]]],
     i = Min[Length[list2], i + 1];
     f = Cases[Last /@ list2, {i, _}][[1, 2]]],
    Alignment -> {Right, Center}, ImageSize -> Automatic]

   }]]

enter image description here

$\endgroup$
1
$\begingroup$

I think the following code solves your problem:

list={"time","Snow","mother","queen","stepmother","magic","day","queen","huntsman"};
f=list[[1]];
{Button["Previous", 
  list=RotateRight[list]; f=list[[1]]],
Button["Next", 
  list=RotateLeft[list]; f=list[[1]]]}
Dynamic[f]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.