Selecting an NSTextField

  • Hello:

    I'm looking for a way to programmatically select an NSTextField so it
    is ready to be edited.  I've found a way to do this with an
    NSTextView, as such:

    [theWindow makeFirstResponder: theTextView];
    [theTextView setSelectedRange:NSMakeRange(0,0)];

    However, I'm not having luck finding a way to do a similar thing with
    an NSTextField.  I can "highlight" the text field with a line of code
    like this:

    [[self window] makeFirstResponder: myTextField];

    But this does not put the cursor in the field so I can start typing
    immediately.  What am I missing?  It seems that this should be fairly
    easy to do, but I'm just not finding the answer.

    - Chad
  • On 4 Mar 2008, at 5:21 AM, Chad Armstrong wrote:

    > Hello:
    >
    > I'm looking for a way to programmatically select an NSTextField so
    > it is ready to be edited.  I've found a way to do this with an
    > NSTextView, as such:
    >
    > [theWindow makeFirstResponder: theTextView];
    > [theTextView setSelectedRange:NSMakeRange(0,0)];
    >
    >
    > However, I'm not having luck finding a way to do a similar thing
    > with an NSTextField.  I can "highlight" the text field with a line
    > of code like this:
    >
    > [[self window] makeFirstResponder: myTextField];
    >
    > But this does not put the cursor in the field so I can start typing
    > immediately.  What am I missing?  It seems that this should be
    > fairly easy to do, but I'm just not finding the answer.
    >
    > - Chad

    You can use -selectText:. Read the documentation about the field
    editor <http://developer.apple.com/documentation/Cocoa/Conceptual/TextArchitecture/
    Concepts/TextFieldsAndViews.html
    > what you're missing.

    Christiaan
  • >>

    Hello:

    I tried using -selectText: as well, but it wasn't working for me.  I
    looked a little closer at my code to see if something was taking away
    the focus, and indeed it was!  So, I was able to use the [myTextField
    selectText: self]; and that worked perfectly.  Something that should
    have been fairly trivial was, it just was other matters that made it
    difficult.

    Thanks for the help.

    > You can use -selectText:. Read the documentation about the field
    > editor <http://developer.apple.com/documentation/Cocoa/Conceptual/TextArchitecture/
    Concepts/TextFieldsAndViews.html
    > > what you're missing.

    Regards,

    Chad
previous month march 2008 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Go to today