예제 #1
0
        public MessageBoxCustomForm(string messageTop, string messageBottom, string title, MessageBoxIcon icon,
                                    string okText, string cancelText, bool okIsDangerous, string[]?choiceStrings = null)
        {
            InitializeComponent();

            _multiChoice = choiceStrings?.Length > 0;

            #region Set fonts

            // Set these after InitializeComponent() in case that sets other fonts, but before anything else
            MessageTopLabel.Font    = SystemFonts.MessageBoxFont;
            MessageBottomLabel.Font = SystemFonts.MessageBoxFont;
            SelectAllButton.Font    = SystemFonts.MessageBoxFont;
            OKButton.Font           = SystemFonts.MessageBoxFont;
            Cancel_Button.Font      = SystemFonts.MessageBoxFont;
            ChoiceListBox.Font      = SystemFonts.DefaultFont;

            #endregion

            #region Set passed-in values

            if (icon != MessageBoxIcon.None)
            {
                SetMessageBoxIcon(icon);
            }

            Text = title;
            MessageTopLabel.Text    = messageTop;
            MessageBottomLabel.Text = messageBottom;

            if (_multiChoice)
            {
                // Set this first: the list is now populated
                for (int i = 0; i < choiceStrings !.Length; i++)
                {
                    ChoiceListBox.Items.Add(choiceStrings[i]);
                }
            }
            else
            {
                ChoiceListBox.Hide();
                SelectButtonsFLP.Hide();
                MessageBottomLabel.Hide();
            }

            #endregion

            #region Autosize controls

            int innerControlWidth = MainFLP.Width - 10;
            MessageTopLabel.MaximumSize    = new Size(innerControlWidth, MessageTopLabel.MaximumSize.Height);
            MessageBottomLabel.MaximumSize = new Size(innerControlWidth, MessageBottomLabel.MaximumSize.Height);

            // Set this second: the list is now sized based on its content
            if (_multiChoice)
            {
                ChoiceListBox.Height =
                    (ChoiceListBox.ItemHeight * ChoiceListBox.Items.Count.Clamp(5, 20)) +
                    ((SystemInformation.BorderSize.Height * 4) + 3);
            }

            // Set these before window autosizing
            if (_multiChoice)
            {
                ChoiceListBox.Width    = innerControlWidth;
                SelectButtonsFLP.Width = innerControlWidth + 1;
            }

            // Set these before setting button text
            if (okIsDangerous)
            {
                OKButton.TextImageRelation = TextImageRelation.ImageBeforeText;
                OKButton.ImageAlign        = ContentAlignment.MiddleCenter;
                OKButton.Image             = Resources.ExclMarkCircleRed_14;
            }

            OKButton.SetTextAutoSize(okText, OKButton.Width);
            Cancel_Button.SetTextAutoSize(cancelText, Cancel_Button.Width);

            #endregion

            Localize();

            SelectButtonsFLP.Height = SelectAllButton.Height;
        public MessageBoxCustomForm(
            string messageTop,
            string messageBottom,
            string title,
            MessageBoxIcon icon,
            string okText,
            string cancelText,
            bool okIsDangerous,
            string[]?choiceStrings     = null,
            bool multiSelectionAllowed = true)
        {
#if DEBUG
            InitializeComponent();
#else
            InitializeComponentSlim();
#endif

            _multiChoice = choiceStrings?.Length > 0;

            #region Set fonts

            // Set these after InitializeComponent() in case that sets other fonts, but before anything else
            MessageTopLabel.Font    = SystemFonts.MessageBoxFont;
            MessageBottomLabel.Font = SystemFonts.MessageBoxFont;
            SelectAllButton.Font    = SystemFonts.MessageBoxFont;
            OKButton.Font           = SystemFonts.MessageBoxFont;
            Cancel_Button.Font      = SystemFonts.MessageBoxFont;
            ChoiceListBox.Font      = SystemFonts.DefaultFont;

            #endregion

            #region Set passed-in values

            if (icon != MessageBoxIcon.None)
            {
                ControlUtils.SetMessageBoxIcon(IconPictureBox, icon);
            }

            Text = title;
            MessageTopLabel.Text    = messageTop;
            MessageBottomLabel.Text = messageBottom;

            if (_multiChoice)
            {
                ChoiceListBox.MultiSelect = multiSelectionAllowed;

                ChoiceListBox.BeginUpdate();
                // Set this first: the list is now populated
                for (int i = 0; i < choiceStrings !.Length; i++)
                {
                    ChoiceListBox.Items.Add(choiceStrings[i]);
                }
                ChoiceListBox.EndUpdate();

                if (!multiSelectionAllowed)
                {
                    SelectAllButton.Hide();
                }
            }
            else
            {
                ChoiceListBox.Hide();
                SelectButtonsFLP.Hide();
                MessageBottomLabel.Hide();
            }

            #endregion

            #region Autosize controls

            int innerControlWidth = MainFLP.Width - 10;
            MessageTopLabel.MaximumSize    = new Size(innerControlWidth, MessageTopLabel.MaximumSize.Height);
            MessageBottomLabel.MaximumSize = new Size(innerControlWidth, MessageBottomLabel.MaximumSize.Height);

            if (_multiChoice)
            {
                // Set this second: the list is now sized based on its content
                ChoiceListBox.Size = new Size(innerControlWidth,
                                              (ChoiceListBox.ItemHeight * ChoiceListBox.Items.Count.Clamp(5, 20)) +
                                              (SystemInformation.BorderSize.Height * 2));

                // Set this before window autosizing
                SelectButtonsFLP.Width = innerControlWidth + 1;
            }

            // Set these before setting button text
            if (okIsDangerous)
            {
                OKButton.TextImageRelation = TextImageRelation.ImageBeforeText;
                OKButton.ImageAlign        = ContentAlignment.MiddleCenter;
                OKButton.Image             = Images.RedExclamationMarkCircle;
            }

            // This is here instead of in the localize method because it's not really localizing, it's just setting
            // text that was passed to it. Which in our case actually is localized, but you know.
            OKButton.Text      = okText;
            Cancel_Button.Text = cancelText;

            #endregion

            Localize();

            SelectButtonsFLP.Height = SelectAllButton.Height;

            #region Autosize window

            // Run this after localization, so we have the right button widths

            #region Local functions

            int GetControlFullHeight(Control control, bool onlyIfVisible = false) =>
            !onlyIfVisible || _multiChoice
                    ? control.Margin.Vertical +
            control.Height
                    : 0;