Solved

Binding Object Data sources

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

912 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

22 Experts available now in Live!

Get 1:1 Help Now