// Copyright (C) Stichting Deltares and State of the Netherlands 2026. All rights reserved. // // This file is part of Riskeer. // // Riskeer is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public License // along with this program. If not, see . // // All names, logos, and references to "Deltares" are registered trademarks of // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. using System; using System.Drawing; using System.Windows.Forms; namespace Core.Common.Controls.Dialogs { /// /// Base class for dialogs which should be derived in order to get a consistent look and feel. /// The base class ensures: /// /// /// the dialog is shown in the center of the parent form ( is set to ); /// /// /// no task bar icon is shown ( is set to false); /// /// /// no minimize control box item is shown ( is set to false); /// /// /// no maximize control box item is shown ( is set to false); /// /// /// the minimum width and minimum height are always set during creation time; /// /// /// the owning form is always provided when showing the dialog (see ); /// /// /// the dialog can be closed using the ESC key on the keyboard (see ). /// /// /// /// /// The "new" implementation for only hides the base method. Therefore, don't cast to . /// Otherwise the original implementation will be used. /// public abstract partial class DialogBase : Form { private readonly IWin32Window dialogParent; private readonly int minHeight; private readonly int minWidth; /// /// Constructs a new . /// /// The owner of the dialog. /// The icon to show in the control box. /// The minimum width of the dialog. /// The minimum height of the dialog. /// Thrown when or is null. /// Thrown when or is not greater than 0. protected DialogBase(IWin32Window dialogParent, Bitmap icon, int minWidth, int minHeight) : this(dialogParent, BitmapToIcon(icon), minWidth, minHeight) {} /// /// Constructs a new . /// /// The owner of the dialog. /// The icon to show in the control box. /// The minimum width of the dialog. /// The minimum height of the dialog. /// Thrown when or is null. /// Thrown when or is not greater than 0. protected DialogBase(IWin32Window dialogParent, Icon icon, int minWidth, int minHeight) { InitializeComponent(); if (dialogParent == null) { throw new ArgumentNullException(nameof(dialogParent)); } if (icon == null) { throw new ArgumentNullException(nameof(icon)); } if (minWidth <= 0) { throw new ArgumentException("The minimum width of the dialog should be greater than 0"); } if (minHeight <= 0) { throw new ArgumentException("The minimum height of the dialog should be greater than 0"); } this.dialogParent = dialogParent; this.minWidth = minWidth; this.minHeight = minHeight; Icon = icon; } /// /// This method provides a new implementation of . /// In this new implementation the dialog is shown by passing the owner provided during creation time (see ). /// /// A . public new DialogResult ShowDialog() { return base.ShowDialog(dialogParent); } protected override void OnLoad(EventArgs e) { // Set the minimum size here in order to prevent virtual member calls MinimumSize = new Size(minWidth, minHeight); // Initialize the cancel button (as this cannot be done during creation time) CancelButton = GetCancelButton(); base.OnLoad(e); } /// /// Gets the cancel button of the . /// /// The cancel button. /// By forcing derivatives to provide a cancel button, dialogs can be closed by hitting the ESC key on the keyboard. protected abstract Button GetCancelButton(); private static Icon BitmapToIcon(Bitmap icon) { return icon == null ? null : Icon.FromHandle(icon.GetHicon()); } } }