Skip to content

Instantly share code, notes, and snippets.

@chamons
Created December 19, 2016 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chamons/2195b5072ecb50aecac7e44bc91ffd1f to your computer and use it in GitHub Desktop.
Save chamons/2195b5072ecb50aecac7e44bc91ffd1f to your computer and use it in GitHub Desktop.
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