Csharp/CSharp Tutorial/GUI Windows Forms/NumericUpDown
Содержание
Get value from NumericUpDown
<source lang="csharp">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); }
}</source>
NumericUpDown: set value, Minimum, Maximum, Increment, Decimal places, Readonly, TextAlign
<source lang="csharp">using System; using System.Drawing; using System.Windows.Forms; public class NumericUpDowns : Form {
NumericUpDown nupdwn; public NumericUpDowns() { Size = new Size(480,580); nupdwn = new NumericUpDown(); nupdwn.Parent = this; nupdwn.Location = new Point(50, 50); nupdwn.Size = new Size(60,20); nupdwn.Value = 1; nupdwn.Minimum = -10; nupdwn.Maximum = 10; nupdwn.Increment = .25m; // decimal nupdwn.DecimalPlaces = 2; nupdwn.ReadOnly = true; nupdwn.TextAlign = HorizontalAlignment.Right; nupdwn.ValueChanged += new EventHandler(nupdwn_OnValueChanged); } private void nupdwn_OnValueChanged(object sender, EventArgs e) { Console.WriteLine(nupdwn.Value); } static void Main() { Application.Run(new NumericUpDowns()); }
}</source>
NumericUpDown validated event
<source lang="csharp">using System; using System.ruponentModel; using System.Drawing; using System.Windows.Forms; using System.IO; using System.Text; class DialogValid : System.Windows.Forms.Form {
private Button okButton; private Button cancelButton; private NumericUpDown num; public decimal Num { get { return num.Value; } set { num.Value = value; } } void OnValidating(Object sender, CancelEventArgs e) { MessageBox.Show("NumericUpDown is validating"); } void OnValid(Object sender,EventArgs e) { MessageBox.Show("NumericUpDown is valid"); } public DialogValid() { Size = new Size(400,100); FormBorderStyle = FormBorderStyle.FixedDialog; Text = "Dialog test"; okButton = new Button(); okButton.DialogResult = DialogResult.OK; okButton.Location = new Point(20,28); okButton.Size = new Size(80,25); okButton.Text = "OK"; Controls.Add(okButton); cancelButton = new Button(); cancelButton.Location = new Point(300,28); cancelButton.Size = new Size(80,25); cancelButton.Text = "Cancel"; cancelButton.DialogResult = DialogResult.Cancel; Controls.Add(cancelButton); Label l = new Label(); l.Text = "NumericUpDown"; l.Location = new Point(20,5); l.Size = new Size(120,25); Controls.Add(l); num = new NumericUpDown(); num.Location = new Point(140,5); num.Size = new Size(80,25); num.Minimum = (decimal)10.0; num.Maximum = (decimal)100.0; num.Value = (decimal)10.0; num.Validating+=new CancelEventHandler(OnValidating); num.Validated+=new EventHandler(OnValid); Controls.Add(num); }
} public class NumericUpDownValidationEvent{
public static void Main() { DialogValid dlg = new DialogValid(); DialogResult r = dlg.ShowDialog(); Console.WriteLine(dlg.Num); }
}</source>
NumericUpDown validating event
<source lang="csharp">using System; using System.ruponentModel; using System.Drawing; using System.Windows.Forms; using System.IO; using System.Text; class DialogValid : System.Windows.Forms.Form {
private Button okButton; private Button cancelButton; private NumericUpDown num; public decimal Num { get { return num.Value; } set { num.Value = value; } } void OnValidating(Object sender, CancelEventArgs e) { MessageBox.Show("NumericUpDown is validating"); } void OnValid(Object sender,EventArgs e) { MessageBox.Show("NumericUpDown is valid"); } public DialogValid() { Size = new Size(400,100); FormBorderStyle = FormBorderStyle.FixedDialog; Text = "Dialog test"; okButton = new Button(); okButton.DialogResult = DialogResult.OK; okButton.Location = new Point(20,28); okButton.Size = new Size(80,25); okButton.Text = "OK"; Controls.Add(okButton); cancelButton = new Button(); cancelButton.Location = new Point(300,28); cancelButton.Size = new Size(80,25); cancelButton.Text = "Cancel"; cancelButton.DialogResult = DialogResult.Cancel; Controls.Add(cancelButton); Label l = new Label(); l.Text = "NumericUpDown"; l.Location = new Point(20,5); l.Size = new Size(120,25); Controls.Add(l); num = new NumericUpDown(); num.Location = new Point(140,5); num.Size = new Size(80,25); num.Minimum = (decimal)10.0; num.Maximum = (decimal)100.0; num.Value = (decimal)10.0; num.Validating+=new CancelEventHandler(OnValidating); num.Validated+=new EventHandler(OnValid); Controls.Add(num); }
} public class NumericUpDownValidationEvent{
public static void Main() { DialogValid dlg = new DialogValid(); DialogResult r = dlg.ShowDialog(); Console.WriteLine(dlg.Num); }
}</source>
NumericUpDown: value changed event
<source lang="csharp">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); }
}</source>
NumericUpDown Value changed event handler
<source lang="csharp">using System; using System.Drawing; using System.Windows.Forms; public class NumericUpDowns : Form {
NumericUpDown nupdwn; public NumericUpDowns() { Size = new Size(480,580); nupdwn = new NumericUpDown(); nupdwn.Parent = this; nupdwn.Location = new Point(50, 50); nupdwn.Size = new Size(60,20); nupdwn.Value = 1; nupdwn.Minimum = -10; nupdwn.Maximum = 10; nupdwn.Increment = .25m; // decimal nupdwn.DecimalPlaces = 2; nupdwn.ReadOnly = true; nupdwn.TextAlign = HorizontalAlignment.Right; nupdwn.ValueChanged += new EventHandler(nupdwn_OnValueChanged); } private void nupdwn_OnValueChanged(object sender, EventArgs e) { Console.WriteLine(nupdwn.Value); } static void Main() { Application.Run(new NumericUpDowns()); }
}</source>