Message Boards Message Boards

GROUPS:

How Do I Set the Focus To a Given Control in a DialogInput[] Statement?

Posted 8 years ago
7587 Views
|
2 Replies
|
4 Total Likes
|

I have the following code:

DialogInput[{name = "", id = ""}, 
  Grid[{{"Name: ", 
     InputField[Dynamic[name], String, 
      FieldHint -> "Enter the name"]}, {"ID: ", 
     InputField[Dynamic[id], String, 
      FieldHint -> "Enter the three-digit ID"]}, {"", 
     Grid[{{Button["OK", DialogReturn[{name, id}], 
         ImageSize -> Automatic], 
        Button["Cancel", DialogReturn[{}], 
         ImageSize -> Automatic]}}]}}], 
  WindowTitle -> "Add New Record"];

When I execute the statement -- either from within a notebook or via code produced in Workbench -- the dialog pops up, but there is no focus on any control, so I have to use the mouse before I can start typing.

Is there a way to set the focus to the first InputField[], or, better, any control on the DialogInput[]?

Thanks in advance

Brad

POSTED BY: Brad Varey
2 Replies
Posted 5 years ago

Worth noting that this can also be done with the newish FrontEnd`MoveCursorToInputField which appears as if it was introduced to handle the cloud dialog functionality in v10 or maybe v11

POSTED BY: b3m2a1 ​ 

This method is undocumented but used in Input which sets focus on its field automatically.

http://mathematica.stackexchange.com/a/81352/

Moreover it is introduced by WRI quite recently so maybe it will stay.

DialogInput[{name = "", id = ""}, 
 Grid[{{"Name: ",    InputField[Dynamic[name], String,     FieldHint -> "Enter the name"]},
          {"ID: ",    InputField[Dynamic[id], String,    FieldHint -> "Enter the three-digit ID",   
                                             BoxID -> "fieldWithID"]}, 
          {"", 
    Grid[{{Button["OK", DialogReturn[{name, id}],       ImageSize -> Automatic],       Button["Cancel", DialogReturn[{}], 
        ImageSize -> Automatic]}}]}}], 
 WindowTitle -> "Add New Record",
 Initialization :> (
   MathLink`CallFrontEnd[        FrontEnd`BoxReferenceFind[
     FE`BoxReference[InputNotebook[], {{"fieldWithID"}},       FE`BoxOffset -> {FE`BoxChild[1]}, 
      FE`SearchStart -> "StartFromBeginning"]]]
   )]

enter image description here

If you want to avoid undocumented functions, here is a description of approach based on SelectionMove. Probably more stable but not so flexible.

http://mathematica.stackexchange.com/a/1463/5478

POSTED BY: Kuba Podkalicki
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract