[英]For each line in selected lines (vb.net)
如何獲得其中已選擇文本的行? 例如:
選定的行將是1、2、3和4(0是第一行)
我怎樣才能像這樣的代碼:
For Each line as string(or integer) in textbox1."SelectedLines"
'Do something here for each line
Next
謝謝
我認為您正在尋找SelectedText屬性。 (在C#中)
foreach(string line in textBox1.SelectedText.Split(new string[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries))
{
//dostuffhere
}
(在我嘗試VB時)
Dim splitter(1) As String
splitter(0) = Environment.NewLine
For Each y As String In TextBox1.SelectedText.Split(splitter, StringSplitOptions.RemoveEmptyEntries)
//do stuff here
Next
從字面上看,即使僅選擇了行1和行4的一部分,也需要找到行號。 如下進行:
If RichTextBox1.SelectionLength > 0 Then
Dim firstLine As Integer = RichTextBox1.GetLineFromCharIndex(RichTextBox1.SelectionStart)
Dim lastLine As Integer = RichTextBox1.GetLineFromCharIndex(RichTextBox1.SelectionStart + RichTextBox1.SelectionLength)
For line As Integer = firstLine To lastLine
Dim txt = RichTextBox1.Lines(line)
' do something...
Next
End If
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.