Click here to Skip to main content
15,886,518 members

Keep parent form in-focus (modal) when interating with child form?

kkdxghlctlcxxtidyuum asked:

Open original thread
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.
Tags: Visual Basic

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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