2011-11-30 50 views

回答

2

假設您的用戶控件名爲UserControl1和UserControl2。除非UserControl1具有對UserControl2的引用,否則不能直接對其進行更改。在這種情況下,一種解決方案是允許窗體或父控件通過向UserControl1添加事件並在窗體上處理它來處理進行更改。

在的UserControl1:

'Define an Event the form can handle at the class level 
Public Event SomePropertyUpdated() 

然後在任何你需要它的時候,你會想改變其他控制文本框提高你的事件:

RaiseEvent SomePropertyUpdated() 

在形式:

'The sub that is called when the second control needs updated 
Public Sub UpdateTextBoxes() 
     UserControl2.Textbox1.text = userControl1.Property 
End Sub 

在表單的加載事件中爲您創建的事件添加處理程序:

AddHandler UserControl1.SomePropertyUpdated, AddressOf UpdateTextBoxes 

在窗體的關閉事件刪除事件的處理:

RemoveHandler UserControl1.SomePropertyUpdated, AddressOf UpdateTextBoxes 

這是有一些方法來處理這種情況之一。你試圖做的具體細節通常決定使用什麼方法。

相關問題