Solved

Binding Object Data sources

Posted on 2006-10-25
2
183 Views
Last Modified: 2011-08-01
I have the following as a class library:

namespace ClassLibrary1
{
    public class Class1
    {
        private int infield1;

        public int Infield1
        {
            get { return infield1; }
            set { infield1 = value; }
        }
        private int infield2;

        public int Infield2
        {
            get { return infield2; }
            set { infield2 = value; }
        }
        private int outfield1;

        public int Outfield1
        {
            get { return outfield1; }
            set { outfield1 = value; }
        }

        public void Calculate()
        {
            outfield1 = infield1 + infield2;
        }
    }
}
// --------------------  End of ClassLibrary1

I then create an Object Data Source which creates a Class1 Data Source.  I right-click on the data source and choose Details. Then I drag the data source onto the form and it creates a form with the Object bound (class1BindingSource).   I added a button to the form to process the calculation.

Here is the code to the form:

    public partial class Form1 : Form
    {
        Class1 calc = new Class1();

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {            
            calc.Calculate();
            class1BindingSource.DataSource = calc;
        }

        private void infield1TextBox_TextChanged(object sender, EventArgs e)
        {
            calc.Infield1 = Int32.Parse(infield1TextBox.Text);
        }

        private void infield2TextBox_TextChanged(object sender, EventArgs e)
        {
            calc.Infield2 = Int32.Parse(infield2TextBox.Text);
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
// End of form code

This code successfully takes the two input fields, adds them up and displays the result in the output field.

Here is the question: can I somehow bind the input (infield1TextBox, infield2TextBox) at design time so that I don't have to set each TextChanged events?  I have many fields and this seems like more work than needed if it is possible.

Thanks.
0
Comment
Question by:techhound
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 17807700
If you select the textbox and look at the Properties window you will see an option called "(DataBindings)". Expand that and you will be able to bind the Text property to the required property of your object.

To go a step further in your databinding you can make your object notify the Form when a property changes. If you do that then you only need to set the DataSource once when the form loads rather than having to reset it everytime you call Calc().

namespace ClassLibrary1
{
    public class Class1: INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged

        private void NotifyPropertyChanged(string info)
        {
             if (PropertyChanged != null)
                 PropertyChanged(this, new PropertyChangedEventArgs(info));
        }

        private int infield1;
        public int Infield1
        {
            get { return infield1; }
            set { infield1 = value; }
        }

        private int infield2;
        public int Infield2
        {
            get { return infield2; }
            set { infield2 = value; }
        }

        private int outfield1;
        public int Outfield1
        {
            get { return outfield1; }
            set
            {
                outfield1 = value;
                NotifyPropertyChanged("Outfield1");
            }
        }

        public void Calculate()
        {
            this.Outfield1 = infield1 + infield2;
        }
    }
}

Then you can change your form to:

    public partial class Form1 : Form
    {
        Class1 calc = new Class1();

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {            
            calc.Calculate();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            class1BindingSource.DataSource = calc;
        }
    }

If you bind another Textbox to Outfield1 then the results of the calculation will automatically be displayed.
0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 500 total points
ID: 17807706
You may need to add a reference to the System.ComponentModel assembly to your Class Library if it is not already there.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question