How to get the text of the line where the cursor is positioned?

0

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 ...

    
asked by anonymous 18.07.2016 / 02:05

1 answer

3

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));
}
    
18.07.2016 / 02:58