![](/img/trans.png)
[英]Bring the front user control with a button which is inside in another user control
[英]Bringing front a user control by a button which is located in another user control
我有一個場景(Windows窗體,C#、. NET):
us1
和us2
。 btnDisplay
內us1
。 us1
是可見的,因為我已經將它放在前面。 btnDisplay
, us2
必須走到前面,但我沒有任何控制us2
內us1.cs
! 我應該如何實施呢?
我會考慮兩種選擇。
1)第一種選擇是將btnDisplay的修飾符更改為internal
或public
而不是private
。
這使您可以訂閱表單中的click事件。 當觸發click事件時,您只需將另一個控件置於最前面即可。
public Form1()
{
InitializeComponent();
us11.btnDisplay.Click += BtnDisplay_Click;
}
private void us11_DisplayButtonClicked(object sender, EventArgs e)
{
us21.BringToFront();
}
2)另一個選擇是將您的us2控件傳遞給us1控件,以便us1控件可以將us2置於最前面。
public partial class us1 : UserControl
{
// Property to hold the us2 control.
public us2 us2 { get; set; }
public us1()
{
InitializeComponent();
}
private void btnDisplay_Click(object sender, EventArgs e)
{
// Bring the other control to the front if the property has been set.
us2?.BringToFront();
}
}
在您的表單中,在us1上設置us2屬性:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
us11.us2 = us21;
}
}
如果對我們來說“知道”另一個控件有意義,那么我可能會選擇選項2。 如果讓表單了解單擊的按鈕,然后再執行某項操作更有意義,則選項1可能更好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.