我創建了一個名為TQSSetpointEditor的 UserControl ,出于所有意圖和目的,它只是一個顯示有關物件的一些資訊的控制元件。
我試圖讓用戶控制元件更改其寬度以始終適合視窗/表單的大小減去偏移量以在控制元件和邊緣之間添加一點邊框。
我不能只是停靠這些專案的原因是該專案位于另一個名為TQSSetpointGroup 的自定義用戶控制元件中,該控制元件托管一組 TQSSetpointEditor。TQSSetpoint 組只是一個流程布局面板,因此我無法將控制元件停靠在控制元件中,因為它沒有按照我希望的方式執行。
我發現最簡單的解決方案是通過delegate將 Setpoint Editors 的寬度更改為視窗的寬度。問題是我根本不知道如何使用委托,更不用說實際使用了。
我的想法圍繞著每當調整表單大小時,回呼到 TQSSetpointEditor 然后將控制元件的寬度調整為表單的新寬度。
圖1:

正如您從影像中看到的,用戶控制元件越過表單的右側,但我希望用戶控制元件通過從表單實體發送資訊的委托根據表單的寬度動態地重塑其寬度到 setpointeditor 控制元件的所有實體(主要是因為我想知道如何正確使用委托)。
uj5u.com熱心網友回復:
解決方案: 將公共委托放在同一個命名空間中:
public delegate void ResizeControlCallback(Size newSize);
然后將委托的實體放在要從中發送資料的表單中:
// Delegate to change the size of each editor based on the size of the form.
public event ResizeControlCallback ResizeControl;
/// <summary>
/// Sends over the size of the form everytime it's resized to give the controls a dynamic feel.
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
private void HandleChildControlResize(object sender, EventArgs args)
{
if (ResizeControl != null) { ResizeControl(Size); }
}
然后在您想要監聽并根據事件更改其屬性的控制元件中,向其添加一個事件監聽器,然后從引數中“獲取”值。
WrapperForm.Instance.ResizeControl = HandleResize;
/// <summary>
/// Resizes the editor width, based on the width of the wrapperform, also changes the padding based on the width;
/// </summary>
/// <param name="newSize"></param>
private void HandleResize(Size newSize)
{
Width = newSize.Width - RightOffset;
Height = EditorSize.Height;
if (Width <= 500) { defaultMargin = new Padding(0, 12, 0, 0); }
if (Width > 501 && Width <= 750) { defaultMargin = new Padding(10, 12, 1, 10); }
if (Width > 751 && Width <= 1000) { defaultMargin = new Padding(20, 12, 20, 0); }
if (Width > 1001 && Width <= 1500) { defaultMargin = new Padding(30, 12, 30, 0); }
if (Width >= 1501) { defaultMargin = new Padding(40, 12, 40, 0); }
spidNameLbl.Margin = defaultMargin;
unitMeasureLbl.Margin = defaultMargin;
tdtsControl.Margin = defaultMargin;
warningLbl.Margin = defaultMargin;
utcModifiedLbl.Margin = defaultMargin;
notesLbl.Margin = defaultMargin;
}
因此,每次調整表單大小時,控制元件的寬度也將調整大小。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/312901.html
上一篇:PythonKivy接收[CRITICAL]App.root必須是Widget的_instance_
下一篇:閃亮的標題影像似乎未格式化
