brad
(Brad Varey)
April 30, 2015, 12:19pm
1
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
kubapod
(Kuba Podkalicki)
May 1, 2015, 8:20am
2
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"]]]
)]
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
b3m2a1
(b3m2a1 โโ)
March 15, 2018, 9:05pm
3
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