Forum Xamarin.Mac

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

NSTextField with Formatter is not displayed with IntValue assignment

GT4485GT4485 Member ✭✭

Hello.
In the following code, the NSTextField is assigned with IntValue, and is displayed as empty instead of 123. (With StringValue, or without Formatter, the field is displayed correctly.
Why ?
Regards,
Gilbert

using AppKit;
using CoreGraphics;
using Foundation;
using System;

namespace NSTextFieldValidation
{
    public partial class ViewController : NSViewController
    {
        public ViewController(IntPtr handle) : base(handle) { }
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            var fm = new TestFormatter();
            NSTextField Tbx1 = new NSTextField(new CGRect(10, 50, 100, 30));
            View.AddSubview(Tbx1);
            Tbx1.Formatter = fm;
            //Tbx1.StringValue = "123";
            Tbx1.IntValue = 123;
        }
    }
    public class TestFormatter : NSFormatter
    {
        public override bool GetObjectValue(out NSObject obj, string str, out NSString error)
        {
            error = null;
            obj = new NSString(str);
            return true;
        }
        public override bool IsPartialStringValid(string partialString, out string newString, out NSString error)
        {
            error = (NSString)string.Empty;
            newString = partialString;

            if (newString.Length % 3 != 0)
            {
                error = new NSString("La longueur n'est pas multiple de 3");
                return false;
            }
            return true;
        }
        public override string StringFor(NSObject value)
        {
            var text = value as NSString;
            return text ?? string.Empty;
        }
    }
}
Sign In or Register to comment.