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

Asked

Viewed 150 times

0

I have a textbox, and I need to get the content of the line where the cursor is positioned. For example, I am in the third line of the textbox (cursor positioned on it), I would like to get the contents of this line. I thank you already...

  • Is Windows Forms?

  • It is Windows Forms yes

  • @Localhost ñ understood very well what q vc want, can not use the option Copiar? You could also show us what your code looks like and how you’re trying to do it?

  • For example, I am in the third line of the textbox, moving with the arrow up or down, and if for example I am in that third line (cursor positioned in it), I would like to get the contents of that line, you know?

  • The main issue that I’m failing to address is getting the content of the line on which I’m focusing

  • @Localhost yes I think I understand, you want to get all the content from the line where the vertical bar is positioned | or just a word?

  • That, all the content.

Show 2 more comments

1 answer

3


I imagine you have one TextBox with the property MultLine qualified to true.

Thus, to obtain the contents of the line where the vertical bar is positioned | just use this method I 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));
}
  • 1

    Thank you, Valeuzão. It helped a lot :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.