none
bindingsource.currentchanged. Is there and event before that? RRS feed

  • Question

  • I need to validate my object before the user moves to the next one. However when I catch currentchanged it already has moved. I am currently bound to a list(of T) so I don't have anything to catch in there like I can with a datagrid.

     

     

    Blizzardice

     

    Wednesday, December 26, 2007 5:15 PM

Answers

  • I would validate my data before I let the user move to the next record.
    Wednesday, December 26, 2007 5:50 PM
  •  

    Hi Blizzardice,

    You can also handle the CurrentItemChanged event of the BindindSource. This event occurs when a property value of the Current property has changed. Try something like the following:

    Code Block

    namespace BindingSourceP

    {

        public partial class Form3 : Form

        {

            public Form3()

            {

                InitializeComponent();

            }

     

            BindingSource bs = new BindingSource();

     

            private void Form3_Load(object sender, EventArgs e)

            {

                List<BusinessObject> list = new List<BusinessObject>();

                for (int i = 1; i < 10; i++)

                {

                    list.Add(new BusinessObject(i, "name" + i));

                }

                bs.DataSource = list;

                bs.CurrentItemChanged += new EventHandler(bs_CurrentItemChanged);

                this.textBox1.DataBindings.Add("Text", bs, "Id");

                this.textBox2.DataBindings.Add("Text", bs, "Name");

            }

     

            void bs_CurrentItemChanged(object sender, EventArgs e)

            {

                string err = (this.bs.Current as BusinessObject).ValidateObject();

                if (err != "")

                    MessageBox.Show(err);

            }    

     

            private void btnMove_Click(object sender, EventArgs e)

            {

                bs.MoveNext();

            }

        }

     

        class BusinessObject

        {

            public BusinessObject() { }

     

            public BusinessObject(int i, string str)

            {

                id = i;

                name = str;

            }

            int id;

            public int Id

            {

                get { return id; }

                set { id = value; }

            }

     

            string name;

            public string Name

            {

                get { return name; }

                set { name = value; }

            }

     

            public string ValidateObject()

            {

                string error = "";

                if( id >100)

                    error += "Id should not be more than 100!\n";

                if (name.Length > 20)

                    error += "The length of Name should no be more than 20";

                return error;

            }

        }

    }

     

    Hope this helps.
    Best regards.
    Rong-Chun Zhang

    Friday, December 28, 2007 3:50 AM

All replies

  • I would validate my data before I let the user move to the next record.
    Wednesday, December 26, 2007 5:50 PM
  •  

    Hi Blizzardice,

    You can also handle the CurrentItemChanged event of the BindindSource. This event occurs when a property value of the Current property has changed. Try something like the following:

    Code Block

    namespace BindingSourceP

    {

        public partial class Form3 : Form

        {

            public Form3()

            {

                InitializeComponent();

            }

     

            BindingSource bs = new BindingSource();

     

            private void Form3_Load(object sender, EventArgs e)

            {

                List<BusinessObject> list = new List<BusinessObject>();

                for (int i = 1; i < 10; i++)

                {

                    list.Add(new BusinessObject(i, "name" + i));

                }

                bs.DataSource = list;

                bs.CurrentItemChanged += new EventHandler(bs_CurrentItemChanged);

                this.textBox1.DataBindings.Add("Text", bs, "Id");

                this.textBox2.DataBindings.Add("Text", bs, "Name");

            }

     

            void bs_CurrentItemChanged(object sender, EventArgs e)

            {

                string err = (this.bs.Current as BusinessObject).ValidateObject();

                if (err != "")

                    MessageBox.Show(err);

            }    

     

            private void btnMove_Click(object sender, EventArgs e)

            {

                bs.MoveNext();

            }

        }

     

        class BusinessObject

        {

            public BusinessObject() { }

     

            public BusinessObject(int i, string str)

            {

                id = i;

                name = str;

            }

            int id;

            public int Id

            {

                get { return id; }

                set { id = value; }

            }

     

            string name;

            public string Name

            {

                get { return name; }

                set { name = value; }

            }

     

            public string ValidateObject()

            {

                string error = "";

                if( id >100)

                    error += "Id should not be more than 100!\n";

                if (name.Length > 20)

                    error += "The length of Name should no be more than 20";

                return error;

            }

        }

    }

     

    Hope this helps.
    Best regards.
    Rong-Chun Zhang

    Friday, December 28, 2007 3:50 AM