解决TextBox多层元素读取的问题

This commit is contained in:
小红帽 2024-01-24 17:25:48 +08:00
parent e91ba9d8bc
commit e85766dcc5
2 changed files with 42 additions and 10 deletions

View File

@ -1442,24 +1442,40 @@ namespace CPF.Controls
sb.Append("<pre>"); sb.Append("<pre>");
StringBuilder textSb = new StringBuilder(); StringBuilder textSb = new StringBuilder();
IDocumentContainer documentContainer = Document; IDocumentContainer documentContainer = Document;
var l = Math.Min(caretIndex.Count, selectionEnd.Count); var cs = caretIndex.ToList();
var ss = selectionEnd.ToList();
var l = Math.Max(cs.Count, ss.Count);
if (cs.Count < l)
{//假如两个索引层级不一样,要补充索引
for (int i = 0; i < l - cs.Count; i++)
{
cs.Add(0);
}
}
if (ss.Count < l)
{
for (int i = 0; i < l - ss.Count; i++)
{
ss.Add(0);
}
}
for (int i = 0; i < l; i++) for (int i = 0; i < l; i++)
{ {
if (caretIndex[i] > selectionEnd[i]) if (cs[i] > ss[i])
{ {
GetText(sb, textSb, i, documentContainer, caretIndex, selectionEnd); GetText(sb, textSb, i, documentContainer, cs, ss);
break; break;
} }
else if (caretIndex[i] < selectionEnd[i]) else if (cs[i] < ss[i])
{ {
GetText(sb, textSb, i, documentContainer, selectionEnd, caretIndex); GetText(sb, textSb, i, documentContainer, ss, cs);
break; break;
} }
else else
{ {
if (i < l - 1) if (i < l - 1)
{ {
documentContainer = documentContainer.Children[(int)caretIndex[i]] as IDocumentContainer; documentContainer = documentContainer.Children[(int)cs[i]] as IDocumentContainer;
if (documentContainer == null) if (documentContainer == null)
{ {
break; break;
@ -1499,7 +1515,23 @@ namespace CPF.Controls
sb.Append("<pre>"); sb.Append("<pre>");
StringBuilder textSb = new StringBuilder(); StringBuilder textSb = new StringBuilder();
IDocumentContainer documentContainer = Document; IDocumentContainer documentContainer = Document;
var l = Math.Min(start.Count, end.Count); start = start.ToList();
end = end.ToList();
var l = Math.Max(start.Count, end.Count);
if (start.Count < l)
{//假如两个索引层级不一样,要补充索引
for (int i = 0; i < l - start.Count; i++)
{
start.Add(0);
}
}
if (end.Count < l)
{
for (int i = 0; i < l - end.Count; i++)
{
end.Add(0);
}
}
for (int i = 0; i < l; i++) for (int i = 0; i < l; i++)
{ {
if (start[i] > end[i]) if (start[i] > end[i])

View File

@ -663,8 +663,8 @@ namespace CPF.Controls
dc.FillRectangle(backBrush, backRect); dc.FillRectangle(backBrush, backRect);
} }
} }
lastOffset = line.X + line.Width; lastOffset = document.Left + line.X + line.Width;
lastTop = line.Y; lastTop = document.Top + line.Y;
} }
foreach (var line in documentContainer.Lines) foreach (var line in documentContainer.Lines)
{ {
@ -802,6 +802,6 @@ namespace CPF.Controls
timer.Dispose(); timer.Dispose();
} }
} }
} }