如何:決定作用中的 MDI 子系
有時候,您會想要提供一個命令,在控制項上操作,該控制項著重于目前作用中的子表單。 例如,假設您想要將選取的文字從子表單的文字方塊複製到剪貼簿。 您會使用 Click 標準 [編輯] 功能表上的 [複製] 功能表項目事件,建立將選取的文字複製到剪貼簿的程式。
由於 MDI 應用程式可以有許多相同子表單的實例,因此程式必須知道要使用的表單。 若要指定正確的表單,請使用 ActiveMdiChild 屬性,其會傳回具有焦點或最近使用中之子表單。
當您在表單上有數個控制項時,您也需要指定哪個控制項為使用中。 ActiveMdiChild與 屬性一樣, ActiveControl 屬性會傳回具有作用中子表單焦點的控制項。 下列程式說明可以從子表單功能表、MDI 表單上的功能表或工具列按鈕呼叫的複製程式。
若要判斷使用中的 MDI 子系 (將其文字複製到剪貼簿)
在 方法中,將使用中子表單的作用中控制項文字複製到剪貼簿。
注意
此範例假設有一個 MDI 父表單 (
Form1
) 包含控制項的一或多個 MDI 子視窗 RichTextBox 。 如需詳細資訊,請參閱 建立 MDI 父表單 。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 Sub
protected 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."); } } }
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應