Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
15
UltraFormatedTextEditor SelectionStart position seems to be incorrect
posted

Hello,

i'm using the UltraFormatedTextEditor to display some user inserted code. However i need to determine if the cursor position is inside curly braces. Based on that i want to insert values wrapped in braces or not. 

Refering to this forum i tried to use textEditor.EditInfo.SelectionStart to get my cursor position and check the opening and closing braces with very simple code:

    private bool CheckIfInsideBrackets()
    {
      var curserPosition = textEditor.EditInfo.SelectionStart;
      var text = new string(textEditor.Text.Where(c => !char.IsControl(c)).ToArray());
      int countOpen = 0, countClose = 0;

      if(text.Contains("@{"))
      { 
        for (var i = textEditor.Text.IndexOf("@{"); i < curserPosition; i++)
        {
          if (textEditor.Text.ElementAt(i).Equals('{')) { countOpen++; }
          if (textEditor.Text.ElementAt(i).Equals('}')) { countClose++; }
        }
      }
      return countClose != countOpen;
    }

But after all i get a wrong brace count, because the position of the cursor is not correct. I insertet a screenshot to illustrate my problem. As you can see the cursor is after the last closing brace. But the returned SelectionStart is right behind "obj.Price;" so I miss out on the last two braces

I hope someone can help me with this problem.