Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3308772
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:33:41+00:00 2026-05-17T21:33:41+00:00

public partial class Form1 : Form { MyClass myClass = new MyClass(one, two); public

  • 0
public partial class Form1 : Form
{
    MyClass myClass = new MyClass("one", "two");

    public Form1()
    {
        InitializeComponent();
        textBox1.DataBindings.Add("Text", myClass, "Text1", false, DataSourceUpdateMode.Never);
        textBox2.DataBindings.Add("Text", myClass, "Text2", false, DataSourceUpdateMode.Never);
    }

    private void saveButton_Click(object sender, EventArgs e)
    {
        myClass.Text1 = textBox1.Text;
        myClass.Text2 = textBox2.Text;
        //textBox1.DataBindings["Text"].WriteValue();
        //textBox2.DataBindings["Text"].WriteValue();
    }
}

public class MyClass : INotifyPropertyChanged
{
    private string _Text1;
    private string _Text2;

    public event PropertyChangedEventHandler PropertyChanged;

    public string Text1
    {
        get { return _Text1; }
        set { _Text1 = value; OnPropertyChanged(new PropertyChangedEventArgs("Text1")); }
    }

    public string Text2
    {
        get { return _Text2; }
        set { _Text2 = value; OnPropertyChanged(new PropertyChangedEventArgs("Text2")); }
    }

    public MyClass(string text1, string text2)
    {
        Text1 = text1;
        Text2 = text2;
    }

    protected void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        if (PropertyChanged != null) PropertyChanged(this, e);
    }
}

I think is pretty clear what I’m trying to achieve. I want my form to save the changes made in my two TextBoxes to myClass. But whenever I press the save button after editing both text boxes, and saveButton_Click is invoked, the second textBox2‘s Text goes back to the original text (“two”). I tried using Binding‘s WriteValue function but the same thing happens. Using .net 4.0.

Edit Thanks for your answers, but I don’t need workarounds. I can find them myself. I just need to understand a little bit better how binding works. I would like to understand why is this happening?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T21:33:41+00:00Added an answer on May 17, 2026 at 9:33 pm

    Apparently, updating any value on the data source will cause all bindings to be updated. This explains the behavior (setting myClass.Text1 causes textBox2 to be updated with the current value of myClass.Text2). Unfortunately, the few posts I was able to find pretty much just said, “that’s how it works”.

    One way to handle this is to create a BindingSource, set BindingSource.DataSource = myClass, and then bind your TextBoxes to the BindingSource.

    BindingSource raises ListChanged events if the underlying data source is a list and items are added, removed, etc., or if the DataSource properties change. You can suppress these events by setting BindingSource.RaiseListChangedEvents to false, which would let you set multiple properties on myClass without data-binding updating the bound controls.

    public partial class Form1 : Form
    {
        MyClass myClass = new MyClass("one", "two");
        BindingSource bindingSource = new BindingSource();
    
        public Form1()
        {
            InitializeComponent();
    
            bindingSource.DataSource = myClass;
    
            textBox1.DataBindings.Add("Text", bindingSource, "Text1", true, DataSourceUpdateMode.Never);
            textBox2.DataBindings.Add("Text", bindingSource, "Text2", true, DataSourceUpdateMode.Never);                
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            bindingSource.RaiseListChangedEvents = false;
            myClass.Text1 = textBox1.Text;
            myClass.Text2 = textBox2.Text;
            bindingSource.RaiseListChangedEvents = true;
        }
    }
    

    HTH

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void
namespace myApplication { public partial class Form1 : Form { public Form1() { InitializeComponent();
public partial class Form1 : Form { bool AfterDocumentCompleted = false; int steps =
public partial class Form1 : Form What does the partial in this declaration mean?
partial class Form1 { //hidden private void InitializeComponent() { this.picture = new System.Windows.Forms.PictureBox(); //hidden
You should be able to create a generic form: public partial class MyGenericForm<T> :
The following code is the simplified version of my problem: public partial class Form1
I have a control that is created like so: public partial class MYControl :
I've got the following Generic usercontrol declared: public partial class MessageBase<T> : UserControl {
In Silverlight I noticed that the code-behind Page class inherits from UserControl: public partial

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.