How can I bind a Winform textbox to a class property?

Bill picture Bill · Jan 29, 2016 · Viewed 9.9k times · Source

I have read a number of questions on Stack Overflow and elsewhere that describe tying a textbox to a class but I cannot seem to even get the basics working without receiving an error from VS when compiling.

(1) What I want to accomplish is to display the text of a property from a class.

(2) When the user modifies that text, I want the property to automatically update.

Unfortunately I cannot even get past (1) yet.

The class:

class BookProperties : INotifyPropertyChanged
{

    private string _bookTitle;
    public string bookTitle { get { return _bookTitle; } set { SetField(ref _bookTitle, value, "bookTitle"); } }

    #region handle property changes

    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
    protected bool SetField<T>(ref T field, T value, string propertyName)
    {
        if (EqualityComparer<T>.Default.Equals(field, value)) return false;
        field = value;
        OnPropertyChanged(propertyName);
        return true;
    }

    #endregion
}

The class initializer:

BindingList<BookProperties> bookProperty = new BindingList<BookProperties>();

The connection to the textbox:

    textBox1.DataBindings.Clear();
    textBox1.DataBindings.Add("Text", bookProperty, "bookProperty.bookTitle");

I have also tried this:

    textBox1.DataBindings.Clear();
    textBox1.DataBindings.Add("Text", bookProperty, "bookProperty[0].bookTitle");

Visual Studio throws the following error:

Child list for field bookProperty cannot be created.

START EDIT: Trying this code, I remove the additional element from the third parameter as a few have suggested.

bookProperty.Add(new BookProperties(){bookTitle="C#"});
textBox1.DataBindings.Add("Text", bookProperty[0], "bookTitle");

Now, I receive this error. I had received it before and searched for a resolution but think it might be too generic for me to figure out what exactly I am doing wrong.

An unhandled exception of type 'System.ArgumentException' occurred in System.Windows.Forms.dll

Additional information: This causes two bindings in the collection to bind to the same property.

END EDIT

I am starting to think there is something fundamentally wrong with my approach since I have seen similar code that people say works so I am hopeful someone can tell me what I am doing wrong. Please note this is a Windows Form issue, not XAML. Thanks.

FIXED Sorry about the last edit. It turns out I had previously attempted to manually link the textbox to a data source using the VS designer. After I removed that data source, everything worked. Thank you for the help!

Answer

Jamaxack picture Jamaxack · Jan 29, 2016

Try this:

bookProperty.Add(new BookProperties(){bookTitle="C#"});
textBox1.DataBindings.Add("Text", bookProperty[0], "bookTitle");

Second argument is source that should be shown, third parameter is source class property. Also make sure there is items in bookProperty list.

Hope helps.