+ Reply to Thread
Results 1 to 3 of 3

Thread: RichTextBox & databinding

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    31

    RichTextBox & databinding

    Trying to bind a String to a RichTextBox.Text property so that when the String value changes, that change is reflected in the RichTextBox. So far I'm unsuccessful.
    Code:
    string test = "Test";
    rtxt_chatLog.DataBindings.Add("Text",test,null);
    test = "a";
    This shows "Test" in the rtxt_chatLog, but not the "a".

    Even tried adding
    Code:
    rtxt_chatLog.Refresh();
    but that does not make any difference.

    Also tried this one but this does not work either:
    Code:
    public class Test
    {
        public string Property { get; set; }
    }
    
    Test t = new Test();
    t.Property = "test";
    rtxt_chatLog.DataBindings.Add("Text", t, "Property");
    t.Property = "a";

  2. #2
    The String class doesn't implement INotifyPropertyChanged, so there are no events for the binding source to tell the RichTextBox that something changed.
    Try updating your class with the INotifyPropertyChanged :
    Code:
    public class Test : INotifyPropertyChanged {
      public event PropertyChangedEventHandler PropertyChanged;
    
      private string _PropertyText = string.Empty;
    
      public string PropertyText {
        get { return _PropertyText; }
        set {
          _PropertyText = value;
          OnPropertyChanged("PropertyText");
        }
      }
    
      private void OnPropertyChanged(string propertyName) {
        if (PropertyChanged != null)
          PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
      }
    
    }
    Also, it looks like DataBinding doesn't like the name "Property" for a property name. Try changing it to something else other than "Property".
    rtxt_chatLog.DataBindings.Add("Text", t, "PropertyText");

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    31
    Thank you very much for helping .

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts