Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Data Binding / September 2007

Tip: Looking for answers? Try searching our database.

Beginner

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Marco Schwarz - 14 Sep 2007 14:30 GMT
I try to use Databinding with the BindingList. So I write a class
Person(Name, ReadOnly) and a PersonList : BindingList<Person>.

In my form I have two controls (TextBox and CheckBox) in the
MainFormLoad function I binding as follow:

void MainFormLoad(object sender, EventArgs e)
{
 personList = new PersonList();
 cbxPersonReadOnly.DataBindings.Add("Checked", personList, "ReadOnly");
 txtPersonName.DataBindings.Add("Text", personList, "Name");
 txtPersonName.DataBindings.Add("ReadOnly", personList, "ReadOnly");
}

My question:

I click on the checkbox (ReadOnly) and the TextBox don't changed. Then
click on Next or Prev and the TextBox.ReadOnly is changed.

Can someone says me, what I do wrong?

Thanks
Marco
Marco Schwarz - 14 Sep 2007 14:44 GMT
> My question:
>
> I click on the checkbox (ReadOnly) and the TextBox don't changed. Then
> click on Next or Prev and the TextBox.ReadOnly is changed.

Sorry, now it works correctly. Perhaps I run a old version of the
application.

Thanks
Marco
Marco Schwarz - 14 Sep 2007 15:24 GMT
Marco Schwarz schrieb:
> Sorry, now it works correctly. Perhaps I run a old version of the
> application.

Only with the Event

void CbxPersonReadOnlyClick(object sender, EventArgs e)
{       
    txtPersonName.ReadOnly = cbxPersonReadOnly.Checked;
}

it works fine.

Have someone an idea?

Thx
Marco
Morten Wennevik [C# MVP] - 15 Sep 2007 08:50 GMT
> I try to use Databinding with the BindingList. So I write a class
> Person(Name, ReadOnly) and a PersonList : BindingList<Person>.
[quoted text clipped - 19 lines]
> Thanks
> Marco

Hi Marco,

It's a combination of things that is causing your TextBox to update late.

First, the default databinding behaviour is to update its underlying source once you validate the control for instance by leaving it.  Since you want the TextBox to gray out immediatly as you click the CheckBox you need to tell the databinding to update immediatly (see the code below on how to do this).

Second, since you bind two controls to the same property the second control won't get notified, and won't get updated until you cange the datasource by selecting another item or similar.  To fix this, Person needs to implement the INotifyPropertyChanged event, and fire an event every time this property is changed.  This is also true for any property you change programmatically (see the code below on how to implement and use INotifyPropertyChanged)

    public partial class Form2 : System.Windows.Forms.Form
    {

        public Form2()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            // You probably don't need a PersonList class
            BindingList<Person> personList = new BindingList<Person>();
            personList.Add(new Person("One"));
            personList.Add(new Person("Two"));
            personList.Add(new Person("Three"));

            // The ListBox will allow us to change persons and see the effect of setting ReadOnly
            listBox1.DataSource = personList;
            listBox1.DisplayMember = "Name";

            // Note the added parameters for the Checked property
            // Setting DataSourceUpdateMode.OnPropertyChanged means the Person will get updated immediatly.
            // This is also good on TextBoxes where you want to update something while you write.
            checkBox1.DataBindings.Add("Checked", personList, "ReadOnly", false, DataSourceUpdateMode.OnPropertyChanged);
            textBox1.DataBindings.Add("Text", personList, "Name");
            textBox1.DataBindings.Add("ReadOnly", personList, "ReadOnly");
        }
    }

    public class Person : INotifyPropertyChanged
    {
        private bool readOnly;

        public bool ReadOnly
        {
            get { return readOnly; }
            set
            {
                readOnly = value;
                // Fire a PropertyChanged event so the TextBox will get notified
                PropertyChanged(this, new PropertyChangedEventArgs("Locked"));
            }
        }

        private string name;

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

        public Person(string name)
        {
            this.Name = name;
        }

        // This is the code added by SHIFT-ALT-F10 on INotifyPropertyChanged
        // All this interface does is declare a single event
        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }

Signature

Happy coding!
Morten Wennevik [C# MVP]

Marco Schwarz - 15 Sep 2007 11:39 GMT
Thanks, it work fine

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.