Active MDI Form Controls in VB.NET

In this Article You can learn How to determine a Active MDI form and Working with its controls.
  • 5538
 

Introduction

In this article, I am going to tell about the working of controls which has focus on the currently active child form.   

Get to Work

With the help of below given example, you can learn how to create an procedure that copies selected text to the clipboard using the click event of the copy menu control. Here are the step required to build an procedure to copy selected text from the child form text box.

To Create a procedure on active form or giving a specific instance of the recently active child form you can use ActiveMdiChild property. Same as to specify the active control of the form on which procedure works you have use ActiveControl property.

Step to Create and Implement MDI Child Form

  1. Assumes there is an MDI parent form having MenuStrip with option New, Window and Close in New menu, main form contain one Child form having a RichTexBox.
     
    1.gif

  2. Add one More control in Main form MenuStrip As Copy under Edit Menu.
     
     2.gif

  3. Double click on Copy control and write this Code.
     

    Public Sub mniCopy_Click(ByVal sender As Object, _
       ByVal e As System.EventArgs) Handles mniCopy.Click

       ' Determine the active child form.
       Dim activeChild As Form = Me.ActiveMDIChild

       ' If there is an active child form, find the active control, which
       ' in this example should be a RichTextBox.
       If (Not activeChild Is Nothing) Then
          Try
             Dim theBox As RichTextBox = _
             Ctype(activeChild.ActiveControl, RichTextBox)
             If (Not theBox Is Nothing) Then
                ' Put selected text on Clipboard.
                Clipboard.SetDataObject(theBox.SelectedText)
             End If
          Catch
             MessageBox.Show("You need to select a RichTextBox.")
          End Try
       End If
    End Sub
     

  4. Debug the application and click on New button a MDI Child form with RichTextBox will open. Now you write anything in text box and select them now go to Copy control in Edit Menu and Click on it. Your text was copied you can past the text from your clipboard.

      3.gif

Summary

In this article, we discussed how to Create a procedure which helps to copy selected text from the active child form to clipboard using Visual Studio 2010 and VB.NET.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.