Jay Bazuzi
Jay Bazuzi

Reputation: 46526

How do I get the line of text under the cursor in a TextView in gtk#?

I have a GTK# TextView and I want to read the line of text under the cursor. I don't see a single method that will do that, so I think I need to combine several method calls, like Buffer.GetText, Buffer.GetIterAtOffset, Buffer.CursorPosition, but it's not obvious to me what the right combination is.

Upvotes: 2

Views: 1005

Answers (1)

Kristof
Kristof

Reputation: 445

TextIter are a bit odd to use. Buffer.CursorPosition gives you the current position.

It's easy to find the end of the line:

var end = Buffer.CursorPosition;
end.ForwardToLineEnd();

To get the first character, there's not symetrical method, so you might try:

var start = Buffer.CursorPosition;
start.BackwardChars(start.LineOffset); // LineOffset gives you the iter offset on the current line. 

Upvotes: 1

Related Questions