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 750745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:35:11+00:00 2026-05-14T14:35:11+00:00

The following seems to be a relatively common pattern (to me, not to the

  • 0

The following seems to be a relatively common pattern (to me, not to the community at large) to bind a string variable to the contents of a TextBox.

class MyBackEndClass
{
    public event EventHandler DataChanged;
    string _Data;
    public string Data
    {
        get { return _Data; }
        set
        {
            _Data = value;
            //Fire the DataChanged event
        }
    }
}

class SomeForm : // Form stuff
{
    MyBackEndClass mbe;
    TextBox someTextBox;
    SomeForm() 
    {
        someTextBox.TextChanged += HandleTextBox();
        mbe.DataChanged += HandleData();
    }

    void HandleTextBox(Object sender, EventArgs e)
    {
        mbe.Data = ((TextBox)sender).Text;
    }

    void HandleData(Object sender, EventArgs e)
    {
        someTextBox.Text = ((MyBackEndClass) sender).Data;
    }
}

The problem is that changing the TextBox fires the changes the data value in the backend, which causes the textbox to change, etc. That runs forever.

Is there a better design pattern (other than resorting to a nasty boolean flag) that handles this case correctly?

EDIT: To be clear, in the real design the backend class is used to synchronize changes between multiple forms. Therefore I can’t just use the SomeTextBox.Text property directly.

Billy3

  • 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-14T14:35:11+00:00Added an answer on May 14, 2026 at 2:35 pm

    Ok I’ve wrote some code, but you might not like it 🙂

    public class DataChangedEventArgs : EventArgs
    {
        public string Data { get; set; }
    
        public DataChangedEventArgs(string data)
        {
            Data = data;
        }
    }
    public delegate void DataChangedEventHander(DataChangedEventArgs e);
    public class BackEnd
    {
        public event DataChangedEventHander OnDataChanged;
        private string _data;
        public string Data
        {
            get { return _data; }
            set
            {
                _data = value;
                RaiseOnDataChanged();
            }
        }
    
        private static readonly object _sync = new object();
        private static BackEnd _instance;
        public static BackEnd Current
        {
            get
            {
                lock (_sync)
                {
                    if (_instance == null)
                        _instance = new BackEnd();
                    return _instance;
                }
            }
        }
        private void RaiseOnDataChanged()
        {
            if(OnDataChanged != null)
                OnDataChanged(new DataChangedEventArgs(Data));
        }
    }
    public class ConsumerControl
    {
        public event EventHandler OnTextChanged;
        private string _text;
        public string Text
        {
            get
            {
                return _text;
            }
            set
            {
                _text = value;
                if (OnTextChanged != null)
                    OnTextChanged(this, EventArgs.Empty);
            }
        }
    }
    public class Consumer
    {
        public ConsumerControl Control { get; set; }
    
        public Consumer()
        {
            Control = new ConsumerControl();
            BackEnd.Current.OnDataChanged += NotifyConsumer;
            Control.OnTextChanged += OnTextBoxDataChanged;
        }
    
        private void OnTextBoxDataChanged(object sender, EventArgs e)
        {
            NotifyBackEnd();
        }
    
        private void NotifyConsumer(DataChangedEventArgs e)
        {
            Control.Text = e.Data;
        }
        private void NotifyBackEnd()
        {
            // unsubscribe
            BackEnd.Current.OnDataChanged -= NotifyConsumer;
            BackEnd.Current.Data = Control.Text;
            // subscribe again
            BackEnd.Current.OnDataChanged += NotifyConsumer;
        }
    }
    public class BackEndTest
    {
        public void Run()
        {
            var c1 = new Consumer();
            var c2 = new Consumer();
            c1.Control.Text = "1";
            BackEnd.Current.Data = "2";
        }
    }
    

    The main idia is here:

    // unsubscribe
    BackEnd.Current.OnDataChanged -= NotifyConsumer;
    BackEnd.Current.Data = Control.Text;
    // subscribe again
    BackEnd.Current.OnDataChanged += NotifyConsumer;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code seems not to work, even though the file appears to be
The following javascript code seems to leak memory but I’m not sure why. I’ve
It seems that the following is a common method given in many tutorials on
Hi i'm relatively new to java programming. The following program i've written seems to
I am relatively new to the design pattern, and it seems to me that
The following two statements both have negligible execution times over a relatively large table.
The following code seems to be just too much, for getting a single count
I can't believe that the following statement seems to be still true So, I
I have absolutely no idea why this is happening but the following code seems
The following XML implementation seems to work fine: <?xml version=1.0 encoding=utf-8?> <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:layout_width=fill_parent

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.