From 8e21ddffc9718ff88fb32743f73bd4205a4c1d3c Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Wed, 27 Jul 2022 21:56:59 +0200 Subject: [PATCH] Vertical align the data control --- I18N Commander/UI WinForms/Components/Setting.cs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/I18N Commander/UI WinForms/Components/Setting.cs b/I18N Commander/UI WinForms/Components/Setting.cs index 021eef9..f0a1bd6 100644 --- a/I18N Commander/UI WinForms/Components/Setting.cs +++ b/I18N Commander/UI WinForms/Components/Setting.cs @@ -17,17 +17,25 @@ public partial class Setting : UserControl this.labelIcon.Image = settingMetaData.Icon; this.labelSettingName.Text = settingMetaData.SettingName(); this.labelExplanation.Text = settingMetaData.SettingExplanation(); + + var dataControl = settingMetaData.SetupDataControl(); + this.tableLayout.Controls.Add(dataControl, 2, 0); - // Calculate the needed height of the explanation label when the parent window is resized: + // Ensure, that this data control is vertical centered by calculating the needed margin, considering the outer size of the table layout: + var margin = (this.tableLayout.GetRowHeights().First() - dataControl.Height) / 2f; + dataControl.Margin = new Padding(0, (int) margin, 0, (int)margin); + + // Calculate the needed height of the explanation label & centering of the data control when the parent window is resized: this.tableLayout.Resize += (sender, args) => { // Adjust the height of the parent controls (table & user control): this.tableLayout.Height = Math.Max((int)this.labelExplanation.CreateGraphics().MeasureString(this.labelExplanation.Text, this.labelExplanation.Font, new SizeF(this.labelExplanation.Width, 1000)).Height, 66); this.Height = this.tableLayout.Height + this.tableLayout.Margin.Vertical; + + // Ensure, that this data control is vertical centered by calculating the needed margin, considering the outer size of the table layout: + var margin = (this.tableLayout.GetRowHeights().First() - dataControl.Height) / 2f; + dataControl.Margin = new Padding(0, (int) margin, 0, (int)margin); }; - - var dataControl = settingMetaData.SetupDataControl(); - this.tableLayout.Controls.Add(dataControl, 2, 0); } private readonly record struct SettingUIData(