title: c# iOS Can NOT position cursor in UITextField in position 1
body: <p>No matter what I do, I can NOT set the cursor to position 1 in this string <code>(___) ___-____</code> position 1 is the position immediately following the opening <code>(</code>.  I'm doing this in the <code>EditingStarted</code> method of the delegate, As a guide, I'm following the code <a href="https://stackoverflow.com/questions/53083394/c-sharp-xamarin-uitextfield-setting-the-cursor-position">here</a>.  currently my code is:</p>
  <pre><code>    public override void EditingStarted(UITextField textField)
      {
          if (MyParent.EditMask != &quot;&quot;)
          {
              textField.Text = MyParent.EditMask.Replace(&quot;#&quot;, &quot;_&quot;);
              // Set cursor position
              NSRange therange = new NSRange(index, 0);
              UITextPosition start = textField.GetPosition(textField.BeginningOfDocument, therange.Length - 1);
              UITextPosition end = textField.GetPosition(start, therange.Length);
              textField.SelectedTextRange = textField.GetTextRange(start, end);
          }
      }
  </code></pre>
  <p>The cursor ALWAYS appears immediately following the closing <code>)</code>, nothing I do changes that.  I have no idea why. I've tried getting the position of the first underscore:</p>
  <pre><code>int position = textField.Text.IndexOf(&quot;_&quot;);
  NSRange therange = new NSRange(position, 0);
  </code></pre>
  <p>But again, that results in the cursor being positioned  immediately after the closing <code>)</code>.  Anyone see what I'm doing wrong?</p>
  <p>**** Update ****</p>
  <p>Just so folks understand the context, The code above is part of a class called <code>UIMaskedTextField</code>that I created to handle all of my mask/text input formating in the user interface.  That class is:</p>
  <pre><code>class UIMaskedTextField : UITextField
  {
      public string EditMask { get; set; }
  
      public UIMaskedTextField()
      {
          this.Delegate = new MaskTextViewDelegate(this);
      }
  
  }
  class MaskTextViewDelegate : UITextFieldDelegate
  {
      private UIMaskedTextField MyParent;
      int index = 0;
      public MaskTextViewDelegate(UIMaskedTextField parent)
      {
          MyParent = parent;
      }
  
      public override void EditingStarted(UITextField textField)
      {
          if (MyParent.EditMask != &quot;&quot;)
          {
              textField.Text = MyParent.EditMask.Replace(&quot;#&quot;, &quot;_&quot;);
              // Set cursor position
              int position = textField.Text.IndexOf(&quot;_&quot;);
  
              NSRange therange = new NSRange(position, 0);
              UITextPosition start = textField.GetPosition(textField.BeginningOfDocument, therange.Location);
              UITextPosition end = textField.GetPosition(start, therange.Length);
              textField.SelectedTextRange = textField.GetTextRange(start, end);
  
          }
      }
      public override bool ShouldChangeCharacters(UITextField textField, NSRange range, string replacementString)
      {
          int fieldlength = 10; // MyParent.EditMask.Length;
          string text = textField.Text;
          int NeedASpace = MyParent.EditMask.IndexOf(&quot; &quot;);
          string newText = &quot;&quot;;
          if (text == &quot;&quot;)
          {
              newText = MyParent.EditMask.Replace(&quot;#&quot;, &quot;_&quot;);
          } else
          {
              newText = text;
          }
  
          string totalChar = newText.Replace(&quot; &quot;, &quot;&quot;);
          totalChar = totalChar.Replace(&quot;(&quot;, &quot;&quot;);
          totalChar = totalChar.Replace(&quot;)&quot;, &quot;&quot;);
          totalChar = totalChar.Replace(&quot;-&quot;, &quot;&quot;);
          totalChar = totalChar.Replace(&quot;_&quot;, &quot;&quot;);
  
          int val;
          if ((totalChar + replacementString).Length &lt;= fieldlength) {
              if (replacementString != &quot;&quot;)
              {
                  index = newText.IndexOf(&quot;_&quot;);
                  StringBuilder sb = new StringBuilder(newText);
                  char character = char.Parse(replacementString);
                  sb[index] = character;
                  newText = sb.ToString();
  
  
                  textField.Text = newText;
  
                  // Set cursor to next position, this works flawlessly
                  NSRange therange = new NSRange(index, 0);
                  UITextPosition start = textField.GetPosition(textField.BeginningOfDocument, therange.Location + 1);
                  UITextPosition end = textField.GetPosition(start, therange.Length);
                  textField.SelectedTextRange = textField.GetTextRange(start, end);
  
                  newText = &quot;&quot;;
              }
              else
              { // Still working the backspace key, so not done here yet.
                  if (text.Length &gt; 1)
                  {
                      newText = text.Substring(0, text.Length - 1);
                  }
                  else
                  {
                      newText = &quot;&quot;;
                  }
              }
          }
  
          return Int32.TryParse(newText, out val);
      }
  }
  </code></pre>