Csharp/CSharp Tutorial/GUI Windows Forms/DomainUpDown

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

ColorUpDown

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
    public class MainForm : System.Windows.Forms.Form
    {
        private System.Windows.Forms.DomainUpDown knownColorUpDown;
    public MainForm()
    {
            this.knownColorUpDown = new System.Windows.Forms.DomainUpDown();
            this.SuspendLayout();
            // 
            // knownColorUpDown
            // 
            this.knownColorUpDown.Location = new System.Drawing.Point(112, 56);
            this.knownColorUpDown.Name = "knownColorUpDown";
            this.knownColorUpDown.Size = new System.Drawing.Size(152, 20);
            this.knownColorUpDown.TabIndex = 0;
            this.knownColorUpDown.SelectedItemChanged += new System.EventHandler(this.knownColorUpDown_SelectedItemChanged);
            // 
            // MainForm
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(376, 150);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.knownColorUpDown});
            this.Text = "ColorUpDown Example";
            this.ResumeLayout(false);

      string [] colorNames = Enum.GetNames(typeof(KnownColor));
      knownColorUpDown.Items.AddRange(colorNames);
      knownColorUpDown.SelectedIndex = 0;
    }
        static void Main() 
        {
            Application.Run(new MainForm());
        }
    private void knownColorUpDown_SelectedItemChanged(object sender, System.EventArgs e)
    {
      string currentColorName = (string)knownColorUpDown.SelectedItem;
      try
      {
        BackColor = Color.FromName(currentColorName);
      }
      catch(ArgumentException exception)
      {
        MessageBox.Show(exception.Message);
      }
    }
  }

DomainUpDown: selected item changed event

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class UpDownForm : System.Windows.Forms.Form
{
  private System.ruponentModel.Container components = null;
  private System.Windows.Forms.Label lblCurrSel;
  private System.Windows.Forms.Button btnGetSelections;
  private System.Windows.Forms.Label label2;
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.NumericUpDown numericUpDown;
  private System.Windows.Forms.DomainUpDown domainUpDown;
  public UpDownForm()
  {
    InitializeComponent();
    domainUpDown.SelectedIndex = 2;
  }
  protected override void Dispose( bool disposing )
  {
    if( disposing )
    {
      if (components != null) 
      {
        components.Dispose();
      }
    }
    base.Dispose( disposing );
  }
  private void InitializeComponent()
  {
    this.label1 = new System.Windows.Forms.Label();
    this.label2 = new System.Windows.Forms.Label();
    this.numericUpDown = new System.Windows.Forms.NumericUpDown();
    this.domainUpDown = new System.Windows.Forms.DomainUpDown();
    this.btnGetSelections = new System.Windows.Forms.Button();
    this.lblCurrSel = new System.Windows.Forms.Label();
    ((System.ruponentModel.ISupportInitialize)(this.numericUpDown)).BeginInit();
    this.SuspendLayout();
    // 
    // label1
    // 
    this.label1.Font = new System.Drawing.Font("Verdana", 12F);
    this.label1.Location = new System.Drawing.Point(8, 24);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(224, 32);
    this.label1.TabIndex = 2;
    this.label1.Text = "Domain UpDown Control";
    // 
    // label2
    // 
    this.label2.Font = new System.Drawing.Font("Verdana", 12F);
    this.label2.Location = new System.Drawing.Point(8, 80);
    this.label2.Name = "label2";
    this.label2.Size = new System.Drawing.Size(232, 32);
    this.label2.TabIndex = 3;
    this.label2.Text = "Numeric UpDown Control";
    // 
    // numericUpDown
    // 
    this.numericUpDown.Location = new System.Drawing.Point(264, 80);
    this.numericUpDown.Maximum = new System.Decimal(new int[] {
                                    5000,
                                    0,
                                    0,
                                    0});
    this.numericUpDown.Name = "numericUpDown";
    this.numericUpDown.Size = new System.Drawing.Size(168, 20);
    this.numericUpDown.TabIndex = 1;
    this.numericUpDown.ThousandsSeparator = true;
    this.numericUpDown.UpDownAlign = System.Windows.Forms.LeftRightAlignment.Left;
    this.numericUpDown.ValueChanged += new System.EventHandler(this.numericUpDown_ValueChanged);
    // 
    // domainUpDown
    // 
    this.domainUpDown.Items.Add("A");
    this.domainUpDown.Items.Add("B");
    this.domainUpDown.Items.Add("C");
    this.domainUpDown.Items.Add("D");
    this.domainUpDown.Location = new System.Drawing.Point(264, 24);
    this.domainUpDown.Name = "domainUpDown";
    this.domainUpDown.Size = new System.Drawing.Size(168, 20);
    this.domainUpDown.Sorted = true;
    this.domainUpDown.TabIndex = 0;
    this.domainUpDown.Text = "domainUpDown1";
    this.domainUpDown.Wrap = true;
    this.domainUpDown.SelectedItemChanged += new System.EventHandler(this.domainUpDown_SelectedItemChanged);
    // 
    // btnGetSelections
    // 
    this.btnGetSelections.Location = new System.Drawing.Point(16, 136);
    this.btnGetSelections.Name = "btnGetSelections";
    this.btnGetSelections.Size = new System.Drawing.Size(136, 24);
    this.btnGetSelections.TabIndex = 4;
    this.btnGetSelections.Text = "Get Current Selections";
    this.btnGetSelections.Click += new System.EventHandler(this.btnGetSelections_Click);
    // 
    // lblCurrSel
    // 
    this.lblCurrSel.BackColor = System.Drawing.Color.Linen;
    this.lblCurrSel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
    this.lblCurrSel.Location = new System.Drawing.Point(176, 120);
    this.lblCurrSel.Name = "lblCurrSel";
    this.lblCurrSel.Size = new System.Drawing.Size(256, 48);
    this.lblCurrSel.TabIndex = 5;
    // 
    // UpDownForm
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(448, 181);
    this.Controls.Add(this.lblCurrSel);
    this.Controls.Add(this.btnGetSelections);
    this.Controls.Add(this.label2);
    this.Controls.Add(this.label1);
    this.Controls.Add(this.numericUpDown);
    this.Controls.Add(this.domainUpDown);
    this.Name = "UpDownForm";
    this.Text = "Spin Controls";
    ((System.ruponentModel.ISupportInitialize)(this.numericUpDown)).EndInit();
    this.ResumeLayout(false);
  }
  [STAThread]
  static void Main() 
  {
    Application.Run(new UpDownForm());
  }
  protected void numericUpDown_ValueChanged (object sender, System.EventArgs e)
  {
    this.Text = "You changed the numeric value...";
  }
  protected void domainUpDown_SelectedItemChanged (object sender, System.EventArgs e)
  {
    this.Text = "You changed the string value...";
  }
  protected void btnGetSelections_Click (object sender, System.EventArgs e)
  {
    // Get info from updowns...
    lblCurrSel.Text = string.Format("String: {0}\nNumber: {1}", domainUpDown.Text, numericUpDown.Value);
  }
}

DomainUpDown selected value changed event

using System;
using System.Drawing;
using System.Windows.Forms;
public class DomainUpDowns : Form
{
  DomainUpDown dupdwn;
  public DomainUpDowns()
  {
    Size = new Size(480,580);
    dupdwn = new DomainUpDown();
    dupdwn.Parent = this;
    dupdwn.Location = new Point(50, 50);
    dupdwn.Size = new Size(150,dupdwn.PreferredHeight);
    dupdwn.ReadOnly = true;
    dupdwn.TextAlign = HorizontalAlignment.Center;
    dupdwn.UpDownAlign = LeftRightAlignment.Left;
    dupdwn.Wrap = true;
    dupdwn.SelectedItemChanged +=  new EventHandler(dupdwn_OnSelectedItemChanged);
    dupdwn.Items.Add(BorderStyle.Fixed3D);
    dupdwn.Items.Add(BorderStyle.FixedSingle);
    dupdwn.Items.Add(BorderStyle.None); 
    dupdwn.SelectedIndex = 0;    //  zero-based index
  }  
  private void dupdwn_OnSelectedItemChanged(object sender, EventArgs e)
  {
    Console.WriteLine(dupdwn.SelectedItem);
  }
  static void Main() 
  {
    Application.Run(new DomainUpDowns());
  }
}

Get value from DomainUpDomain

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class UpDownForm : System.Windows.Forms.Form
{
  private System.ruponentModel.Container components = null;
  private System.Windows.Forms.Label lblCurrSel;
  private System.Windows.Forms.Button btnGetSelections;
  private System.Windows.Forms.Label label2;
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.NumericUpDown numericUpDown;
  private System.Windows.Forms.DomainUpDown domainUpDown;
  public UpDownForm()
  {
    InitializeComponent();
    domainUpDown.SelectedIndex = 2;
  }
  protected override void Dispose( bool disposing )
  {
    if( disposing )
    {
      if (components != null) 
      {
        components.Dispose();
      }
    }
    base.Dispose( disposing );
  }
  private void InitializeComponent()
  {
    this.label1 = new System.Windows.Forms.Label();
    this.label2 = new System.Windows.Forms.Label();
    this.numericUpDown = new System.Windows.Forms.NumericUpDown();
    this.domainUpDown = new System.Windows.Forms.DomainUpDown();
    this.btnGetSelections = new System.Windows.Forms.Button();
    this.lblCurrSel = new System.Windows.Forms.Label();
    ((System.ruponentModel.ISupportInitialize)(this.numericUpDown)).BeginInit();
    this.SuspendLayout();
    // 
    // label1
    // 
    this.label1.Font = new System.Drawing.Font("Verdana", 12F);
    this.label1.Location = new System.Drawing.Point(8, 24);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(224, 32);
    this.label1.TabIndex = 2;
    this.label1.Text = "Domain UpDown Control";
    // 
    // label2
    // 
    this.label2.Font = new System.Drawing.Font("Verdana", 12F);
    this.label2.Location = new System.Drawing.Point(8, 80);
    this.label2.Name = "label2";
    this.label2.Size = new System.Drawing.Size(232, 32);
    this.label2.TabIndex = 3;
    this.label2.Text = "Numeric UpDown Control";
    // 
    // numericUpDown
    // 
    this.numericUpDown.Location = new System.Drawing.Point(264, 80);
    this.numericUpDown.Maximum = new System.Decimal(new int[] {
                                    5000,
                                    0,
                                    0,
                                    0});
    this.numericUpDown.Name = "numericUpDown";
    this.numericUpDown.Size = new System.Drawing.Size(168, 20);
    this.numericUpDown.TabIndex = 1;
    this.numericUpDown.ThousandsSeparator = true;
    this.numericUpDown.UpDownAlign = System.Windows.Forms.LeftRightAlignment.Left;
    this.numericUpDown.ValueChanged += new System.EventHandler(this.numericUpDown_ValueChanged);
    // 
    // domainUpDown
    // 
    this.domainUpDown.Items.Add("A");
    this.domainUpDown.Items.Add("B");
    this.domainUpDown.Items.Add("C");
    this.domainUpDown.Items.Add("D");
    this.domainUpDown.Location = new System.Drawing.Point(264, 24);
    this.domainUpDown.Name = "domainUpDown";
    this.domainUpDown.Size = new System.Drawing.Size(168, 20);
    this.domainUpDown.Sorted = true;
    this.domainUpDown.TabIndex = 0;
    this.domainUpDown.Text = "domainUpDown1";
    this.domainUpDown.Wrap = true;
    this.domainUpDown.SelectedItemChanged += new System.EventHandler(this.domainUpDown_SelectedItemChanged);
    // 
    // btnGetSelections
    // 
    this.btnGetSelections.Location = new System.Drawing.Point(16, 136);
    this.btnGetSelections.Name = "btnGetSelections";
    this.btnGetSelections.Size = new System.Drawing.Size(136, 24);
    this.btnGetSelections.TabIndex = 4;
    this.btnGetSelections.Text = "Get Current Selections";
    this.btnGetSelections.Click += new System.EventHandler(this.btnGetSelections_Click);
    // 
    // lblCurrSel
    // 
    this.lblCurrSel.BackColor = System.Drawing.Color.Linen;
    this.lblCurrSel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
    this.lblCurrSel.Location = new System.Drawing.Point(176, 120);
    this.lblCurrSel.Name = "lblCurrSel";
    this.lblCurrSel.Size = new System.Drawing.Size(256, 48);
    this.lblCurrSel.TabIndex = 5;
    // 
    // UpDownForm
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(448, 181);
    this.Controls.Add(this.lblCurrSel);
    this.Controls.Add(this.btnGetSelections);
    this.Controls.Add(this.label2);
    this.Controls.Add(this.label1);
    this.Controls.Add(this.numericUpDown);
    this.Controls.Add(this.domainUpDown);
    this.Name = "UpDownForm";
    this.Text = "Spin Controls";
    ((System.ruponentModel.ISupportInitialize)(this.numericUpDown)).EndInit();
    this.ResumeLayout(false);
  }
  [STAThread]
  static void Main() 
  {
    Application.Run(new UpDownForm());
  }
  protected void numericUpDown_ValueChanged (object sender, System.EventArgs e)
  {
    this.Text = "You changed the numeric value...";
  }
  protected void domainUpDown_SelectedItemChanged (object sender, System.EventArgs e)
  {
    this.Text = "You changed the string value...";
  }
  protected void btnGetSelections_Click (object sender, System.EventArgs e)
  {
    // Get info from updowns...
    lblCurrSel.Text = string.Format("String: {0}\nNumber: {1}", domainUpDown.Text, numericUpDown.Value);
  }
}

Remove selected item from DomainUpDown

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class Form1 : System.Windows.Forms.Form {
    private System.Windows.Forms.Button button2;
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.DomainUpDown UPDOWN_DOMAIN;
    private System.Windows.Forms.Label label1;
    public Form1() {
        this.button2 = new System.Windows.Forms.Button();
        this.textBox1 = new System.Windows.Forms.TextBox();
        this.button1 = new System.Windows.Forms.Button();
        this.UPDOWN_DOMAIN = new System.Windows.Forms.DomainUpDown();
        this.label1 = new System.Windows.Forms.Label();
        this.SuspendLayout();
        // 
        this.button2.Location = new System.Drawing.Point(136, 80);
        this.button2.Name = "button2";
        this.button2.TabIndex = 8;
        this.button2.Text = "Add Item";
        // 
        this.textBox1.Location = new System.Drawing.Point(24, 80);
        this.textBox1.Name = "textBox1";
        this.textBox1.TabIndex = 7;
        this.textBox1.Text = "";
        // 
        this.button1.Location = new System.Drawing.Point(264, 40);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(64, 23);
        this.button1.TabIndex = 6;
        this.button1.Text = "Remove";
        // 
        this.UPDOWN_DOMAIN.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(192)), ((System.Byte)(192)));
        this.UPDOWN_DOMAIN.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
        this.UPDOWN_DOMAIN.ForeColor = System.Drawing.SystemColors.HotTrack;
        this.UPDOWN_DOMAIN.Location = new System.Drawing.Point(24, 40);
        this.UPDOWN_DOMAIN.Name = "UPDOWN_DOMAIN";
        this.UPDOWN_DOMAIN.Size = new System.Drawing.Size(232, 26);
        this.UPDOWN_DOMAIN.Sorted = true;
        this.UPDOWN_DOMAIN.TabIndex = 5;
        this.UPDOWN_DOMAIN.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
        this.UPDOWN_DOMAIN.UpDownAlign = System.Windows.Forms.LeftRightAlignment.Left;
        this.UPDOWN_DOMAIN.Wrap = true;
        // 
        // label1
        // 
        this.label1.Location = new System.Drawing.Point(24, 16);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(136, 23);
        this.label1.TabIndex = 9;
        this.label1.Text = "UpDownDomain Control";
        // 
        // Form1
        // 
        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(344, 117);
        this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                      this.label1,
                                                                      this.UPDOWN_DOMAIN,
                                                                      this.button1,
                                                                      this.button2,
                                                                      this.textBox1});
        this.Text = "UpDownDomain Control";
        this.Load += new System.EventHandler(this.Form1_Load);
        this.ResumeLayout(false);
    }
    [STAThread]
    static void Main() {
        Application.Run(new Form1());
    }
    private void Form1_Load(object sender, System.EventArgs e) {
        UPDOWN_DOMAIN.Items.Add("A");
        UPDOWN_DOMAIN.Items.Add("B");
        UPDOWN_DOMAIN.Items.Add("C");
        UPDOWN_DOMAIN.Items.Add("D");
        UPDOWN_DOMAIN.Items.Add("E");
        UPDOWN_DOMAIN.Items.Add("F");
        UPDOWN_DOMAIN.Items.Add("G");
        UPDOWN_DOMAIN.Items.Add("H");
        UPDOWN_DOMAIN.Items.Add("I");
        UPDOWN_DOMAIN.Items.Add("J");
        UPDOWN_DOMAIN.Items.Add("K");
        UPDOWN_DOMAIN.Items.Add("L");
        UPDOWN_DOMAIN.Items.Add("M");
        UPDOWN_DOMAIN.Items.Add("N");
        UPDOWN_DOMAIN.Items.Add("O");
        UPDOWN_DOMAIN.Items.Add("P");
    }
    private void button1_Click(object sender, System.EventArgs e) {
        int nItemSel = UPDOWN_DOMAIN.SelectedIndex;
        if (nItemSel >= 0) {
            UPDOWN_DOMAIN.Items.RemoveAt(nItemSel);
            UPDOWN_DOMAIN.Update();
            UPDOWN_DOMAIN.Text = "";
        }
    }
    private void button2_Click(object sender, System.EventArgs e) {
        if (textBox1.Text == "") {
            MessageBox.Show("Enter a string to add");
            return;
        }
        UPDOWN_DOMAIN.Items.Add(textBox1.Text);
        textBox1.Text = "";
    }
}

Subclass DomainUpDown

using System;
using System.Windows.Forms;
public class MyUpDownControl : System.Windows.Forms.DomainUpDown {
    private int currentPos = 0;
    private string DisplayText = "";
    public MyUpDownControl() {
        Items.Add("FRANCE");
        Items.Add("ITALY");
        Items.Add("USA");
        Items.Add("UK");
        Items.Add("AUSTRALIA");
        Items.Add("INDIA");
        Items.Add("ZAMBIA");
        Items.Add("MALASYIA");
    }
    public override void DownButton() {
        currentPos++;
        if (currentPos >= Items.Count)
            currentPos = 0;
        UpdateEditText();
    }
    public override void UpButton() {
        currentPos--;
        if (currentPos < 0) currentPos = Items.Count - 1;
        UpdateEditText();
    }
    protected override void UpdateEditText() {
        DisplayText = (string)this.Items[currentPos];
        this.Text = DisplayText;
    }
    public void Sort() {
        if (this.Sorted)
            this.Sorted = false;
        else
            this.Sorted = true;
        if (this.Sorted)
            this.Sort();
        UpdateEditText();
    }
}
public class Form1 : System.Windows.Forms.Form {
    private System.ruponentModel.Container components = null;
    private System.Windows.Forms.Label label1;
    private MyUpDownControl mDC = null;
    public Form1() {
        this.mDC = new MyUpDownControl();
        this.label1 = new System.Windows.Forms.Label();
        this.SuspendLayout();
        this.mDC.Items.Add("FRANCE");
        this.mDC.Items.Add("ITALY");
        this.mDC.Items.Add("USA");
        this.mDC.Items.Add("UK");
        this.mDC.Items.Add("AUSTRALIA");
        this.mDC.Items.Add("INDIA");
        this.mDC.Items.Add("ZAMBIA");
        this.mDC.Items.Add("MALASYIA");
        this.mDC.Location = new System.Drawing.Point(40, 40);
        this.label1.Location = new System.Drawing.Point(16, 16);
        this.label1.Size = new System.Drawing.Size(200, 16);
        this.label1.Text = "Derived DomainUpDown Controller";
        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(248, 85);
        this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.label1,
                                      this.mDC});
        this.Text = "My Domain Controller";
        this.ResumeLayout(false);
    }
    [STAThread]
    static void Main() {
        Application.Run(new Form1());
    }
}

TextAlign and UpDownAlign

using System;
using System.Drawing;
using System.Windows.Forms;
public class DomainUpDowns : Form
{
  DomainUpDown dupdwn;
  public DomainUpDowns()
  {
    Size = new Size(480,580);
    dupdwn = new DomainUpDown();
    dupdwn.Parent = this;
    dupdwn.Location = new Point(50, 50);
    dupdwn.Size = new Size(150,dupdwn.PreferredHeight);
    dupdwn.ReadOnly = true;
    dupdwn.TextAlign = HorizontalAlignment.Center;
    dupdwn.UpDownAlign = LeftRightAlignment.Left;
    dupdwn.Wrap = true;
    dupdwn.SelectedItemChanged +=  new EventHandler(dupdwn_OnSelectedItemChanged);
    dupdwn.Items.Add(BorderStyle.Fixed3D);
    dupdwn.Items.Add(BorderStyle.FixedSingle);
    dupdwn.Items.Add(BorderStyle.None); 
    dupdwn.SelectedIndex = 0;    //  zero-based index
  }  
  private void dupdwn_OnSelectedItemChanged(object sender, EventArgs e)
  {
    Console.WriteLine(dupdwn.SelectedItem);
  }
  static void Main() 
  {
    Application.Run(new DomainUpDowns());
  }
}