Solved

Binding Object Data sources

Posted on 2006-10-25
2
179 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
  • 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

705 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now