Nasıl yapılır: Etkin MDI Alt Öğesini Belirleme
Bazen, o anda etkin olan alt forma odaklanan denetim üzerinde çalışan bir komut sağlamak gerekir. Örneğin, alt formun metin kutusundan seçilen metni Pano'ya kopyalamak istediğinizi varsayalım. Standart Düzenle menüsündeki Kopyala menü öğesinin olayını kullanarak seçili metni Click Panoya kopyalanan bir yordam oluşturabilirsiniz.
Bir MDI uygulaması aynı alt formun birçok örneğine sahip olduğundan, yordamın hangi formun kullan gerektiğini biliyor olması gerekir. Doğru formu belirtmek için odağı olan veya en son etkin olan alt formu ActiveMdiChild döndüren özelliğini kullanın.
Bir formda birden fazla denetim olduğunda, hangi denetimin etkin olduğunu da belirtmeniz gerekir. özelliği ActiveMdiChild gibi özelliği ActiveControl de etkin alt forma odaklanarak denetimi döndürür. Aşağıdaki yordam bir alt form menüsünden, MDI formundaki bir menüden veya araç çubuğu düğmesinden çağrılabilir bir kopyalama yordamını gösterir.
Etkin MDI alt dosyasını belirlemek için (metnini Panoya kopyalamak için)
Bir yöntemin içinde, etkin alt formun etkin denetimi metnini Pano'ya kopyalayın.
Not
Bu örnekte, bir denetim içeren bir veya daha fazla MDI alt pencereye sahip bir MDI üst formu (
Form1) olduğu RichTextBox varsaydır. Daha fazla bilgi için bkz. MDI Üst Formları Oluşturma.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 Dim theBox As RichTextBox = _ TryCast(activeChild.ActiveControl, RichTextBox) If (Not theBox Is Nothing) Then 'Put selected text on Clipboard. Clipboard.SetDataObject(theBox.SelectedText) Else MessageBox.Show("You need to select a RichTextBox.") End If End If End Subprotected void mniCopy_Click (object sender, System.EventArgs e) { // Determine the active child form. Form activeChild = this.ActiveMdiChild; // If there is an active child form, find the active control, which // in this example should be a RichTextBox. if (activeChild != null) { try { RichTextBox theBox = (RichTextBox)activeChild.ActiveControl; if (theBox != null) { // Put the selected text on the Clipboard. Clipboard.SetDataObject(theBox.SelectedText); } } catch { MessageBox.Show("You need to select a RichTextBox."); } } }