[英]Adding user control to GroupBox changes the user control's layout in WinForms
[英]How to update the layout whenever a control changes height in Winforms?
我有一個自定義LayoutEngine,它可以正確處理布局。 但是,如果任何控件更改高度,我希望布局更新(再次調用)。
這可能嗎? 如果是這樣,我應該怎么做?在哪里? 在控件內部還是布局引擎? 最好是我不希望在使用此布局的任何地方重復此代碼。
因此,如果我可以將其封裝在控件或布局引擎中,那將是很好的。
在控件或布局容器中:
Dictionary<Control,int>
,它將保存每個感興趣的控件的當前高度變量。 在初始布局中:
通過感興趣的控件(如果嵌套)進行遞歸,或者
通過感興趣的控件進行迭代(如果未嵌套)
...使用“標准”迭代或遞歸,Linq遞歸或Linq“迭代” ...:
...當您遞歸或迭代時,在“字典”中為每個控件輸入其當前高度...
...在每個感興趣的控件上附加一個'SizeChanged處理程序,該處理程序在您的布局引擎類中調用相同的方法(可能是靜態方法?):為清楚起見:我們將其稱為“事件調度代碼”。
在您感興趣的所有控件的事件調度代碼中,現在由任何“受監視”控件上的SizeChanged事件觸發:
使用Control作為鍵進行字典查找:獲取Height屬性值並與Control的當前Height值進行比較:
假設Height屬性已更改:
一種。 調用您的布局引擎來“完成任務”。
b。 更新該控件的“高度”的Dictinary值。
注意:由於SizeChanged事件將以“發送者”作為“對象”進行調用,因此您需要先將其轉換為Control類型,然后再訪問其“ Height”屬性。
這是您的代碼看起來的“粗略草圖”:
// note : untested code : use caution ... test rigorously ...
// stub for the Dictionary of monitored Controls
private Dictionary<Control, int> LayoutManager_MonitoredControls = new Dictionary<Control, int>();
// the SizeChanged Event Handler you "install" for all Controls you wish to monitor
private void LayoutManager_SizeChanged(object sender, EventArgs e)
{
Control theControl = sender as Control;
int cHeight = theControl.Height;
if (LayoutManager_MonitoredControls[theControl] != theControl.Height);
{
// update the Dictionary
LayoutManager_MonitoredControls[theControl] = cHeight;
// call your code to update the Layout here ...
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.