Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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]]
share|improve this question

2 Answers 2

up vote 11 down vote accepted

Try:

Button["Select this cell", SelectionMove[ButtonNotebook[], All, EvaluationCell]]
share|improve this answer
    
This is better than mine. +1 –  halirutan Mar 15 '12 at 2:59
1  
@halirutan I just browsed the help ... beginner's luck :) –  belisarius Mar 15 '12 at 3:04

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[]
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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