.net RichTextbox-行长度选择问题
我正在编写一个小应用程序,可以加载一个文本文件来匹配正则表达式: 我根据匹配行的长度更改前景色文本 问题是,如果一行是多行,那么它就不能按预期工作,只选择行的一半,例如以“title”开头的第二行,这是: 您可以在这里看到问题: 但奇怪的是,如果我调整表单的大小,让我看到完整的行,然后我重新键入正则表达式,那么它将按预期工作并选择整行: 我不知道如何在不调整表单大小的情况下解决此问题 这是我用来更改前景色的代码:.net RichTextbox-行长度选择问题,.net,regex,vb.net,richtextbox,rich-text-editor,.net,Regex,Vb.net,Richtextbox,Rich Text Editor,我正在编写一个小应用程序,可以加载一个文本文件来匹配正则表达式: 我根据匹配行的长度更改前景色文本 问题是,如果一行是多行,那么它就不能按预期工作,只选择行的一半,例如以“title”开头的第二行,这是: 您可以在这里看到问题: 但奇怪的是,如果我调整表单的大小,让我看到完整的行,然后我重新键入正则表达式,那么它将按预期工作并选择整行: 我不知道如何在不调整表单大小的情况下解决此问题 这是我用来更改前景色的代码: Private Sub MatchRegEx() Label_Mat
Private Sub MatchRegEx()
Label_Matched_Value.Text = "0"
Label_Missed_Value.Text = "0"
If (TextBox_RegEx.Text = "" Or TextBox_RegEx.Text = TextBox_Hint) And RichTextBox_Strings.Text.Length = 0 Then
Label_Info.Text = ""
RichTextBox_Strings.Select(0, RichTextBox_Strings.Text.Length)
RichTextBox_Strings.SelectionColor = Color.FromArgb(248, 248, 242)
ElseIf (TextBox_RegEx.Text = "" Or TextBox_RegEx.Text = TextBox_Hint) And RichTextBox_Strings.Text.Length > 0 Then
Label_Info.Text = ""
RichTextBox_Strings.SelectionColor = Color.FromArgb(248, 248, 242)
Else
Try
Label_Info.Text = "Valid RegEx"
For i As Integer = 0 To RichTextBox_Strings.Lines.Length - 1
If System.Text.RegularExpressions.Regex.IsMatch(RichTextBox_Strings.Lines(i), RegEx) Then
If Not RichTextBox_Strings.Focused Then
RichTextBox_Strings.Select(RichTextBox_Strings.GetFirstCharIndexFromLine(i), RichTextBox_Strings.Lines(i).Length)
End If
RichTextBox_Strings.SelectionColor = Color.LimeGreen
Label_Matched_Value.Text = CInt(Label_Matched_Value.Text) + 1
Else
If Not RichTextBox_Strings.Focused Then
RichTextBox_Strings.Select(RichTextBox_Strings.GetFirstCharIndexFromLine(i), RichTextBox_Strings.Lines(i).Length)
End If
RichTextBox_Strings.SelectionColor = Color.FromArgb(248, 248, 242)
Label_Missed_Value.Text = CInt(Label_Missed_Value.Text) + 1
End If
Next
Catch ex As Exception
Label_Info.Text = "Invalid RegEx"
RichTextBox_Strings.SelectAll()
RichTextBox_Strings.SelectionColor = Color.FromArgb(248, 248, 242)
End Try
End If
End Sub
以及示例文本:
Title : The Upbeats - Primitive Technique
Genre : Drum & Bass
Year : 2013
Page : http://www.mp3crank.com/the-upbeats/primitive-technique.htm
Download: http://potload.com/zj0scbxjuw90
Title : Kirsty Maccoll - A New England: The Very Best of Kirsty Maccoll
Genre : Folk, Pop
Year : 2013
Page : http://www.mp3crank.com/kirsty-maccoll/a-new-england-the-very-best-of-kirsty-maccoll.htm
Download: http://potload.com/ziixpepo07lu
Title : Of Montreal - Young Froth / Taypiss
Genre : Indie, Pop
Year : 2013
Page : http://www.mp3crank.com/of-montreal/young-froth-taypiss.htm
Download: http://potload.com/hyc4okxucnlu
更新:
我已调试该问题,且值正确:
Line 0 - selected range: 43 length
Line 8 - selected range: 73 length
Line 16 - selected range: 45 length
是“标题”行的确切行数和长度(当窗体调整大小以查看整行时)
更新2:
如果我将richtextbox的textfont更改为5pt这样的小字体(以默认表单大小显示整行),那么所有操作也都会按预期进行
所以这个问题似乎是这样的:当整行显示为多行时,因为整行不适合表单的大小,那么它的计数就像多行一样?
如何解决这个问题
更新3:
这是完整的来源,如果你想测试它
Public Class Form1
#Region " Vars / Properties "
Dim TextBox_Hint As String = "Type your RegEx here..."
Dim MatchRegEx_Flag As Boolean = True
Public Property RegEx() As String
Get
Return TextBox_RegEx.Text
End Get
Set(ByVal value As String)
TextBox_RegEx.Text = value
End Set
End Property
#End Region
#Region " Controls "
' TextBox RegEx [Enter/Leave]
Private Sub TextBox_RegEx_Hint(sender As Object, e As EventArgs) Handles TextBox_RegEx.Enter, TextBox_RegEx.Leave
If sender.Text = TextBox_Hint Then
Label_Info.Text = ""
sender.text = ""
ElseIf sender.Text = "" Then
sender.text = TextBox_Hint
Label_Info.Text = ""
End If
End Sub
' TextBox RegEx [TextChanged]
Private Sub TextBox_RegEx_TextChanged(sender As Object, e As EventArgs) Handles TextBox_RegEx.TextChanged
If MatchRegEx_Flag Then
MatchRegEx_Flag = False
MatchRegEx()
MatchRegEx_Flag = True
End If
End Sub
' Button Copy RegEx [Click]
Private Sub Button_Copy_RegEx_Click(sender As Object, e As EventArgs) Handles Button_Copy_RegEx.Click
Clipboard.SetText(TextBox_RegEx.Text)
End Sub
' Button Copy Matches [Click]
Private Sub Button_Copy_Matches_Click(sender As Object, e As EventArgs) Handles Button_Copy_Matches.Click
Clipboard.SetText(" ")
For i As Integer = 0 To RichTextBox_Strings.Lines.Length - 1
If System.Text.RegularExpressions.Regex.IsMatch(RichTextBox_Strings.Lines(i), RegEx) Then
RichTextBox_Strings.Select(RichTextBox_Strings.GetFirstCharIndexFromLine(i), RichTextBox_Strings.Lines(i).Length)
Clipboard.SetText(Clipboard.GetText & vbNewLine & RichTextBox_Strings.SelectedText)
End If
Next
End Sub
' Button Load [ Click]
Private Sub Button_TextFile_Click(sender As Object, e As EventArgs) Handles Button_TextFile.Click
Dim Textfile As New OpenFileDialog()
Textfile.InitialDirectory = Environ("programfiles")
Textfile.Title = "Load a text from file..."
Textfile.Filter = "Text-files|*.txt"
If Textfile.ShowDialog() = DialogResult.OK Then
RichTextBox_Strings.SuspendLayout()
RichTextBox_Strings.Text = My.Computer.FileSystem.ReadAllText(Textfile.FileName)
RichTextBox_Strings.ResumeLayout()
End If
End Sub
' RichTextBox [MouseHover]
Private Sub RichTextBox_Strings_MouseHover(sender As Object, e As EventArgs) Handles RichTextBox_Strings.MouseHover
'sender.focus()
End Sub
' RichTextBox [TextChanged]
Private Sub RichTextBox_Strings_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox_Strings.TextChanged
If MatchRegEx_Flag Then
MatchRegEx_Flag = False
MatchRegEx()
MatchRegEx_Flag = True
End If
End Sub
#End Region
#Region " Procedures "
Private Sub MatchRegEx()
Label_Matched_Value.Text = "0"
Label_Missed_Value.Text = "0"
If (TextBox_RegEx.Text = "" Or TextBox_RegEx.Text = TextBox_Hint) And RichTextBox_Strings.Text.Length = 0 Then
Label_Info.Text = ""
RichTextBox_Strings.Select(0, RichTextBox_Strings.Text.Length)
RichTextBox_Strings.SelectionColor = Color.FromArgb(248, 248, 242)
ElseIf (TextBox_RegEx.Text = "" Or TextBox_RegEx.Text = TextBox_Hint) And RichTextBox_Strings.Text.Length > 0 Then
Label_Info.Text = ""
RichTextBox_Strings.SelectionColor = Color.FromArgb(248, 248, 242)
Else
Try
Label_Info.Text = "Valid RegEx"
For i As Integer = 0 To RichTextBox_Strings.Lines.Length - 1
If System.Text.RegularExpressions.Regex.IsMatch(RichTextBox_Strings.Lines(i), RegEx) Then
If Not RichTextBox_Strings.Focused Then
RichTextBox_Strings.Select(RichTextBox_Strings.GetFirstCharIndexFromLine(i), RichTextBox_Strings.Lines(i).Length)
End If
RichTextBox_Strings.SelectionColor = Color.LimeGreen
Label_Matched_Value.Text = CInt(Label_Matched_Value.Text) + 1
Else
If Not RichTextBox_Strings.Focused Then
RichTextBox_Strings.Select(RichTextBox_Strings.GetFirstCharIndexFromLine(i), RichTextBox_Strings.Lines(i).Length)
End If
RichTextBox_Strings.SelectionColor = Color.FromArgb(248, 248, 242)
'MsgBox(RichTextBox_Strings.Lines(i))
Label_Missed_Value.Text = CInt(Label_Missed_Value.Text) + 1
End If
Next
Catch ex As Exception
' MsgBox(ex.Message)
Label_Info.Text = "Invalid RegEx"
RichTextBox_Strings.SelectAll()
RichTextBox_Strings.SelectionColor = Color.FromArgb(248, 248, 242)
End Try
End If
End Sub
#End Region
End Class
我以前也面临这个问题吗?禁用富文本框的
wordwrap
属性可能会解决您的问题:)请参阅和
或者,完全关闭wordwrap,并允许框水平滚动。您调试过它吗?当您在匹配正则表达式的行上设置断点时,当循环位于错误行上时,
RichTextBox\u Strings.Lines(i)
的值是多少?我现在已经调试了它,您可以看到我的更新,值正确:第0行-选定:43长度第8行-选定:73长度第16行-选定:45长度我以前也遇到过这个问题吗?禁用富文本框的wordwrap属性可能会解决您的问题:)@Civa太简单了,我太笨了!如果你想成为代表,请以asnwer的身份发布你的评论!谢谢你的帮助。
Public Class Form1
#Region " Vars / Properties "
Dim TextBox_Hint As String = "Type your RegEx here..."
Dim MatchRegEx_Flag As Boolean = True
Public Property RegEx() As String
Get
Return TextBox_RegEx.Text
End Get
Set(ByVal value As String)
TextBox_RegEx.Text = value
End Set
End Property
#End Region
#Region " Controls "
' TextBox RegEx [Enter/Leave]
Private Sub TextBox_RegEx_Hint(sender As Object, e As EventArgs) Handles TextBox_RegEx.Enter, TextBox_RegEx.Leave
If sender.Text = TextBox_Hint Then
Label_Info.Text = ""
sender.text = ""
ElseIf sender.Text = "" Then
sender.text = TextBox_Hint
Label_Info.Text = ""
End If
End Sub
' TextBox RegEx [TextChanged]
Private Sub TextBox_RegEx_TextChanged(sender As Object, e As EventArgs) Handles TextBox_RegEx.TextChanged
If MatchRegEx_Flag Then
MatchRegEx_Flag = False
MatchRegEx()
MatchRegEx_Flag = True
End If
End Sub
' Button Copy RegEx [Click]
Private Sub Button_Copy_RegEx_Click(sender As Object, e As EventArgs) Handles Button_Copy_RegEx.Click
Clipboard.SetText(TextBox_RegEx.Text)
End Sub
' Button Copy Matches [Click]
Private Sub Button_Copy_Matches_Click(sender As Object, e As EventArgs) Handles Button_Copy_Matches.Click
Clipboard.SetText(" ")
For i As Integer = 0 To RichTextBox_Strings.Lines.Length - 1
If System.Text.RegularExpressions.Regex.IsMatch(RichTextBox_Strings.Lines(i), RegEx) Then
RichTextBox_Strings.Select(RichTextBox_Strings.GetFirstCharIndexFromLine(i), RichTextBox_Strings.Lines(i).Length)
Clipboard.SetText(Clipboard.GetText & vbNewLine & RichTextBox_Strings.SelectedText)
End If
Next
End Sub
' Button Load [ Click]
Private Sub Button_TextFile_Click(sender As Object, e As EventArgs) Handles Button_TextFile.Click
Dim Textfile As New OpenFileDialog()
Textfile.InitialDirectory = Environ("programfiles")
Textfile.Title = "Load a text from file..."
Textfile.Filter = "Text-files|*.txt"
If Textfile.ShowDialog() = DialogResult.OK Then
RichTextBox_Strings.SuspendLayout()
RichTextBox_Strings.Text = My.Computer.FileSystem.ReadAllText(Textfile.FileName)
RichTextBox_Strings.ResumeLayout()
End If
End Sub
' RichTextBox [MouseHover]
Private Sub RichTextBox_Strings_MouseHover(sender As Object, e As EventArgs) Handles RichTextBox_Strings.MouseHover
'sender.focus()
End Sub
' RichTextBox [TextChanged]
Private Sub RichTextBox_Strings_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox_Strings.TextChanged
If MatchRegEx_Flag Then
MatchRegEx_Flag = False
MatchRegEx()
MatchRegEx_Flag = True
End If
End Sub
#End Region
#Region " Procedures "
Private Sub MatchRegEx()
Label_Matched_Value.Text = "0"
Label_Missed_Value.Text = "0"
If (TextBox_RegEx.Text = "" Or TextBox_RegEx.Text = TextBox_Hint) And RichTextBox_Strings.Text.Length = 0 Then
Label_Info.Text = ""
RichTextBox_Strings.Select(0, RichTextBox_Strings.Text.Length)
RichTextBox_Strings.SelectionColor = Color.FromArgb(248, 248, 242)
ElseIf (TextBox_RegEx.Text = "" Or TextBox_RegEx.Text = TextBox_Hint) And RichTextBox_Strings.Text.Length > 0 Then
Label_Info.Text = ""
RichTextBox_Strings.SelectionColor = Color.FromArgb(248, 248, 242)
Else
Try
Label_Info.Text = "Valid RegEx"
For i As Integer = 0 To RichTextBox_Strings.Lines.Length - 1
If System.Text.RegularExpressions.Regex.IsMatch(RichTextBox_Strings.Lines(i), RegEx) Then
If Not RichTextBox_Strings.Focused Then
RichTextBox_Strings.Select(RichTextBox_Strings.GetFirstCharIndexFromLine(i), RichTextBox_Strings.Lines(i).Length)
End If
RichTextBox_Strings.SelectionColor = Color.LimeGreen
Label_Matched_Value.Text = CInt(Label_Matched_Value.Text) + 1
Else
If Not RichTextBox_Strings.Focused Then
RichTextBox_Strings.Select(RichTextBox_Strings.GetFirstCharIndexFromLine(i), RichTextBox_Strings.Lines(i).Length)
End If
RichTextBox_Strings.SelectionColor = Color.FromArgb(248, 248, 242)
'MsgBox(RichTextBox_Strings.Lines(i))
Label_Missed_Value.Text = CInt(Label_Missed_Value.Text) + 1
End If
Next
Catch ex As Exception
' MsgBox(ex.Message)
Label_Info.Text = "Invalid RegEx"
RichTextBox_Strings.SelectAll()
RichTextBox_Strings.SelectionColor = Color.FromArgb(248, 248, 242)
End Try
End If
End Sub
#End Region
End Class