テキストを選択した行を取得するにはどうすればよいですか? 例えば:
選択された行は 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