1

I have used StackOverFLow a few times to steal snippets. This time I have a question since my thievery skills seem to have diminished. I'm using VB.NET.

I have a NoteButton in MyApp. The first time NoteButton is clicked Notepad starts with a particular text file. On subsequent clicks of NoteButton - if NotePad and the file are open then activate Notepad.

If NotePad has been minimized how do I change the WindowState / AppWinstyle to "normal" and bring it to the foreground?

Here is what I have...


    On Error GoTo EHandler
    Dim TheDirectory = MyApp.My.Application.Info.DirectoryPath
    If IsRunningExe("notepad.exe") Then  'Check if notepad is already running
        On Error Resume Next
        AppActivate("MyList.txt - Notepad")
        If Err.Number <> 0 Then
            Err.Clear()
            Process.Start("notepad.exe", TheDirectory & "\MyList.txt")
        End If
                    'I need to restore a minimized Notepad to "Normal" state and in the foreground 
                     here.
        Exit Sub
    End If
    Process.Start("notepad.exe", TheDirectory & "\MyList.txt")
    Exit Sub
    EHandler:
    MsgBox("There was an error opening the text file.", vbOKOnly + vbInformation)

Thanks for your time,

Greg

  • Google "vb.net ui automation restore window". [Sample hit](https://stackoverflow.com/questions/2315561/correct-way-in-net-to-switch-the-focus-to-another-application). – Hans Passant Nov 24 '20 at 20:45
  • This SO question should be able to help you out: [Restore a minimized window of another application](https://stackoverflow.com/questions/9099479/restore-a-minimized-window-of-another-application). It's in C# but you should be able translate it easy enough – JayV Nov 24 '20 at 20:47
  • You can use [IsIconic()...example here.](https://stackoverflow.com/a/19863085/2330053) – Idle_Mind Nov 24 '20 at 21:16
  • 1
    In .net, please don't use `On Error GoTo...`. We have Exception and `Try...Catch...Finally...End Try` – Mary Nov 25 '20 at 00:04
  • Thanks to all for the help. I did get it working but to avoid any embarrassment on my side - I won't post my code here. Mary - I learned VBA when the Excel 4.0 macro language was abandoned and I was forced to port my add-in app over from the macro language to VBA. It wasn't easy. That was in about 1996. My first dip into vb.Net is this app. It does what I need and want (it talks to my 3D printer). It's good enough and nobody else will have to come behind me and straighten out my spaghetti. – GregValiant Nov 28 '20 at 01:10

0 Answers0