Solved

Windows Form Focus

Posted on 2006-10-23
2
159 Views
Last Modified: 2012-05-05
I have a MDI Parent with child windows included.  When more than one child window is opened the user can click between these windows.  When a user clicks on the window I need it to get focus and change a label value that is on the MDI Parent.  Any suggestions?
0
Comment
Question by:dhathsato
2 Comments
 
LVL 10

Expert Comment

by:Kinger247
ID: 17788084
You need to trigger an event in the child for for the mdi to receive.

Create an MDI called 'MDIForm' which opens 2 forms using withevents, Add a panel and a label in the panel.
With the following code:

Public Class MDIForm

    Private WithEvents form2 As New Form2
    Private WithEvents form3 As New Form3

    Private Sub MDIForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        form2.MdiParent = Me
        form2.Show()

        form3.MdiParent = Me
        form3.Show()
    End Sub

    Private Sub HasFocus(ByVal FormName As String) Handles form2.HasFocus, form3.HasFocus
        Label1.Text = FormName
    End Sub
End Class


Add 2 more forms with default names and add the following code to both (mind the form name)

Public Class Form2
    Public Event HasFocus(ByVal FormName As String)

    Private Sub Form2_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.GotFocus
        RaiseEvent HasFocus(Me.Text)
    End Sub
End Class
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 250 total points
ID: 17788929
Hi dhathsato;

All you need to do is in each child form create an event handler called Activated. Add the text to the MDI parent label as shown below. Each child form has a reference to the parent MDI form. Cast that reference to the form type of the MDI parent as shown below.

In the sample code the name of the MDI parent form is MyMDIParent and the MyMDIParent form has a label called Label1. The below code will take the text in the child form title bar text and add it to the label of the MDI parent form.

    Private Sub Child_Activated(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles MyBase.Activated

        CType(Me.MdiParent, MyMDIParent).Label1.Text = Me.Text

    End Sub


Fernando
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question