Files
Pilz/Pilz.UI/Dialogs/FlyoutBase.cs
Pilzinsel64 cb2831ce4a refactor flyoutbase and radflyoutbase
- hopefully fix problems with child classes and tabindex problems
2025-01-14 07:50:47 +01:00

207 lines
5.9 KiB
C#

using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
namespace Pilz.UI.Dialogs;
public partial class FlyoutBase : UserControl
{
private TableLayoutPanel tableLayoutPanel_TitlePanel;
private Label label_Title;
private TableLayoutPanel tableLayoutPanel_ActionPanel;
private Button button_Cancel;
private Button button_Accept;
public DialogResult Result { get; protected set; }
public bool RegisterDialogAccept { get; set; } = true;
public bool RegisterDialogCancel { get; set; } = false;
[DefaultValue(true)]
protected bool ActionPanelVisible
{
get => tableLayoutPanel_ActionPanel.Visible;
set => tableLayoutPanel_ActionPanel.Visible = value;
}
[DefaultValue(true)]
protected bool CancelButtonVisible
{
get => button_Cancel.Visible;
set => button_Cancel.Visible = value;
}
[DefaultValue(true)]
protected bool CancelButtonEnable
{
get => button_Cancel.Enabled;
set => button_Cancel.Enabled = value;
}
[DefaultValue(true)]
protected bool ConfirmButtonEnable
{
get => button_Accept.Enabled;
set => button_Accept.Enabled = value;
}
[Localizable(true)]
[DefaultValue("Okay")]
public string ConfirmButtonText
{
get => button_Accept.Text;
set => button_Accept.Text = value;
}
[Localizable(true)]
[DefaultValue("Cancel")]
public string CancelButtonText
{
get => button_Cancel.Text;
set => button_Cancel.Text = value;
}
[Localizable(true)]
[DefaultValue("")]
public string Title
{
get => label_Title.Text;
set
{
label_Title.Text = value;
SetShowTitlePanel();
}
}
public FlyoutBase()
{
InitializeComponent();
ParentChanged += FlyoutBase_ParentChanged;
}
[MemberNotNull(nameof(tableLayoutPanel_TitlePanel))]
[MemberNotNull(nameof(label_Title))]
[MemberNotNull(nameof(tableLayoutPanel_ActionPanel))]
[MemberNotNull(nameof(button_Cancel))]
[MemberNotNull(nameof(button_Accept))]
private void InitializeComponent()
{
SuspendLayout();
// button_Accept
button_Accept = new Button
{
Name = "button_Accept",
Size = new Size(75, 23),
TabIndex = int.MaxValue - 1,
Text = "Accept",
UseVisualStyleBackColor = true
};
button_Accept.Click += Button_Accept_Click;
// button_Cancel
button_Cancel = new Button
{
Name = "button_Cancel",
Size = new Size(75, 23),
TabIndex = int.MaxValue,
Text = "Cancel",
UseVisualStyleBackColor = true
};
button_Cancel.Click += Button_Cancel_Click;
// label_Title
label_Title = new Label
{
Name = "label_Title",
Dock = DockStyle.Fill,
TextAlign = ContentAlignment.MiddleLeft
};
// tableLayoutPanel_TitlePanel
tableLayoutPanel_TitlePanel = new TableLayoutPanel
{
Name = "tableLayoutPanel_TitlePanel",
Dock = DockStyle.Top,
Size = new Size(ClientSize.Width, 29),
Visible = false,
ColumnCount = 1,
RowCount = 1
};
tableLayoutPanel_TitlePanel.SuspendLayout();
tableLayoutPanel_TitlePanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F));
tableLayoutPanel_TitlePanel.RowStyles.Add(new RowStyle(SizeType.Percent, 50F));
tableLayoutPanel_TitlePanel.Controls.Add(label_Title, 0, 0);
// tableLayoutPanel_ActionPanel
tableLayoutPanel_ActionPanel = new TableLayoutPanel
{
Name = "tableLayoutPanel_ActionPanel",
AutoSize = true,
AutoSizeMode = AutoSizeMode.GrowAndShrink,
Dock = DockStyle.Bottom,
Size = new Size(ClientSize.Width, 29),
ColumnCount = 3,
RowCount = 1
};
tableLayoutPanel_ActionPanel.SuspendLayout();
tableLayoutPanel_ActionPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F));
tableLayoutPanel_ActionPanel.ColumnStyles.Add(new ColumnStyle());
tableLayoutPanel_ActionPanel.ColumnStyles.Add(new ColumnStyle());
tableLayoutPanel_ActionPanel.RowStyles.Add(new RowStyle());
tableLayoutPanel_ActionPanel.Controls.Add(button_Cancel, 2, 0);
tableLayoutPanel_ActionPanel.Controls.Add(button_Accept, 1, 0);
// FlyoutBase
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
Controls.Add(tableLayoutPanel_ActionPanel);
Controls.Add(tableLayoutPanel_TitlePanel);
Size = new Size(300, 150);
tableLayoutPanel_TitlePanel.ResumeLayout(false);
tableLayoutPanel_ActionPanel.ResumeLayout(false);
ResumeLayout(false);
PerformLayout();
}
private void FlyoutBase_ParentChanged(object? sender, EventArgs e)
{
var frm = FindForm();
if (frm != null)
{
if (RegisterDialogAccept)
frm.AcceptButton = button_Accept;
if (RegisterDialogCancel)
frm.CancelButton = button_Cancel;
}
}
protected void Close(DialogResult result)
{
Result = result;
if (FindForm() is DialogBase dialogForm)
dialogForm.Close();
}
protected virtual bool ValidateOK()
{
return true;
}
protected virtual void SetShowTitlePanel()
{
tableLayoutPanel_TitlePanel.Visible = !string.IsNullOrWhiteSpace(label_Title.Text);
}
protected virtual void Button_Accept_Click(object? sender, EventArgs e)
{
if (ValidateOK())
Close(DialogResult.OK);
}
protected virtual void Button_Cancel_Click(object? sender, EventArgs e)
{
Close(DialogResult.Cancel);
}
}