Solved

Binding Object Data sources

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

776 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