in Education by
I am trying to set the position of caret in richtextbox based on index position of a word. Even though I am able to change the caret position, the caret does not move to the correct location. Here is my sample code: private void Button_Click(object sender, RoutedEventArgs e) { RTB_Main.Document.Blocks.Clear(); for (int i = 0; i < 10; i++) { Paragraph para = new Paragraph(new Run(i + "")); RTB_Main.Document.Blocks.Add(para); } TextRange richText = new TextRange(RTB_Main.Document.ContentStart, RTB_Main.Document.ContentEnd); string searchText = tb_Search.Text; // 1 to 9 int position = Regex.Match(richText.Text, searchText).Index; RTB_Main.CaretPosition = RTB_Main.Document.ContentStart; RTB_Main.CaretPosition = RTB_Main.CaretPosition.GetPositionAtOffset(position); RTB_Main.Focus(); } What is wrong with this approach? Also, Please let me know if there is a better way to set the caret position to an index? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
The problem in my case was caused by new line characters \r\n. I just replaced these with another characters and it worked for me. Note that I am replacing them with not 2 characters but 4. private void Button_Click(object sender, RoutedEventArgs e) { RTB_Main.Document.Blocks.Clear(); for (int i = 0; i < 10; i++) { Paragraph para = new Paragraph(new Run(i + "")); RTB_Main.Document.Blocks.Add(para); } TextRange richText = new TextRange(RTB_Main.Document.ContentStart, RTB_Main.Document.ContentEnd); string searchText = tb_Search.Text; // 1 to 9 string tmpStr = richText.Text.Replace("\r\n", "...."); int position = Regex.Match(tmpStr, searchText).Index; RTB_Main.CaretPosition = RTB_Main.Document.ContentStart; RTB_Main.CaretPosition = RTB_Main.CaretPosition.GetPositionAtOffset(position); RTB_Main.Focus(); }

Related questions

0 votes
    I want my WPF application to be skinnable, by applying a certain XAML template, and the changes to be ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    So I created a WPF application on my monitor which has bigger height (1280 vs 1024) resolution. It ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 26, 2022 in Education by JackTerrance
0 votes
    I am creating a WPF Application that uses Fluent library. On my computer with Windows 7, 2GB RAM and ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    I'm trying to connect to a website that uses cookies. My login is as follows: private void button1_Click ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
    Is there any simple way to repeat code in a try/catch block after an exception occurs without using method ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 1, 2022 in Education by JackTerrance
0 votes
    Someone please correct me if I'm wrong, but parsing a yyyy/MM/dd (or other specific formats) ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 26, 2022 in Education by JackTerrance
0 votes
    I have an interface that I have defined in C++ which now needs to be implemented in C#. What is ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 26, 2022 in Education by JackTerrance
0 votes
    Someone please correct me if I'm wrong, but parsing a yyyy/MM/dd (or other specific formats) ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 26, 2022 in Education by JackTerrance
0 votes
    I have an interface that I have defined in C++ which now needs to be implemented in C#. What is ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 25, 2022 in Education by JackTerrance
0 votes
    I would like to know how to unload an assembly that is loaded into the main AppDomain. I have the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    I would like to know how to unload an assembly that is loaded into the main AppDomain. I have the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    I'm having an annoying problem registering a javascript event from inside a user control within a formview ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 13, 2022 in Education by JackTerrance
0 votes
    I'm working on an extension (BHO) for Internet Explorer, and having trouble attaching a .NET delegate to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    Working on a project using sqlServer 2008. EDIT for clarification : table 1 contains 1 field that has to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 4, 2022 in Education by JackTerrance
0 votes
    I want to remove the inbuilt grey small caret from ion-select, and use my custom caret(arrow) instead ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 22, 2022 in Education by JackTerrance
...