I have an app that lists text files in a combo box. A user can select a file then add strings to a list to search the selected file for these strings. Any line that is found with the search criteria is copied to the richtextbox. I am looking to be able to click the text and open the selected file in notepad at the location that was clicked in the RTB.
-
The answer to this similar question "[How to launch a process which will open a text file in any editor and automatically move cursor to a certain line number?](http://stackoverflow.com/questions/13755233/how-to-launch-a-process-which-will-open-a-text-file-in-any-editor-and-automatica)" explains how to do the same with Notepad++. – Adam Porad Jan 31 '14 at 20:31
-
The only issue I am seeing with that answer is how to make the text selectable in the richtextbox and use that as the point to move the cursor too. – Jim Davis Jan 31 '14 at 20:36
2 Answers
Something like this:
void StartInNotepad(string fileName)
{
Process notepad = new Process();
notepad.StartInfo.FileName = "notepad.exe";
notepad.StartInfo.Arguments = fileName;
notepad.Start();
}

- 12,657
- 1
- 31
- 56
You need to know that you cannot open a file in Notepad and go directly to a specific position. But you can do it in Notepad++ !
First install Notepad++. Then do the following:
to be able to click the text and open the selected file in notepad at the location that was clicked in the RTB, you need to subscribe to MouseClick event of the RTB.
private void richTextBoxDialog_MouseClick(object sender, MouseEventArgs e) { string filePath = comboBoxFiles.SelectedItem.ToString(); if (e.Button == MouseButtons.Left) { int clickIndex = richTextBoxDialog.GetCharIndexFromPosition(e.Location); int index = GetIndexInFile(filePath, richTextBoxDialog.Text, clickIndex); Point p = GetPositionFromFileIndex(File.ReadAllText(filePath), index); OpenNppXY(filePath, p.X, p.Y); } }
The following method will return the index of currently clicked position w.r.t. the selected file. You may need to decide which to consider: '\r' and '\n' as a single newline or two newlines to adjust the correct position.
int GetIndexInFile(string Filepath, string searchStr, int curIndexRTB) { string content = File.ReadAllText(Filepath, Encoding.Default); return content.IndexOf(searchStr) + curIndexRTB; }
This method will return the line number and column position of the clicked position. You may need to tweak a little bit in order to get the correct location.
Point GetPositionFromFileIndex(string input, int index) { Point p = new Point(); p.X = input.Take(index).Count(c => c == '\n') + 1; p.Y = input.Take(index).ToString().Substring(input.Take(index).ToString().LastIndexOf('\n') + 1).Length + 1; return p; }
finally, this will open the selected file in NPP by placing the caret to the desired location.
void OpenNppXY(string fileFullPath, int line, int column) { var nppDir = (string)Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Notepad++", null, null); var nppExePath = Path.Combine(nppDir, "Notepad++.exe"); var sb = new StringBuilder(); sb.AppendFormat("\"{0}\" -n{1} -c{2}", fileFullPath, line, column); Process.Start(nppExePath, sb.ToString()); }

- 3,900
- 1
- 18
- 20