Question

I am trying to utilise the CHANGED event of an EntryElement in Monotouch.Dialog so that I can get the length of any input string, and act on it, once it reaches a certain length, for example

RootElement root = new RootElement (null);
Section pinSection = new Section ("", "Please enter your 6 digit pin");
EntryElement pin = new EntryElement("Enter your pin","","",false);
pin.KeyboardType = UIKeyboardType.NumberPad;

pin.Changed += (object sender, EventArgs e) => {
Console.WriteLine("Pin Changed to {0}, Length: 1}",pin.Value,pin.Value.ToString().Length);
                };

The CHANGED event does not fire when the value is updated. It only fires when the user stops editing, and the entry element loses focus.

Is there any way to attach an event so that we can respond to invidual keystroke changes to the entry element value?

Was it helpful?

Solution

Instead of using the Changed event, I would subclass the EntryElement and override the CreateTextMethod. I use this as a integer only EntryElement, you should be able to adapt this for your task by adding your own event that would get fired when the text length was reached

public class IntegerEntryElement : EntryElement
{
    public IntegerEntryElement (string c, string p, string v) : base(c, p, v)
    {
        _maxLength = 0;
        TextAlignment = UITextAlignment.Right;
    }

    static NSString cellKey = new NSString("IntegerEntryElement");

    protected override NSString CellKey { get { return cellKey; } }
    private int _maxLength;
    public int MaxLength
    {
        get { return _maxLength; }
        set
        {
            if ((value >= 0) || (value <= 10))
            {
                _maxLength = value;
            }
        }
    }

    public int IntValue
    {
        get
        {
            int intValue = 0;
            Int32.TryParse(Value, out intValue);
            return intValue;
        }
        set
        {
            Value = value.ToString();
        }

    }

    public void Clear()
    {
        Value = "";
    }

    protected override UITextField CreateTextField (RectangleF frame)
    {
        RectangleF newframe = frame;
        newframe.Width -= 10;

        UITextField TextField = base.CreateTextField (newframe);

        TextField.KeyboardType = UIKeyboardType.NumberPad;
        TextField.ClearButtonMode = UITextFieldViewMode.WhileEditing;

        TextField.ShouldChangeCharacters = (UITextField textField, NSRange range, string replacementString) =>
        {
            bool result = true;
            string filter="0123456789";

            result = (filter.Contains(replacementString) || replacementString.Equals(string.Empty));

            if ((result) && (MaxLength > 0))
            {
                result = textField.Text.Length + replacementString.Length - range.Length <= MaxLength;
            }

            return result;
        };
        return TextField;
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top