Created
December 19, 2016 20:38
-
-
Save chamons/2195b5072ecb50aecac7e44bc91ffd1f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using AppKit; | |
using CoreGraphics; | |
using Foundation; | |
using System.Runtime.InteropServices; | |
namespace TestFormatter | |
{ | |
[Register ("AppDelegate")] | |
public class AppDelegate : NSApplicationDelegate | |
{ | |
public AppDelegate () | |
{ | |
} | |
public override void DidFinishLaunching (NSNotification notification) | |
{ | |
} | |
public override void WillTerminate (NSNotification notification) | |
{ | |
// Insert code here to tear down your application | |
} | |
} | |
class NumericFormatter : NSNumberFormatter | |
{ | |
[Export ("isPartialStringValid:proposedSelectedRange:originalString:originalSelectedRange:errorDescription:")] | |
public bool IsPartialStringValid (ref NSString partialString, out NSRange proposedSelRange, string origString, NSRange origSelRange, out NSString error) | |
{ | |
proposedSelRange = new NSRange (); | |
error = new NSString (); | |
if (partialString.Length == 0) | |
{ | |
error = new NSString ("there are no value"); | |
AppKitFramework.NSBeep (); | |
return false; | |
} | |
int number; | |
if (!int.TryParse (partialString, out number)) | |
{ | |
error = new NSString ("not number"); | |
AppKitFramework.NSBeep (); | |
return false; | |
}; | |
partialString = (NSString)(partialString + " - Test"); | |
proposedSelRange = origSelRange; | |
return false; | |
} | |
} | |
[Register ("NumericTextField")] | |
public class NumericTextField : NSTextField | |
{ | |
// Called when created from unmanaged code | |
public NumericTextField (IntPtr handle) : base (handle) | |
{ | |
Initialize (); | |
} | |
[Export ("init:")] | |
public NumericTextField (CGRect rect) : base (rect) | |
{ | |
Initialize (); | |
} | |
// Called when created directly from a XIB file | |
[Export ("initWithCoder:")] | |
public NumericTextField (NSCoder coder) : base (coder) | |
{ | |
Initialize (); | |
} | |
public bool IsValid () | |
{ | |
return ((NumericFormatter)Formatter).NumberFromString (StringValue) != null; | |
} | |
void Initialize () | |
{ | |
Formatter = new NumericFormatter (); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment