Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I used a simple dialog with a static method <strong>ShowCustomButtonsDialog</strong>. I placed a text label in the top left corner and changed border style to Dialog. Method simply returns button index or -1.</p> <pre><code>public partial class CustomButtonsDialog : Form { private const int ButtonHeight = 24; private const int ButtonPadding = 6; private const int ButtonInnerPadding = 5; private const int MaxFormWidth = 700; private int buttonIndex = -1; public int ButtonIndex { get { return buttonIndex; } private set { buttonIndex = value; } } public static int ShowCustomButtonsDialog(string text, string title, params string[] buttonsText) { var dlg = new CustomButtonsDialog(text, title, buttonsText.ToList()); dlg.ShowDialog(); return dlg.ButtonIndex; } public static int ShowCustomButtonsDialog(string text, string title, List&lt;string&gt; buttonsText) { var dlg = new CustomButtonsDialog(text, title, buttonsText); dlg.ShowDialog(); return dlg.ButtonIndex; } public CustomButtonsDialog() { InitializeComponent(); } private CustomButtonsDialog(string text, string title, List&lt;string&gt; buttonsText) { InitializeComponent(); Text = title; labelText.Text = text; // добавить кнопки var formWidth = ClientSize.Width; List&lt;int&gt; buttonWidths; using (var gr = CreateGraphics()) { buttonWidths = buttonsText.Select(b =&gt; (int)gr.MeasureString(b, Font).Width + 2 * ButtonInnerPadding).ToList(); } var totalButtonWd = buttonWidths.Sum() + (buttonWidths.Count - 1) * ButtonPadding; if (totalButtonWd &gt; formWidth) { if (totalButtonWd &lt;= MaxFormWidth) Width = Width - ClientSize.Width + totalButtonWd + ButtonPadding * 2; else {// trim some buttons Width = Width - ClientSize.Width + MaxFormWidth; totalButtonWd = ClientSize.Width - ButtonPadding * 2; var avgWidth = (totalButtonWd - (buttonsText.Count - 1) * ButtonPadding) / buttonsText.Count; var sumThins = buttonWidths.Sum(w =&gt; w &lt;= avgWidth ? w : 0); var countThins = buttonWidths.Count(w =&gt; w &lt;= avgWidth); var countFat = buttonsText.Count - countThins; var spareRoom = totalButtonWd - sumThins; var fatWidth = (countThins == 0) || (countFat == 0) ? avgWidth : (spareRoom - (countThins - 1)*ButtonPadding)/countFat; for (var i = 0; i &lt; buttonWidths.Count; i++) if (buttonWidths[i] &gt; avgWidth) buttonWidths[i] = fatWidth; } } // buttons' Y-coords and height labelText.MaximumSize = new Size(totalButtonWd, labelText.MaximumSize.Height); var buttonTop = labelText.Bottom + ButtonPadding; var formHeight = buttonTop + ButtonHeight + ButtonPadding; Height = Height - ClientSize.Height + formHeight; // do make buttons var buttonLeft = ButtonPadding; var tag = 0; for (var i = 0; i &lt; buttonWidths.Count; i++) { var button = new Button { Parent = this, Width = buttonWidths[i], Height = ButtonHeight, Left = buttonLeft, Top = buttonTop, Text = buttonsText[i], Tag = tag++ }; button.Click += ButtonClick; buttonLeft = button.Right + ButtonPadding; Controls.Add(button); } } private void ButtonClick(object sender, EventArgs e) { ButtonIndex = (int) ((Button) sender).Tag; Close(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload