Odpovědět databinding a textbox

  • 15. dubna 2012 20:29
     
     

    i have a class called person which i have shown below. in the form, i instantiate a person object at the class level. there are two buttons which when clicked will change the name property in my object. i want that name property to update dynamically in the textbox as it is changed by clicking on either button. to bind the datasource, i went to the designer and clicked on the textbox, then selected properties then went to data and selected databinding then text then i selected my class person. when i try to run the program with the debugger, i click on both buttons but the textbox does not get updated. why?

     using System;

    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace WindowsFormsApplication1
    {
        class person
        {
            private string firstname;
            

            public string name
            {
                get
                {
                    return firstname;
                }
                set
                {
                    firstname = value;
                }
            }
        }
    }

    FORM CODE: 

    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            person person1 = new person();
            

            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                person1.name = "joe";
                
                
                   }

            private void button2_Click(object sender, EventArgs e)
            {
                person1.name = "frank";
                
            }

        }
    }

Všechny reakce

  • 16. dubna 2012 1:23
    Moderátor
     
      Obsahuje kód

    Your class needs to implement INotifyChanged. Change it like this:

    class person : INotifyPropertyChanged
    {
        private string firstname;
            
        public string name
        {
            get 
            {
                return firstname;
            }
            set
            {
                firstname = value;
                this.NotifyPropertyChanged("name"); 
            }
        }
        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        #endregion
    }


    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com

  • 16. dubna 2012 2:31
     
     
    Thanks for taking the time to respond. I added the code into my class and added the System.Component namespace to the class as well. When I run the program, there are no debug errors shown. My form appears with a blinking cursor inside of the textbox and the two buttons "Joe" and "Frank". I click on either the "Joe" or "Frank" button thinking that the instance property will be set and then the textbox will show the name "Joe" or "Frank" but it still doesn't seem to work. Is there anything else that I could be missing?
  • 16. dubna 2012 4:45
    Moderátor
     
     Odpovědět

    You indicated that you did the DataBinding via the Properties (I never do that ... I always code it all myself).  You should take a look at the IDE generated code (in the Designer.cs file) and see how it decided to databind that TextBox ... maybe it didn't generate the code properly for some reason.

    What you're looking for should be something like this:

    MyTextBox.DataBindings.Add("Text", person1, "name");

    If you can't find that anywhere, try typing it yourself in the constructor ... after the InitializeComponent() and that should take care of it.


    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com

  • 16. dubna 2012 5:42
     
     
    Thanks for all of your help. I started fresh and created an identical project. This time instead of using the properties window, I manually added the data binding to the constructor in form1 after initializecomponent() and now it works fine. Thanks a lot! 
  • 16. dubna 2012 15:40
    Moderátor
     
     

    You're welcome! Glad I could help! =0)

    BTW, another place (usually better) to set up your DataBindings is the Form Load event handler. In your case, the constructor was fine though.


    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com