I have a textbox, and I need to get the contents of the line where the cursor is positioned. For example, I'm in the third line of the textbox (cursor positioned on it), I'd like to get the contents of that line. Thanks in advance ...
I have a textbox, and I need to get the contents of the line where the cursor is positioned. For example, I'm in the third line of the textbox (cursor positioned on it), I'd like to get the contents of that line. Thanks in advance ...
I imagine you have a TextBox
with the MultLine
property enabled for true
.
So, to get the content of the line where the vertical bar is positioned | just use this method that created PegaConteudoDaLinhaAtual()
, see:
string PegaConteudoDaLinhaAtual(TextBox textBox)
{
var textoDaLinha = textBox.Lines[textBox.GetLineFromCharIndex(textBox.SelectionStart)].ToString();
return textoDaLinha;
}
Here is the implementation of it at the click of the button:
private void btnPega_Click(object sender, EventArgs e)
{
MessageBox.Show(PegaConteudoDaLinhaAtual(textBox1));
}