8
$\begingroup$

Let's assume I want a button that, when you click it, get the cell that contains it selected. This approach won't work, it selects the cell containg the selection previous to the click.

Button["Select this cell", SelectionMove[ButtonNotebook[], All, Cell]]
$\endgroup$

2 Answers 2

11
$\begingroup$

Try:

Button["Select this cell", SelectionMove[ButtonNotebook[], All, EvaluationCell]]
$\endgroup$
2
  • $\begingroup$ This is better than mine. +1 $\endgroup$
    – halirutan
    Mar 15, 2012 at 2:59
  • 1
    $\begingroup$ @halirutan I just browsed the help ... beginner's luck :) $\endgroup$ Mar 15, 2012 at 3:04
1
$\begingroup$

What you could use is NotebookFind to search inside your notebook for something unique which distinguishes your Button from all other.

Here I looked at the cell-structure of your Button and used it to make a small function which creates exactly this button-cell with the exception, that it adds a CellTag. The Button itself just searches for this CellTag and selects the Cell where it is found. As a unique string I used simply the creation-time of the button:

MakeSelectionButton[] := With[{t = DateString[]},
  CellPrint@Cell[BoxData[
      ButtonBox["\<\"Select this cell\"\>",
        Appearance -> Automatic,
        ButtonFunction :> NotebookFind[
            ButtonNotebook[], t, All, CellTags],
        Evaluator -> Automatic,
        Method -> "Preemptive"]], "Output", CellTags -> t]
  ]

MakeSelectionButton[]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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