Click here to Skip to main content
15,887,435 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I have a child form within my form. When I click inside the child form, the parent form (my form) loses focus. How can I prevent this from occurring and maintain focus of main parent form even if clicking in child form?

Just to clarify what I mean by "in-focus", this picture demonstrates a focused form (the one at the front), and a form that's not in focus (the one at the back):
http://www.functionx.com/vcsharp/images/modal1.gif

Thanks in advance. :D

My code:
VB
<pre>Imports System.Runtime.InteropServices
Public Class Form1

    Private Const WM_SYSCOMMAND As Integer = &H112
    Private Const SC_MAXIMIZE As Integer = &HF030

    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
    Public Shared Function SetParent(ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As IntPtr
    End Function

    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
    Friend Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
    End Function

    Private Sub SetChildTimer1_Tick(sender As Object, e As EventArgs) Handles SetChildTimer1.Tick

        Dim NtPdProcess As Process = Process.GetProcessesByName("notepad").FirstOrDefault

        If NtPdProcess IsNot Nothing Then

            SetParent(NtPdProcess.MainWindowHandle, Panel1.Handle)
            SendMessage(NtPdProcess.MainWindowHandle, WM_SYSCOMMAND, SC_MAXIMIZE, 0)

        End If

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim startInfo As New ProcessStartInfo("notepad.exe")
        Process.Start(startInfo)
    End Sub
End Class


What I have tried:

I've tried experimenting with ZOrder, but frankly, I'm not very experienced yet.
I've tried experimenting with ShowDialog's, to no avail.
Posted
Updated 19-Nov-17 0:32am
v2
Comments
[no name] 18-Nov-17 18:10pm    
I assume you like to have Focus on the "main form" for Keyboard Input. I don't know it for vb.net but in other Tools there is something like "KeyPreview" for a form. Maybe there is something similar in vb.net?
[no name] 18-Nov-17 18:29pm    
For keyboard input, but not only for keyboard input. I have other controls on my parent window too that require it to be in focus when interacting with the child form (e.g. menustrip - if the main form is not in focus, the user must click twice to open a menustrip title (first click to get the form in focus, second to register click on menustrip item). Thanks a lot for the suggestion though :)
CHill60 19-Nov-17 5:04am    
How do you hope to interact with the child form if another form has focus?
Use key preview and pass anything that can't be handled by the child through to the parent
[no name] 19-Nov-17 5:14am    
It;s okay, trust me, the child form can be interacted with even if it doesn't have focus (which is unusual, but true). Does this make a possible solution easier?
CHill60 19-Nov-17 6:13am    
I asked "how". Perhaps the picture would have helped but the link does not work

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900