我正在尝试找到一种方法来确定RichEditBox(UWP)中特定行中第一个字符的索引
Richtextbox中的很容易
int indx1stlinchr = myRichTextBox.GetFirstCharIndexFromLine(i);
有没有办法做到这一点或解决方法?
Winforms Richtextbox中我的着色方法的一部分
public void colorTheText(string rohtext)
{
myRichTextBox.SelectionLength = 0;
int def = network.SubSop_Deff;
var line = Regex.Split(rohtext, "\n|\r|\n\n");
int ipclassRange = CColor.KlassebitsRange(network.Network_Class, network.Netmask_length);
int k = 1;
int l = 1;
for (int i = 0; i < line.Length; i++)
{
int indx1stlinchr = myRichTextBox.GetFirstCharIndexFromLine(i);
int indexlinedge = line[i].LastIndexOf(" ");
if (line[i].StartsWith(">Network") == true)
{
myRichTextBox.SelectionLength = 0;
myRichTextBox.Select(indx1stlinchr, 12);
myRichTextBox.SelectionColor = Color.Black;
myRichTextBox.Select((indx1stlinchr + 13), 19);
myRichTextBox.SelectionColor = Color.Blue;
if (network.Reserved_IP != null)
{
myRichTextBox.SelectionLength = 0;
myRichTextBox.Select((indx1stlinchr + 76), 13);
myRichTextBox.SelectionColor = Color.Green;
}
if (ipclassRange > 0)
{
myRichTextBox.Select(indx1stlinchr + 39, ipclassRange);
myRichTextBox.SelectionColor = Color.Green;
}
myRichTextBox.SelectionLength = 0;
}
if (line[i].StartsWith("Netmask") == true)
{
myRichTextBox.SelectionLength = 0;
myRichTextBox.Select(indx1stlinchr, 19);
myRichTextBox.SelectionColor = Color.Black;
myRichTextBox.Select((indx1stlinchr + 13), 19);
myRichTextBox.SelectionColor = Color.Blue;
myRichTextBox.Select((indx1stlinchr + 38), 38);
myRichTextBox.SelectionColor = Color.Red;
myRichTextBox.SelectionLength = 0;
if (super == false)
{
indxlinstrt = myRichTextBox.GetFirstCharIndexOfCurrentLine();
if (k == 1)
{
myRichTextBox.Select(indxlinstrt + indexlinedge, (def + 4) * -1);
subrange = CColor.FindeSubRange(Regex.Split(myRichTextBox.SelectedText, ""), network.Netmask_length, def);
k = 0;
}
myRichTextBox.Select(indxlinstrt + indexlinedge, subrange);
myRichTextBox.SelectionColor = Color.DarkViolet;
myRichTextBox.SelectionLength = 0;
}
}//.........etc
}
super = false;
k = 1;
}
答案 0 :(得分:0)
试试这个代码段:
private int GetFirstCharIndexOfLine(ITextDocument document, int line)
{
string value;
document.GetText(TextGetOptions.None, out value);
int size = 0;
while (value.Length > size)
{
ITextRange range = document.GetRange(size, size + 1);
size += range.Expand(TextRangeUnit.Line);
var lineIndex = range.GetIndex(TextRangeUnit.Line);
if (line == lineIndex)
{
//start of range is first line character index
return range.StartPosition;
}
size += 1;
}
return -1;
}