this.textBox.ReadOnly = true;
this.textBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBox.Size = new System.Drawing.Size(259, 208);
this.textBox.TabIndex = 0;
this.textBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox_KeyPress);
//
// okButton
//
this.okButton.DialogResult = System.Windows.Forms.DialogResult.OK;
this.okButton.Location = new System.Drawing.Point(197, 227);
this.okButton.Name = "okButton";
this.okButton.Size = new System.Drawing.Size(75, 23);
this.okButton.TabIndex = 1;
this.okButton.Text = "ОК";
this.okButton.UseVisualStyleBackColor = true;
//
// ErrorMessageForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.okButton);
this.Controls.Add(this.textBox);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "ErrorMessageForm";
this.Text = "Произошла ошибка";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox textBox;
private System.Windows.Forms.Button okButton;
}
}
using System.Windows.Forms;
using ColoringGraphProblem.Views.Dialogs;
namespace ColoringGraphProblem.Views.CommonForms
{
public class ErrorMessageModel
{
private readonly ErrorMessageForm form;
public ErrorMessageModel()
{
form = new ErrorMessageForm();
}
public void ShowMessage(IWin32Window owner, string message)
{
form.SetErrorMessage(message);
form.ShowDialog(owner);
}
}
}
using ColoringGraphProblem.Exceptions;
using ColoringGraphProblem.Views.Dialogs;
namespace ColoringGraphProblem.Views.CommonForms
{
public partial class InputIntegerForm : DialogForm
{
//модель
private readonly InputIntegerModel model;
//Загрузить заголовок формы
public void SetFormCaption(string text)
{
Text = text;
}
//Загрузить текст сообщения
public void SetInputMessage(string text)
{
inputMessageLabel.Text = text;
}
//
public int GetInputInteger()
{
int res;
if (!int.TryParse(intTextBox.Text, out res))
throw new UserException("Выражение '{0}' не является целым числом.", intTextBox.Text);
return res;
}
public InputIntegerForm(InputIntegerModel model)
{
this.model = model;
InitializeComponent();
MakeUnsizeable();
}
private void intTextBox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (e.KeyChar.CompareTo('\r') == 0)
okButton.PerformClick();
}
}
}
namespace ColoringGraphProblem.Views.CommonForms
{
partial class InputIntegerForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.intTextBox = new System.Windows.Forms.TextBox();
this.okButton = new System.Windows.Forms.Button();
this.cancelButton = new System.Windows.Forms.Button();
this.inputMessageLabel = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// intTextBox
//
this.intTextBox.Location = new System.Drawing.Point(9, 24);
this.intTextBox.Name = "intTextBox";
this.intTextBox.Size = new System.Drawing.Size(204, 20);
this.intTextBox.TabIndex = 0;
this.intTextBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.intTextBox_KeyPress);
//
// okButton
//
this.okButton.DialogResult = System.Windows.Forms.DialogResult.OK;
this.okButton.Location = new System.Drawing.Point(9, 50);
this.okButton.Name = "okButton";
this.okButton.Size = new System.Drawing.Size(106, 23);
this.okButton.TabIndex = 1;
this.okButton.Text = "ОК";
this.okButton.UseVisualStyleBackColor = true;
//
// cancelButton
//
this.cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.cancelButton.Location = new System.Drawing.Point(121, 50);
this.cancelButton.Name = "cancelButton";
this.cancelButton.Size = new System.Drawing.Size(92, 23);
this.cancelButton.TabIndex = 2;
this.cancelButton.Text = "Отмена";
this.cancelButton.UseVisualStyleBackColor = true;
//
// inputMessageLabel
//
this.inputMessageLabel.AutoSize = true;
this.inputMessageLabel.Location = new System.Drawing.Point(6, 8);
this.inputMessageLabel.Name = "inputMessageLabel";
this.inputMessageLabel.Size = new System.Drawing.Size(207, 13);
this.inputMessageLabel.TabIndex = 3;
this.inputMessageLabel.Text = "Введите что-то очень важное и нужное:";
//
// InputIntegerForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(219, 78);
this.Controls.Add(this.inputMessageLabel);
this.Controls.Add(this.cancelButton);
this.Controls.Add(this.okButton);
this.Controls.Add(this.intTextBox);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "InputIntegerForm";
this.Text = "caption";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox intTextBox;
private System.Windows.Forms.Button okButton;
private System.Windows.Forms.Button cancelButton;
private System.Windows.Forms.Label inputMessageLabel;
}
}
using System.Windows.Forms;
using ColoringGraphProblem.Views.CommonForms;
namespace ColoringGraphProblem.Views.Dialogs
{
public class InputIntegerModel
{
private readonly InputIntegerForm form;
public InputIntegerModel(string formCaption, string inputMessage)
{
form = new InputIntegerForm(this);
form.SetFormCaption(formCaption);
form.SetInputMessage(inputMessage);
}
//есть 3 варианта возращаемого результата: число - пользователь ввел что-то разумное, null - пользователь нажал отмена, UserException - пользователь сделал не так
public int? GetValue(IWin32Window owner)
{
if (form.ShowDialog(owner) == DialogResult.OK) return form.GetInputInteger();
return null;
}
}
}