Results 1 to 4 of 4

Thread: [RESOLVED] set focus

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    104

    Resolved [RESOLVED] set focus

    i have a program and when i click a button in the program i want ti to set focus on another process i have runing. the other process is running in windowed mode how can i do this?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: set focus

    The easy way is to use the AppActivate function. The alternative is to use a Process object to get the handle to the other app's main window, then call the SetForegroundWindow API function. Some consider it poor form to use Runtime functions like AppActivate, and I'm one of them, but I'd consider that preferable to resorting to API calls. The difference is that AppActivate will not restore the application window if it's minimised, whereas you can test for that and restore it using API functions, although it requires more than just SetForegroundWindow.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: set focus

    You can test for the app being minimized by using the IsIconic API.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    104

    Re: set focus

    Ok i used AppActivate and it works just fine for what i need. I dont need to restore the process if it has been minimized. Here is the code i used incase anyone else is wondering how to do it this way.

    VB Code:
    1. Dim proc As Process
    2. Dim h As Integer
    3.  
    4.   Private Sub form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    5.  
    6.         proc = Process.GetProcessesByName("myprocess")(0)' the process to set focus too must be started before this code is run
    7.  
    8.     End Sub
    9.  
    10.   Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
    11.  
    12.                 AppActivate(proc.Id)'sets focus to the process
    13.     End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width