修改TextBox的GetString方法

This commit is contained in:
小红帽 2023-12-21 16:58:38 +08:00
parent 906c865964
commit de9f1c95dc
2 changed files with 31 additions and 17 deletions

View File

@ -1477,29 +1477,46 @@ namespace CPF.Controls
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public (string text, string html) GetSelectedString() public (string text, string html) GetSelectedString()
{
return GetString(caretIndex, selectionEnd);
}
/// <summary>
/// 获取所有Html和Text格式内容
/// </summary>
/// <returns></returns>
public (string text, string html) GetString()
{
return GetString(new uint[] { 0 }, new uint[] { (uint)Document.Children.Count });
}
/// <summary>
/// 获取Html和Text格式内容
/// </summary>
/// <returns></returns>
public (string text, string html) GetString(IList<uint> start, IList<uint> end)
{ {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
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 l = Math.Min(start.Count, end.Count);
for (int i = 0; i < l; i++) for (int i = 0; i < l; i++)
{ {
if (caretIndex[i] > selectionEnd[i]) if (start[i] > end[i])
{ {
GetText(sb, textSb, i, documentContainer, caretIndex, selectionEnd); GetText(sb, textSb, i, documentContainer, start, end);
break; break;
} }
else if (caretIndex[i] < selectionEnd[i]) else if (start[i] < end[i])
{ {
GetText(sb, textSb, i, documentContainer, selectionEnd, caretIndex); GetText(sb, textSb, i, documentContainer, end, start);
break; break;
} }
else else
{ {
if (i < l - 1) if (i < l - 1)
{ {
documentContainer = documentContainer.Children[(int)caretIndex[i]] as IDocumentContainer; documentContainer = documentContainer.Children[(int)start[i]] as IDocumentContainer;
if (documentContainer == null) if (documentContainer == null)
{ {
break; break;
@ -1511,17 +1528,6 @@ namespace CPF.Controls
return (textSb.ToString(), sb.ToString()); return (textSb.ToString(), sb.ToString());
} }
/// <summary>
/// 获取选中的内容
/// </summary>
/// <returns></returns>
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("建议用GetSelectedString")]
public (string text, string html) GetString()
{
return GetSelectedString();
}
public virtual void Paste() public virtual void Paste()
{ {
RemoveSelect(); RemoveSelect();

View File

@ -145,8 +145,16 @@ namespace CPF.Controls
} }
return; return;
} }
if (string.IsNullOrEmpty(data))
{
return;
}
var root = Root; var root = Root;
var cm = CommandMessage<object>.DeserializeWithString(data); var cm = CommandMessage<object>.DeserializeWithString(data);
if (cm == null)
{
return;
}
Invoke(() => Invoke(() =>
{ {
if (cm.MessageType == MessageType.GetChildren) if (cm.MessageType == MessageType.GetChildren)