Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello All,
I am using a richtextbox and i need to get the caret position as number in order to use it , any idea to get the index number ??
thanks for your help

What I have tried:

i don;t find anythimg and i don't find any function in richtextbox function that return to my the index number
Posted
Updated 28-Jan-17 1:31am

Google is your friend: Be nice and visit him often. He can answer questions a lot more quickly than posting them here...

A very quick search using your subject as the search term gave 30,000 hits: Get richtextbox caret position index number WPF - Google Search[^]
Leading almost immediately to MSDN: RichTextBox, getting caret index.[^]

In future, please try to do at least basic research yourself, and not waste your time or ours.
 
Share this answer
 
Comments
Member 12945224 28-Jan-17 6:31am    
thank you for your help and i will not waste your time again. and i will do more researches for my problems. thx again
OriginalGriff 28-Jan-17 6:51am    
You're welcome!
You can use RichTextBox.CaretPosition[^] property to access the current index of caret inside the content.

Following code was captured from MSDN,
// Create a new FlowDocument, and add 3 paragraphs.
FlowDocument flowDoc = new FlowDocument();
flowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 1"))); 
flowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 2"))); 
flowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 3")));
// Set the FlowDocument to be the content for a new RichTextBox.
RichTextBox rtb = new RichTextBox(flowDoc);

// Get the current caret position.
TextPointer caretPos = rtb.CaretPosition;

// Set the TextPointer to the end of the current document.
caretPos = caretPos.DocumentEnd;

// Specify the new caret position at the end of the current document.
rtb.CaretPosition = caretPos;

You can see that these are a few of the methods how you can manipulate the caret position inside the document, you can get the position, as well as set the position.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900