private int AddButton(TableLayoutPanel panel, string name, string text, InnerDialogReturnCodeEnum ReturnCode) { Button button = new Button(); button.Click += buttonClick; button.Text = text; button.Name = name; button.MinimumSize = new Size(BUTTON_MIN_WIDTH, 20); button.AutoSize = true; button.Anchor = AnchorStyles.None; button.Margin = new Padding(BUTTON_PADDING, 20, BUTTON_PADDING, 20); panel.Controls.Add(button, _formButtons.Count, 0); _formButtons.Add(name, ReturnCode); return(button.Width + button.Margin.Horizontal); }
public IValue answer; // значение переменной устанавливается после закрытия диалога QueryBoxWithTimeOut(string queryText, CLREnumValueWrapper <InnerQuestionDialogModeEnum> buttons, int timeOut, CLREnumValueWrapper <InnerDialogReturnCodeEnum> defaultButton, string title, CLREnumValueWrapper <InnerDialogReturnCodeEnum> timeOutButton) { _timeOut = timeOut; _timeCounter = _timeOut; _form = new Form(); _form.Height = 140; _form.Width = 200; _form.Text = title; _form.StartPosition = FormStartPosition.CenterScreen; _form.MinimumSize = new Size(200, 140); _form.MaximizeBox = false; _form.MinimizeBox = false; _form.ControlBox = false; _form.FormBorderStyle = FormBorderStyle.FixedDialog; TableLayoutPanel CommonPanel = new TableLayoutPanel(); CommonPanel.ColumnCount = 1; CommonPanel.RowCount = 2; CommonPanel.AutoSize = true; _form.Controls.Add(CommonPanel); // Label _queryLabel = new Label(); _queryLabel.Text = queryText; _queryLabel.Padding = new Padding(10, 10, 10, 10); _queryLabel.TextAlign = ContentAlignment.MiddleCenter; _queryLabel.AutoSize = true; Panel panel = new Panel(); panel.Dock = DockStyle.Top; panel.AutoSize = true; panel.Controls.Add(_queryLabel); CommonPanel.Controls.Add(panel, 0, 0); panel.BringToFront(); panel.Anchor = AnchorStyles.None; // Buttons _formButtons = new Dictionary <string, InnerDialogReturnCodeEnum>(); _buttonsPanel = new TableLayoutPanel(); _buttonsPanel.RowStyles.Clear(); _buttonsPanel.ColumnStyles.Clear(); _buttonsPanel.Height = 20; _buttonsPanel.Padding = new Padding(20, 0, 20, 0); _buttonsPanel.Dock = DockStyle.Bottom; _buttonsPanel.AutoSize = true; _buttonsPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize)); _buttonsPanel.Anchor = AnchorStyles.None; CommonPanel.Controls.Add(_buttonsPanel, 0, 1); _buttonsPanel.BringToFront(); _buttonsPanel.RowCount = 1; switch ((InnerQuestionDialogModeEnum)buttons.UnderlyingObject) { case InnerQuestionDialogModeEnum.YesNo: AddButton(_buttonsPanel, "Yes", "Да", InnerDialogReturnCodeEnum.Yes); AddButton(_buttonsPanel, "No", "Нет", InnerDialogReturnCodeEnum.No); break; case InnerQuestionDialogModeEnum.YesNoCancel: AddButton(_buttonsPanel, "Yes", "Да", InnerDialogReturnCodeEnum.Yes); AddButton(_buttonsPanel, "No", "Нет", InnerDialogReturnCodeEnum.No); AddButton(_buttonsPanel, "Cancel", "Отмена", InnerDialogReturnCodeEnum.Cancel); break; case InnerQuestionDialogModeEnum.OK: AddButton(_buttonsPanel, "OK", "ОК", InnerDialogReturnCodeEnum.OK); break; case InnerQuestionDialogModeEnum.OKCancel: AddButton(_buttonsPanel, "OK", "ОК", InnerDialogReturnCodeEnum.OK); AddButton(_buttonsPanel, "Cancel", "Отмена", InnerDialogReturnCodeEnum.Cancel); break; case InnerQuestionDialogModeEnum.RetryCancel: AddButton(_buttonsPanel, "Retry", "Повторить", InnerDialogReturnCodeEnum.Retry); AddButton(_buttonsPanel, "Cancel", "Отмена", InnerDialogReturnCodeEnum.Cancel); break; case InnerQuestionDialogModeEnum.AbortRetryIgnore: AddButton(_buttonsPanel, "Abort", "Прервать", InnerDialogReturnCodeEnum.Abort); AddButton(_buttonsPanel, "Retry", "Повторить", InnerDialogReturnCodeEnum.Retry); AddButton(_buttonsPanel, "Ignore", "Пропустить", InnerDialogReturnCodeEnum.Ignore); break; default: Console.WriteLine("Передано неизвестное значение типа РежимДиалогаВопрос"); return; } _buttonsPanel.ColumnCount = _buttonsPanel.Controls.Count; string timeOutButtonName = ""; if (_buttonsPanel.Controls.Count > 0) { if (defaultButton == null) { _defaultButton = _formButtons[_buttonsPanel.Controls[0].Name]; } else { _defaultButton = _formButtons[defaultButton.UnderlyingObject.ToString()]; } if (timeOutButton == null) { _timeOutButton = _formButtons[_buttonsPanel.Controls[0].Name]; timeOutButtonName = _buttonsPanel.Controls[0].Text; } else { _timeOutButton = _formButtons[timeOutButton.UnderlyingObject.ToString()]; timeOutButtonName = _buttonsPanel.Controls.Find(timeOutButton.UnderlyingObject.ToString(), false)[0].Text; } } using (Graphics g = _form.CreateGraphics()) { SizeF size = g.MeasureString(queryText, _queryLabel.Font); // С механизмом выравнивания элементов надо что-то делать, слишком много химии и волшебных чисел _form.Width = Math.Max((int)Math.Ceiling(size.Width) + _queryLabel.Padding.Horizontal, _formButtons.Count * (BUTTON_MIN_WIDTH + BUTTON_PADDING * 2) + (timeOut > 0 ? BUTTON_MIN_WIDTH / 2 + 60: BUTTON_MIN_WIDTH / 2)); } if (_timeOut != 0) { // Создадим два таймера, //один для срабатывания события окончания ожидания _timeoutTimer = new System.Threading.Timer(OnTimerElapsed, null, _timeOut * 1000, System.Threading.Timeout.Infinite); // и еще один, для посекундного оповещения об истичении времени _tickTimer = new System.Threading.Timer(OnTickTimer, timeOutButtonName, 0, 1000); } // Установим фокус на кнопку по умолчанию Control defaulButtonControl = _buttonsPanel.Controls.Find(_defaultButton.ToString(), false)[0]; defaulButtonControl.Focus(); defaulButtonControl.Select(); using (_timeoutTimer) _form.ShowDialog(null); }