0

I'm trying to do something like add the CTRL-F mechanism to my C# application in visual studio.

My app uses the Process.start method to open Windows Notepad and find a given word. I want to find this word in Notepad and focus it.

I have a way to search this word but how to focus to the correct line as would happen if the user clicked CTRL-F?

Does anybody know how to do this? Or maybe there is there a framework which can help?

My application is about searching any file by typing any sequence of string in textbox and getting file, which contains it, after that i have a open function of this file and i want to get focused word, which im typed in opened notepad (cuz i got txt file containing this string)

For example i typed "hello" in textbox and get file.txt and inside this after opened i have focused word "hello"

  • Does this answer your question? [Bring another processes Window to foreground when it has ShowInTaskbar = false](https://stackoverflow.com/questions/2636721/bring-another-processes-window-to-foreground-when-it-has-showintaskbar-false) – jjxtra Jan 10 '21 at 15:49
  • It is not clear how your application interacts with Notepad. Is there a Notepad API that allows you to script that application? It sounds like maybe you are trying the script the Windows 10 UI to open and drive Notepad, is this true? Or are you trying to build something that behaves like Notepad. You need to be clearer. Good luck and welcome! – rfreytag Jan 10 '21 at 17:44
  • My application is about searching any file by typing any sequence of string in textbox and getting file, which contains it, after that i have a open function of this file and i want to get focused word, which im typed in opened notepad (cuz i got txt file containing this string) – SilverRaven655 Jan 10 '21 at 18:24
  • For example i typed "hello" in textbox and get file.txt and inside this after opened i have focused word "hello" – SilverRaven655 Jan 10 '21 at 18:27

2 Answers2

0

Code to do this:

private async void button2_Click(object sender, EventArgs e)
{
    Process.Start("yourProcess");
    this.WindowState = FormWindowState.Minimized;
    await Task.Delay(1000);
    SendKeys.Send("^(f)");
    foreach (char c in textBox1.Text)
    {
        SendKeys.Send("{" + c + "}");
    }
    SendKeys.Send("{ENTER}");
    //if you want to automatycally remove search window use:
    SendKeys.Send("{ESC}");
}
Kuba_Z2
  • 54
  • 1
  • 6
0

Here is my code for now (but not working) inside: "private async void OpeningFile_Click(object sender, RoutedEventArgs e)"

        try
        {
            var startInfo = new ProcessStartInfo();

            startInfo.FileName = PathResult.ToString();
            startInfo.Arguments = "\"" + PathResult.ToString() + "\"";
            startInfo.UseShellExecute = true;

            Process.Start(startInfo);
       
            MessageBox.Show(ControlHelper.WordToFind);
           
            await Task.Delay(1000);

           
            System.Windows.Forms.SendKeys.SendWait("^(f)");

            foreach(char x in ControlHelper.WordToFind)
            {
                System.Windows.Forms.SendKeys.SendWait("{" + x + "}");
            }

            System.Windows.Forms.SendKeys.SendWait("{ENTER}");
            
            System.Windows.Forms.SendKeys.SendWait("{ESC}");          
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message + "\n" + PathResult.ToString());
        }