Hello,

Cocoa discourages the subclassing of NSTextField, so delegation to a
formatter is much more better.

For your purpose, you can use the NSNumberFormatter or create your own
NSFormatter subclass. See
http://developer.apple.com/DOCUMENTATION/Cocoa/Conceptual/DataFormatting/Articles/CreatingACustomFormatter.html#//apple_ref/doc/uid/20000196or
http://developer.apple.com/DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSFormatter_Class/Reference/Reference.html#//apple_ref/doc/uid/20000204-DontLinkElementID_4for
details. The NSFormatter class gives you to validate and/or alter the
text field content.

Regards, Laurent Etiemble.

2008/12/15 Nick Ifloat <[email protected]>

> Please help me with building an equivalent to the Numbers only input
> validation code for using with NSTextField using Monobjc.
>
> Case: I want the NSTextField to allow the user to input only numbers(0 to
> 9), '.' and '-' symbols.
>
> Request: Well I am stuck. I am able to handle key press events in Windows
> Forms and Gtk#. But I am new to Mac OS X and need your help with building an
> equivalent method to handle key press events on Mac OS X using Monobjc. I am
> providing my sample code of key press event handler using Windows Forms and
> Gtk# bellow. (I have read about NSFormater, NSCell, NSView's keyDown method
> - but I can't seem to find my way on doing the same using Monobjc!)
>
> Provided: 1. Windows Forms & 2. Gtk# numbers only input sample code.
>
> Need: Monobjc code for numbers only input validation sample code.
>
> //Windows Forms Code to handle KeyPressEvents
>
> private void OnlyNumbers(KeyPressEventArgs e)
>        {
>            char c = e.KeyChar;
>            if (!(c >= '0' && c <= '9' || c == '.' || c == '-' || c == 8))
> //8 is the keycode of the backspace key
>            {
>                e.Handled = true;
>            }
>        }
>
> //Then I am calling the method on the text fields KeyPress handler
>
> private void txtNumber_KeyPress(object sender, KeyPressEventArgs e)
>        {
>                OnlyNumbers(e);
>        }
>
> //End of Windows Forms Code
>
> //Gtk# Code to handle/capture & kill Unwanted Keys
>
>       private void OnlyNumbers(object o, Gtk.TextInsertedArgs
> args)//[GLib.ConnectBefore]Gtk.
>        {
>                        try
>                        {
>                                Console.WriteLine("text : " +
> args.Text.ToString());
>                                int pos = ((Entry)o).Position;
>                                string c = ((Entry)o).GetChars(pos, pos+1);
>                                char d;
>                                Char.TryParse(c[0].ToString(), out d);
>                                if (!(d >= '0' && d <= '9' || d == '.' || d
> == '-'))
>                                {
>                                        ((Entry)o).SelectRegion(pos, pos+1);
>                                        ((Entry)o).DeleteSelection();
>                                }
>                        }
>                        catch (System.IndexOutOfRangeException e) {}
>        }
>
> //Then I am passing the method to the textfields OnTextInserted event
> handler
>
> protected virtual void txtNumber_OnTextInserted (object o,
> Gtk.TextInsertedArgs args)
>        {
>                        OnlyNumbers(o, args);
>        }
> //End of Gtk# code
>

Reply via email to