我編碼一個小應用程序在那裏我可以加載一個文本文件,以配合正則表達式:的RichTextbox - 線長度選擇問題
我改變基於在匹配線的長度前景色文本,
的問題是,如果一條線是一個多則如預期它不工作,選擇行只有一個半部分,例如像第二行開始「稱號」,這樣的:
Title : Kirsty Maccoll - A New England: The Very Best of Kirsty Maccoll
你可以看到這裏的問題:
但奇怪的是,如果我調整的形式讓我看到了全行,然後我重新鍵入正則表達式,那麼它將按預期工作,並選擇整條生產線:
我不知道如何解決這個問題,而無需調整窗體大小。
這是我用來改變前景色代碼:
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
UPDATE:
我已經調試問題和值是正確的:
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
你調試嗎?當您設置的行斷點,你匹配的正則表達式,當環路上的故障線路,什麼是'RichTextBox_Strings.Lines(我)'的價值? – stema 2013-05-02 05:59:23
現在我已經調試它,你可以看到我更新的值是否正確:0行 - 選擇:43長8 行 - 選擇:73長 線16 - 選擇:45長 – ElektroStudios 2013-05-02 06:11:22
我也面臨這樣的問題以前不?禁用自動換行屬性,以豐富的文本框可以解決您的問題:) – Civa 2013-05-02 06:13:41