A dialog by user defined property

image_pdfimage_print
   
 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

class MainClass {
    public static void Main() {
        Phone frm = new Phone();
        while (true) {
            frm.ShowDialog();
            if (frm.DialogResult == DialogResult.OK) {
                Console.WriteLine(frm.PhoneNumber);
                if (frm.PhoneNumber.Length == 8 | frm.PhoneNumber.Length == 12) {
                    break;
                } else {
                    MessageBox.Show("Phone number was not formatted correctly. Please correct entry.");
                }
            } else if (frm.DialogResult == DialogResult.Cancel) {
                Console.WriteLine("Form was canceled.");
                break;
            }
        }
        frm.Close();
    }
}
class Phone : Form {
    private System.Windows.Forms.TextBox textBox1  = new System.Windows.Forms.TextBox();
    private System.Windows.Forms.Label label1 = new System.Windows.Forms.Label() ;
    private System.Windows.Forms.Button btnOK = new System.Windows.Forms.Button();
    private System.Windows.Forms.Button btnCancel = new System.Windows.Forms.Button();

    public Phone() {
        this.SuspendLayout();

        this.textBox1.Location = new System.Drawing.Point(122, 21);
        this.textBox1.Margin = new System.Windows.Forms.Padding(1, 3, 3, 3);
        this.textBox1.Size = new System.Drawing.Size(115, 20);

        this.label1.AutoSize = true;
        this.label1.Location = new System.Drawing.Point(10, 26);
        this.label1.Margin = new System.Windows.Forms.Padding(3, 3, 1, 3);
        this.label1.Size = new System.Drawing.Size(110, 14);
        this.label1.Text = "Enter phone number:";
        this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;

        this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK;
        this.btnOK.Location = new System.Drawing.Point(36, 65);
        this.btnOK.Text = "OK";

        this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        this.btnCancel.Location = new System.Drawing.Point(132, 65);
        this.btnCancel.Text = "Cancel";
        this.ClientSize = new System.Drawing.Size(270, 107);
        this.Controls.Add(this.btnCancel);
        this.Controls.Add(this.btnOK);
        this.Controls.Add(this.label1);
        this.Controls.Add(this.textBox1);
        this.ResumeLayout(false);
        this.PerformLayout();
        btnOK.DialogResult = DialogResult.OK;
        btnCancel.DialogResult = DialogResult.Cancel;
    }
    public string PhoneNumber {
        get { return textBox1.Text; }
        set { textBox1.Text = value; }
    }
}