Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Binding Object Data sources

Posted on 2006-10-25
2
Medium Priority
?
188 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 2000 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 2000 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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

715 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