获取 richtextbox 中段落的段落编号(不是行号)vb.net
get paragraph number (not line number) of paragraph in richtextbox vb.net
我有一个包含大量段落的富文本框。 (仅供参考:所有段落将由 vbCrlf 分隔)
我需要知道光标位置的段落索引。
很容易找到行索引
Dim line, index As Integer
index = rtbScriptRTF.SelectionStart
line = rtbScriptRTF.GetLineFromCharIndex(index)
txtTestLINE.Text = line
这不是我想要的,因为一个段落很容易占据很多行。
疯狂的是......我在这些页面的某个地方看到了一段代码,但我找不到任何地方!
任何人都可以指出正确的方向或者帮助我使用正确的语法吗?
谢谢!
编辑:我添加了使用过的代码,但现在无法再使用键盘箭头导航文本 - 有解决方法吗?
Private Sub rtbScriptRTF_KeyDown(sender As Object, e As KeyEventArgs) Handles rtbScriptRTF.KeyDown
Dim index As Integer
Dim lineNum As Integer
rtbScriptRTF.Focus()
rtbScriptRTF.SuspendLayout()
rtbScriptRTF.WordWrap = False
Index = rtbScriptRTF.SelectionStart
lineNum = rtbScriptRTF.GetLineFromCharIndex(index)
txtTestLINE.Text = lineNum + 1
rtbScriptRTF.WordWrap = True
rtbScriptRTF.ResumeLayout()
End Sub
WinForm RichTextBox 包装的本机 RichEdit 控件支持 Text Object Model.
的子集
The TOM defines a substantial set of text manipulation interfaces. Text solutions such as Microsoft Word and rich edit controls support the TOM feature set.
将项目 COM 引用添加到 "tom" 以获得对 .Net 项目中接口定义的访问权限。然后您获得了对控件的 TOM ITextDocument interface by sending then EM_GETOLEINTERFACE message 的引用。获得文档引用后,您可以像使用 MS Word 的对象模型一样浏览文档。
在下面的例子中(新建一个带有RichTextBox
和窗体标签的WinForm项目),SelectionChanged
事件用于在标签中显示选中段落的编号。请注意,对于此 COM,编号从索引一 (1) 开始,而不是从零 (0) 开始。创建 RichTextBox 句柄时获取存储在变量 "doc") 中的文档引用。
' add project COM reference to "tom"
Imports System.Runtime.InteropServices
Public Class Form1
Private doc As tom.ITextDocument
Private comCallCount As Int32 = 1 ' used to force GC to cleanup unreferenced COM object
Public Sub New()
InitializeComponent()
PopulateRTB()
End Sub
Private Sub PopulateRTB()
With RichTextBox1
.AppendText("Paragraph 1: Lorem ipsum dolor sit amet, solum lobortis pri id, vocent volumus praesent nam ut. Impetus aliquip quo at. Eos nisl veniam fierent an, cu dictas veritus vivendo mea, ei sea eruditi prodesset percipitur. Duo in case eirmod adipiscing, qui habemus scriptorem id. Mei nihil facilisis cu, id sumo laboramus consectetuer vel. No nostrud aliquid quo. Nam doming melius scripta eu." & Environment.NewLine)
.AppendText(Environment.NewLine)
.AppendText("Paragraph 3: An pri consul adversarium, ea mel semper quaeque complectitur. Mea te solet concludaturque, ei mucius albucius sit. Per no lucilius detraxit adipisci, ut tamquam delenit cotidieque cum. Mea putent apeirian sapientem cu. Per saperet accumsan rationibus at." & Environment.NewLine)
.AppendText(Environment.NewLine)
.AppendText("Paragraph 5: Velit tollit voluptua pri ut. Convenire honestatis no nam, nulla legere salutatus at ius. Ea duo nisl everti aliquid, dolore aperiam tractatos te duo, quaeque veritus ad vim. At vim consul laoreet, inermis iudicabit et vim." & Environment.NewLine)
.AppendText(Environment.NewLine)
.AppendText("Paragraph 7: Altera efficiendi ad qui, ne quo viris moderatius. Idque singulis suavitate ea vix, vis at ancillae detraxit contentiones, has constituam sadipscing et. Eos et iudico corrumpit, augue nominavi ad mel. Impetus equidem eu duo. Ea sale legimus dignissim vix." & Environment.NewLine)
.AppendText(Environment.NewLine)
.AppendText("Paragraph 9: Et quodsi oblique iudicabit pro, an nam augue consulatu, ad cum graece malorum mediocritatem. Vivendo ullamcorper vim et, maiestatis accommodare sit cu. Duo vocent deleniti et, nec ei congue melius tamquam, modo phaedrum no sea. Ius at velit augue soleat." & Environment.NewLine)
End With
End Sub
Private Sub RichTextBox1_HandleCreated(sender As Object, e As EventArgs) Handles RichTextBox1.HandleCreated
Const WM_USER As Int32 = &H400
Const EM_GETOLEINTERFACE As Int32 = WM_USER + 60
If doc IsNot Nothing Then
Marshal.FinalReleaseComObject(doc)
doc = Nothing
End If
SendMessage(New HandleRef(Me, RichTextBox1.Handle), EM_GETOLEINTERFACE, IntPtr.Zero, doc)
End Sub
Private Sub RichTextBox1_HandleDestroyed(sender As Object, e As EventArgs) Handles RichTextBox1.HandleDestroyed
If doc IsNot Nothing Then
Marshal.FinalReleaseComObject(doc)
doc = Nothing
End If
End Sub
Private Sub RichTextBox1_SelectionChanged(sender As Object, e As EventArgs) Handles RichTextBox1.SelectionChanged
Label1.Text = $"In paragraph number: {doc.Selection.GetIndex(tom.tomConstants.tomParagraph)}"
comCallCount += 1
If comCallCount = 10 Then
CleanupUnreferencedCOM()
comCallCount = 1
End If
End Sub
Private Sub CleanupUnreferencedCOM()
Do
GC.Collect()
GC.WaitForPendingFinalizers()
Loop While Marshal.AreComObjectsAvailableForCleanup
End Sub
<DllImport("user32.dll", CharSet:=CharSet.Auto)>
Public Shared Function SendMessage(ByVal hWnd As HandleRef, ByVal msg As Integer, ByVal wParam As IntPtr, ByRef lParam As tom.ITextDocument) As IntPtr
End Function
End Class
Bradley Smith 还提供了 Managed Wrapper for the Text Object Model (TOM) 库,可以更轻松地使用对象模型。如果您选择使用此库,请注意作者通过托管代码添加了功能,并且库中同时存在基于零和一的索引。因此,请务必阅读 Intellisense 文档或主要项目文档以避免意外。
我有一个包含大量段落的富文本框。 (仅供参考:所有段落将由 vbCrlf 分隔)
我需要知道光标位置的段落索引。
很容易找到行索引
Dim line, index As Integer
index = rtbScriptRTF.SelectionStart
line = rtbScriptRTF.GetLineFromCharIndex(index)
txtTestLINE.Text = line
这不是我想要的,因为一个段落很容易占据很多行。
疯狂的是......我在这些页面的某个地方看到了一段代码,但我找不到任何地方!
任何人都可以指出正确的方向或者帮助我使用正确的语法吗? 谢谢!
编辑:我添加了使用过的代码,但现在无法再使用键盘箭头导航文本 - 有解决方法吗?
Private Sub rtbScriptRTF_KeyDown(sender As Object, e As KeyEventArgs) Handles rtbScriptRTF.KeyDown
Dim index As Integer
Dim lineNum As Integer
rtbScriptRTF.Focus()
rtbScriptRTF.SuspendLayout()
rtbScriptRTF.WordWrap = False
Index = rtbScriptRTF.SelectionStart
lineNum = rtbScriptRTF.GetLineFromCharIndex(index)
txtTestLINE.Text = lineNum + 1
rtbScriptRTF.WordWrap = True
rtbScriptRTF.ResumeLayout()
End Sub
WinForm RichTextBox 包装的本机 RichEdit 控件支持 Text Object Model.
的子集The TOM defines a substantial set of text manipulation interfaces. Text solutions such as Microsoft Word and rich edit controls support the TOM feature set.
将项目 COM 引用添加到 "tom" 以获得对 .Net 项目中接口定义的访问权限。然后您获得了对控件的 TOM ITextDocument interface by sending then EM_GETOLEINTERFACE message 的引用。获得文档引用后,您可以像使用 MS Word 的对象模型一样浏览文档。
在下面的例子中(新建一个带有RichTextBox
和窗体标签的WinForm项目),SelectionChanged
事件用于在标签中显示选中段落的编号。请注意,对于此 COM,编号从索引一 (1) 开始,而不是从零 (0) 开始。创建 RichTextBox 句柄时获取存储在变量 "doc") 中的文档引用。
' add project COM reference to "tom"
Imports System.Runtime.InteropServices
Public Class Form1
Private doc As tom.ITextDocument
Private comCallCount As Int32 = 1 ' used to force GC to cleanup unreferenced COM object
Public Sub New()
InitializeComponent()
PopulateRTB()
End Sub
Private Sub PopulateRTB()
With RichTextBox1
.AppendText("Paragraph 1: Lorem ipsum dolor sit amet, solum lobortis pri id, vocent volumus praesent nam ut. Impetus aliquip quo at. Eos nisl veniam fierent an, cu dictas veritus vivendo mea, ei sea eruditi prodesset percipitur. Duo in case eirmod adipiscing, qui habemus scriptorem id. Mei nihil facilisis cu, id sumo laboramus consectetuer vel. No nostrud aliquid quo. Nam doming melius scripta eu." & Environment.NewLine)
.AppendText(Environment.NewLine)
.AppendText("Paragraph 3: An pri consul adversarium, ea mel semper quaeque complectitur. Mea te solet concludaturque, ei mucius albucius sit. Per no lucilius detraxit adipisci, ut tamquam delenit cotidieque cum. Mea putent apeirian sapientem cu. Per saperet accumsan rationibus at." & Environment.NewLine)
.AppendText(Environment.NewLine)
.AppendText("Paragraph 5: Velit tollit voluptua pri ut. Convenire honestatis no nam, nulla legere salutatus at ius. Ea duo nisl everti aliquid, dolore aperiam tractatos te duo, quaeque veritus ad vim. At vim consul laoreet, inermis iudicabit et vim." & Environment.NewLine)
.AppendText(Environment.NewLine)
.AppendText("Paragraph 7: Altera efficiendi ad qui, ne quo viris moderatius. Idque singulis suavitate ea vix, vis at ancillae detraxit contentiones, has constituam sadipscing et. Eos et iudico corrumpit, augue nominavi ad mel. Impetus equidem eu duo. Ea sale legimus dignissim vix." & Environment.NewLine)
.AppendText(Environment.NewLine)
.AppendText("Paragraph 9: Et quodsi oblique iudicabit pro, an nam augue consulatu, ad cum graece malorum mediocritatem. Vivendo ullamcorper vim et, maiestatis accommodare sit cu. Duo vocent deleniti et, nec ei congue melius tamquam, modo phaedrum no sea. Ius at velit augue soleat." & Environment.NewLine)
End With
End Sub
Private Sub RichTextBox1_HandleCreated(sender As Object, e As EventArgs) Handles RichTextBox1.HandleCreated
Const WM_USER As Int32 = &H400
Const EM_GETOLEINTERFACE As Int32 = WM_USER + 60
If doc IsNot Nothing Then
Marshal.FinalReleaseComObject(doc)
doc = Nothing
End If
SendMessage(New HandleRef(Me, RichTextBox1.Handle), EM_GETOLEINTERFACE, IntPtr.Zero, doc)
End Sub
Private Sub RichTextBox1_HandleDestroyed(sender As Object, e As EventArgs) Handles RichTextBox1.HandleDestroyed
If doc IsNot Nothing Then
Marshal.FinalReleaseComObject(doc)
doc = Nothing
End If
End Sub
Private Sub RichTextBox1_SelectionChanged(sender As Object, e As EventArgs) Handles RichTextBox1.SelectionChanged
Label1.Text = $"In paragraph number: {doc.Selection.GetIndex(tom.tomConstants.tomParagraph)}"
comCallCount += 1
If comCallCount = 10 Then
CleanupUnreferencedCOM()
comCallCount = 1
End If
End Sub
Private Sub CleanupUnreferencedCOM()
Do
GC.Collect()
GC.WaitForPendingFinalizers()
Loop While Marshal.AreComObjectsAvailableForCleanup
End Sub
<DllImport("user32.dll", CharSet:=CharSet.Auto)>
Public Shared Function SendMessage(ByVal hWnd As HandleRef, ByVal msg As Integer, ByVal wParam As IntPtr, ByRef lParam As tom.ITextDocument) As IntPtr
End Function
End Class
Bradley Smith 还提供了 Managed Wrapper for the Text Object Model (TOM) 库,可以更轻松地使用对象模型。如果您选择使用此库,请注意作者通过托管代码添加了功能,并且库中同时存在基于零和一的索引。因此,请务必阅读 Intellisense 文档或主要项目文档以避免意外。