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.
Hi Fabian,
I tested this in a small sample and the SelectionStart value was correct in my test. I have heard of this issue before and I think it might be related to specific types of formatting. Can you provide the Value (full html-like string) for your UltraFormattedTextEditor? I will incorporate your value string into my sample and let you know the result.
Alternatively, you can provide a reproduction sample and I will investigate that.
Please let me know if you have any questions.
Hi Mike,
this is whats inside the Value property of the Editor:
"<span style=\"color:Red; font-weight:bold;\">@{<br/>foreach (var obj in Tickets){<br/>&edsp;&edsp;&edsp;&edsp;print format ("Artikel: {0}", obj.ArticleName);<br/>&edsp;&edsp;&edsp;&edsp;print format ("Anzahl: {0}", obj.Amount);<br/>&edsp;&edsp;&edsp;&edsp;print format ("Einzelpreis {0}", obj.SinglePrice);<br/>&edsp;&edsp;&edsp;&edsp;print format ("Gesamtpreis {0}", obj.Price);<br/>&edsp;&edsp;}<br/>}</span><br/><span style=\"color:DarkGreen; font-weight:bold;\">{TicketsList}</span>"
And this is what i got inside the Text property:
"@{\r\nforeach (var obj in Tickets){\r\n print format (\"Artikel: {0}\", obj.ArticleName);\r\n print format (\"Anzahl: {0}\", obj.Amount);\r\n print format (\"Einzelpreis {0}\", obj.SinglePrice);\r\n print format (\"Gesamtpreis {0}\", obj.Price);\r\n }\r\n}\r\n{TicketsList}"
This is happening because of the use of <br> tags in the Value. I talked with Mike Saltzman about this and he explained that the SelectionStart property is not intended to necessarily match up to the Text property. SelectionStart deals with valid cursor positions only, so when you have text with a <BR/> Tag in it, the <BR/> represents a single caret position, but when you translate the Value into Text, it becomes a \r\n which means it is 2 positions. Of course, the position between the carriage return and the newline is not a valid cursor position, so it's not counted as far as SelectionStart is concerned.
To help developers get around this, Mike added a new method to EditInfo called GetSubstring. This method is similar to the Substring method on System.String. It takes a startIndex and an optional Length and returns the text at that point. It will correctly handle the case where you pass in the SelectionStart as the startingIndex.
You could use code like below to get a string that starts from the text cursor position:
string substring = ultraFormattedTextEditor1.EditInfo.GetSubstring(ultraFormattedTextEditor1.EditInfo.SelectionStart);
Please try this out and let me know if it works for you, or if you have any further questions about our implementation.
Thank you for you´r help.
With the GetSubstring method everything works fine for me.