[英]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.