Answered NumericUpDown and databinding

  • Tuesday, April 29, 2008 5:50 PM
     
     
    I think this is an issue with the numericUpDown control. I am using .net3.0.

    Here is the problem. If you run the code below you will have a form with numericUpDown and a text box. Put a break point on the setter for JobNumber. Run the app.

    If you click in the numericUpDown and type a value and hit tab, the value does NOT get pushed down to the object that the numericUpDown is bound too. However if you click in the numericUpDown and hit tab again it will push the value to the object.

    You can repeat as many times as you want. If you go back to the numericUpDown a 3rd time and type a value and tab it wont work until you tab off a 2nd time.

    Also if you click into the numericUpDown and type value and click on the textBox to leave focus the value will be pushed to the business object.

    I know the databinding is set up kind of weird. I found thing "bug" in code where databinding is set up correctly.


    The databinding I have set up for TextBoxes and other controls work fine. Everything except numericUpDown boxes...

    On a side note, the code base that this bug was found is found has the numericUpDown subclassed. it is possible to put code into our custom control.

    Thanks
    Tony.

    Code:

    Code Snippet

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

    namespace WindowsApplication3
    {
        public class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();


                myClassBindingSource.DataSource = new MyClass();
            }

            private System.ComponentModel.IContainer components = null;
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }

            #region Windows Form Designer generated code

            private void InitializeComponent()
            {
                this.components = new System.ComponentModel.Container();
                this.numericUpDown1 = new System.Windows.Forms.NumericUpDown();
                this.textBox1 = new System.Windows.Forms.TextBox();
                this.myClassBindingSource = new System.Windows.Forms.BindingSource(this.components);
                ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).BeginInit();
                ((System.ComponentModel.ISupportInitialize)(this.myClassBindingSource)).BeginInit();
                this.SuspendLayout();
                //
                // numericUpDown1
                //
                this.numericUpDown1.DataBindings.Add(new System.Windows.Forms.Binding("Value", this.myClassBindingSource, "JobNumber", true));
                this.numericUpDown1.Location = new System.Drawing.Point(102, 90);
                this.numericUpDown1.Name = "numericUpDown1";
                this.numericUpDown1.Size = new System.Drawing.Size(120, 20);
                this.numericUpDown1.TabIndex = 0;
                //
                // textBox1
                //
                this.textBox1.Location = new System.Drawing.Point(102, 142);
                this.textBox1.Name = "textBox1";
                this.textBox1.Size = new System.Drawing.Size(100, 20);
                this.textBox1.TabIndex = 1;
                //
                // myClassBindingSource
                //
                this.myClassBindingSource.DataSource = typeof(WindowsApplication3.MyClass);
                //
                // Form1
                //
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(292, 266);
                this.Controls.Add(this.textBox1);
                this.Controls.Add(this.numericUpDown1);
                this.Name = "Form1";
                this.Text = "Form1";
                ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).EndInit();
                ((System.ComponentModel.ISupportInitialize)(this.myClassBindingSource)).EndInit();
                this.ResumeLayout(false);
                this.PerformLayout();

            }

            #endregion

            private System.Windows.Forms.NumericUpDown numericUpDown1;
            private System.Windows.Forms.TextBox textBox1;
            private System.Windows.Forms.BindingSource myClassBindingSource;  
        }

        public class MyClass
        {
            int? _jobNumber;
            public int? JobNumber
            {
                get { return _jobNumber; }
                set { _jobNumber = value; }
            }
        }
    }

Answers

  • Tuesday, April 29, 2008 8:59 PM
     
     Answered
    Fixed my own issue... Learn something new every day.

    this.numericUpDown.DataBindings.Add("Value", Me._InventoryItems, "UnitCost", True, DataSourceUpdateMode.OnPropertyChanged)

All Replies

  • Tuesday, April 29, 2008 8:59 PM
     
     Answered
    Fixed my own issue... Learn something new every day.

    this.numericUpDown.DataBindings.Add("Value", Me._InventoryItems, "UnitCost", True, DataSourceUpdateMode.OnPropertyChanged)
  • Tuesday, June 08, 2010 9:25 AM
     
     
    Thanks very much for sharing this!