Reputation: 7840
I have multi-lines textbox in c# window-base application. If user click on textbox I want to get the value of selected line. Kindly guide me so that I can make it possible.
Sorry for my bad english.
Upvotes: 2
Views: 2243
Reputation: 19
Use the KeyUp event, like:
Private Sub TextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp
MyString = TextBox1.SelectedText
End Sub
Upvotes: 0
Reputation: 8278
I'm hoping that you mean a listbox?
listBox.SelectedItem gives a ListItem
and then you can get the Text or Value from there
Upvotes: 2
Reputation: 1058
Very briefly:
int sel = this.textBox1.SelectionStart;
string text = this.textBox1.Text;
int selstart = 0;
int selend = 0;
int i = 0;
for (i = sel; i > 0; i--)
if (text[i] == '\n')
break;
selstart = i;
for (i = sel; i < text.Length; i++)
if (text[i] == '\n')
break;
selend = i;
string line = text.Substring(selstart, selend - selstart);
'line' is the line you pointed.
EDITED: Fara posted better solution.
Upvotes: 1
Reputation: 7011
You can try something along the lines of:
// Get selected character.
int charIndex = textbox.SelectionStart;
// Get line index from selected character.
int lineIndex = textbox.GetLineFromCharIndex(charIndex);
// Get line.
string line = textbox.Lines[lineIndex];
Upvotes: 4
Reputation: 6547
If its a multi-line enabled textbox you can use the Lines
property.
Upvotes: 1