From de9f1c95dc2e8ead79ae6930d0321129134d1330 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E7=BA=A2=E5=B8=BD?= <761716178@qq.com> Date: Thu, 21 Dec 2023 16:58:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9TextBox=E7=9A=84GetString?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CPF/Controls/TextBox.cs | 40 +++++++++++++++++++++++----------------- CPF/Controls/View.cs | 8 ++++++++ 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/CPF/Controls/TextBox.cs b/CPF/Controls/TextBox.cs index 1358f18..dae1456 100644 --- a/CPF/Controls/TextBox.cs +++ b/CPF/Controls/TextBox.cs @@ -1477,29 +1477,46 @@ namespace CPF.Controls /// /// public (string text, string html) GetSelectedString() + { + return GetString(caretIndex, selectionEnd); + } + /// + /// 获取所有Html和Text格式内容 + /// + /// + public (string text, string html) GetString() + { + return GetString(new uint[] { 0 }, new uint[] { (uint)Document.Children.Count }); + } + + /// + /// 获取Html和Text格式内容 + /// + /// + public (string text, string html) GetString(IList start, IList end) { StringBuilder sb = new StringBuilder(); sb.Append("
");
             StringBuilder textSb = new StringBuilder();
             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++)
             {
-                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;
                 }
-                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;
                 }
                 else
                 {
                     if (i < l - 1)
                     {
-                        documentContainer = documentContainer.Children[(int)caretIndex[i]] as IDocumentContainer;
+                        documentContainer = documentContainer.Children[(int)start[i]] as IDocumentContainer;
                         if (documentContainer == null)
                         {
                             break;
@@ -1511,17 +1528,6 @@ namespace CPF.Controls
             return (textSb.ToString(), sb.ToString());
         }
 
-        /// 
-        /// 获取选中的内容
-        /// 
-        /// 
-        [EditorBrowsable(EditorBrowsableState.Never)]
-        [Obsolete("建议用GetSelectedString")]
-        public (string text, string html) GetString()
-        {
-            return GetSelectedString();
-        }
-
         public virtual void Paste()
         {
             RemoveSelect();
diff --git a/CPF/Controls/View.cs b/CPF/Controls/View.cs
index 583ffd5..e5bd630 100644
--- a/CPF/Controls/View.cs
+++ b/CPF/Controls/View.cs
@@ -145,8 +145,16 @@ namespace CPF.Controls
                 }
                 return;
             }
+            if (string.IsNullOrEmpty(data))
+            {
+                return;
+            }
             var root = Root;
             var cm = CommandMessage.DeserializeWithString(data);
+            if (cm == null)
+            {
+                return;
+            }
             Invoke(() =>
             {
                 if (cm.MessageType == MessageType.GetChildren)