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 / December 2007

Tip: Looking for answers? Try searching our database.

Postback problem when I am using simple data binding on a overridden TextBox Control

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Henrik Skak Pedersen - 05 Dec 2007 14:08 GMT
Hello,

This example has been cut from a larger context, therefore it looks very
simple. It has just been created to illustrate the error. I have overridden
the standard TextBox so it now looks like this:

   public class MyTextBox : System.Windows.Forms.TextBox
   {
       protected string objectValue;

       public string ObjectValue
       {
           get { return objectValue; }
           set
           {
               this.Text = objectValue as string;
               objectValue = value;
           }
       }
   }

I have then created a form and placed several instances of the MyTextBox
control on it. All the controls bind to a property of a simple class.

---
           Employee employee = new Employee("Heinz", "Svendsen", 36);
           employeeBindingSource.DataSource = employee;
---
           this.firstNameTextBox.DataBindings.Add(new
System.Windows.Forms.Binding("ObjectValue", this.employeeBindingSource,
"FirstName", true));
----

Now if I bind the properties of the Employee class to the Text property of
the MyTextBox everything works fine. But if I bind to the "ObjectValue"
property the binding system writes the value from the MyTextBox back to the
employee class everytime I change control.  It thinks that the value has ben
changed even though I have changed nothing.

If I call EndEdit() on the bindingSource all controls write back the value
to the Employee class. This also only happens if I databind to the
ObjectValue property. Everything is ok if I use the Text property.

Any ideas?

Best regards
Henrik Skak Pedersen
Linda Liu[MSFT] - 06 Dec 2007 05:17 GMT
Hi Henrik,

I performed a test on your sample code and did see the problem you
described.

The reason why this problem doesn't exist when you bind the Text property
of the MyTextBox to the data source is that there's a TextChanged event in
the TextBox class, which data binding uses to get notified when the Text
property is changed.

Because there's no event that data binding can use to get notified when the
ObjectValue property is changed, data binding forces to push the value of
the ObjectValue property back to the data source every time the MyTextBox
loses the focus. If you add a ObjectValueChanged event in the MyTextBox
class, the problem should be solved. The following is a sample:

public class TextBox1 : System.Windows.Forms.TextBox
   {
       protected string objectValue;

       public string ObjectValue
       {
           get { return this.objectValue; }
           set
           {
               if (objectValue != value)
               {
                   objectValue = value;
                   OnObjectValueChanged();
               }
           }
       }
       public event EventHandler ObjectValueChanged;
       private void OnObjectValueChanged()
       {
           if (ObjectValueChanged != null)
           {
               ObjectValueChanged(this, new EventArgs());
           }
       }
   }

Please try the above code in your project to see if the problem is solved
and let me know the result.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Henrik Skak Pedersen - 10 Dec 2007 19:37 GMT
Hi Linda,

Thank you very much for your reply. I had to change the suggested code a bit
because I am using a standard textbox, and the text being entered is being
set in the Text property.  But otherwise it is working great. Thank you very
much for your help.

Best regards

Henrik Skak Pedersen

   public class TextBox : System.Windows.Forms.TextBox
   {
       protected string objectValue;
 public string ObjectValue
 {
  get { return objectValue; }
  set
  {
               objectValue = value;
               base.Text = objectValue as string;
  }
 }

       public event EventHandler ObjectValueChanged;
       private void OnObjectValueChanged()
       {
           if (ObjectValueChanged != null)
               ObjectValueChanged(this, new EventArgs());
       }

       protected override void OnTextChanged(EventArgs e)
       {
           this.objectValue = this.Text;

           base.OnTextChanged(e);
           this.OnObjectValueChanged();

           Console.WriteLine(this.Text);
       }

       [Browsable(false),
       DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
       public override string Text
       {
           get { return base.Text; }
           set { base.Text = value;  }
       }
}

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.