# How can I prevent TabView from resetting the current tab?

This is a simplification of a problem that has arisen in a project regarding Euclidean activities for verifying proportions via operations on pairs of magnitudes. In the project, the LocatorPane is used to enter values for x, y that will be used in carrying out operations on pairs of line segments in order to allow the user to ascertain whether the line segments reflect the ratio, y:x, where x, y are positive integers.

The code below captures the essence of the technical problem I have run into.

I am using LocatorPane to select values (x,y) that will be used in each of the tabs in TabView. However, each time a new locator value is selected, the TabView defaults to the first tab, 1. To test this, click on either tab 2 or tab 3; then set the locator value. TabView will automatically reset to tab 1.

Test Code

Manipulate[
Row[{
TabView[{
1 -> Row[{pt[[1]], pt[[2]]}, "\t"],
2 -> Row[{2*pt[[1]], 2*pt[[2]]}, "\t"],
3 -> Row[{3*pt[[1]], 3*pt[[2]],}, "\t"]}],
LocatorPane[Dynamic@pt, Graphics[{Gray, Disk[]}]]}]]


I tried a variation in which a LocatorPane is placed within each tab, but the reset problem continues to occur.

• – kglr Jun 23 '20 at 22:46
• @kglr. Nice example, from which I can learn. I'm wondering why you didn't have to use Dynamic before TabView, as Michael E2 suggested. – DavidC Jun 23 '20 at 23:57
• DavidC, added Dynamic after an eight year delay:) – kglr Jun 24 '20 at 0:41

Add some Dynamic[] and a variable to keep track of the selected tab:

Manipulate[Row[{
Dynamic@TabView[{
1 -> Pane[Row[{pt[[1]], pt[[2]]}, "\t"], 150],
2 -> Pane[Row[{2*pt[[1]], 2*pt[[2]]}, "\t"], 150],
3 -> Pane[Row[{3*pt[[1]], 3*pt[[2]]}, "\t"], 150]
}, Dynamic@i],
LocatorPane[Dynamic@pt, Graphics[{Gray, Disk[]}]]}], {{pt, {0.,
0.}}, None}, {{i, 1}, None}]


Oh, I added a Pane because it was a pain with the graphics jumping around depending on how many digits were displayed.

The Dynamic@TabView[...] keeps the whole body of the Manipulate from being re-executed when pt is changed (which interrupts dragging the Locator).

• Very nice. However, I am puzzled as to how Mathematica knows that i stands for the current tab. – DavidC Jun 23 '20 at 23:14
• @DavidC From the second argument Dynamic@i. See the first bullet point under "Details" in the docs for TabView; there are examples under "Scope" as well. – Michael E2 Jun 23 '20 at 23:41
• @DavidC I was a little lazy with my explanation: The re-execution (if no Dynamic) does not reset i to 1, so the tab view is preserved; rather, it interrupts dragging the Locator, so it is still necessary. I was misremembering one my trial runs. – Michael E2 Jun 24 '20 at 0:07