2
如何获取已选中文本的行? 例如: 对于选定行中的每一行(vb.net)
所选线将是1,2,3和4(0是第一行)
我怎样才能获得为如下代码:
For Each line as string(or integer) in textbox1."SelectedLines"
'Do something here for each line
Next
由于
如何获取已选中文本的行? 例如: 对于选定行中的每一行(vb.net)
所选线将是1,2,3和4(0是第一行)
我怎样才能获得为如下代码:
For Each line as string(or integer) in textbox1."SelectedLines"
'Do something here for each line
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
我想你正在寻找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
仅供参考 - 如果这是问题的要求 - 这些相同的方法也可用于常规的TextBox控件。 – 2010-01-28 22:40:43
谢谢,我问道后,我意识到我需要的行号,但谢谢你的其他答案s_hewitt – 2010-01-29 18:59:13