簡體   English   中英

VB.Net WinForms UserControl

[英]VB.Net WinForms UserControl

我已經加載了兩個UserControls如何在另一個加載的UserControl上更改UserControl上的TextBox文本屬性。

假設您的用戶控件名為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

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

 AddHandler UserControl1.SomePropertyUpdated, AddressOf UpdateTextBoxes

在窗體的關閉事件中,刪除事件的處理程序:

RemoveHandler UserControl1.SomePropertyUpdated, AddressOf UpdateTextBoxes

這是處理這種情況的幾種方法之一。 您嘗試做的事情的具體細節通常決定了使用什么方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM