Re: simple binding of controls to object properties - refresh cont

From: Rua Haszard Morris (RuaHaszardMorris_at_discussions.microsoft.com)
Date: 10/19/04


Date: Tue, 19 Oct 2004 16:45:09 -0700

Thanks again Chris for looking into this.! Thanks Sijin for already
suggesting this (the correct?) approach.

This is now officially doing my head in - this doesn't work for me. I've
udpated my struct (class!) so that it has event handlers, suitably named (is
it documented anywhere that there is magic binding if eventhandlers are named
appropriately?), and the controls DON'T update!

Can anybody think of a reason why it isn't working for me? I have named
events correctly, but they are not getting set to anything...

Still I think I prefer just manually reseting the bindings, it seems like
bloat to add the eventhandlers & if (event) event() to each set(), anyway!
If the form class needs to handle all those events, forget it, I'll just
reset the bindings!

AND apparently it's all changing for the better in 2005...

My new (wishlist) question is can we define a parent class, which can be
inherited from to provide automatic binding-notification of property changes.
i.e.

class PropertyBindableBase
{
// the magic happens here
}

class SomeProperties : PropertyBindableBase
{
 // a nice small simple class with properties, sets and gets
 public string Ting
 {
   get { return stringy; };
   set { stringy = value; };
 }
 private string stringy = "";
}

class BoundForm : Form
{
 textbox BobTheBound = new TextBox();
 SomeProperties pro = new SomeProperties();
...
 Form()
 {
  BobTheBound.Bindings.Add("Text", someProperties, "Ting")
 }
}

???
Anyway there's a nice long posting summing up my thoughts...

cheers all who helped,
Rua HM.

"Chris Jobson" wrote:

> I think I've now found the right way to do it. The web page
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnadvnet/html/vbnet02252003.asp
> describes how to do it in VB, and I've just translated it to C# and it
> works. Basically if you have a property called Abc then you have to define
> an event named AbcChanged, and raise this event when your property changes
> (though this seems to be a very well-kept secret!!). Here's the trivial
> example that I tested it on:
>
> public class MyString {
> string s;
>
> public event EventHandler theStringChanged;
>
> public MyString(string s) {
> this.s = s;
> }
>
> public string theString {
> get { return s; }
> set {
> if (s != value) {
> s = value;
> if (theStringChanged != null)
> theStringChanged(this, new EventArgs());
> }
> }
> }
> }
> ....
> MyString myStr = new MyString("Hello world");
> ....
> textBox1.DataBindings.Add(new Binding("Text", myStr, "theString"));
>
> The textbox is now properly bound to the MyString object: assigning a new
> value to myStr.theString updates the textbox, and typing a new value in the
> textbox updates myStr.theString when the textBox loses focus.
>
> Chris Jobson
>
> "Chris Jobson" <chris.jobson@btinternet.com> wrote in message
> news:eNVkRSVtEHA.1276@TK2MSFTNGP12.phx.gbl...
> > As an experiment I managed to get this to work by implementing the
> > IBindingList interface in my class and raising the ListChanged event in
> > the property set code. However, this feels like the wrong solution because
> > to implement IBindingList you also have to implement IList, ICollection
> > and IEnumerable - and most of the methods in these interfaces are
> > irrelevant as the class is NOT actually implementing a list. If you want
> > to see the code email me and I'll send it to you off-list (but note that
> > I'll be off-line from the end of this week for about three weeks).
> >
> > Chris Jobson
> >
> > "Rua Haszard Morris" <RuaHaszardMorris@discussions.microsoft.com> wrote in
> > message news:E4E0044C-0CF8-46EA-8739-5BCE490F64F4@microsoft.com...
> >> Thanks everyone for all the help - ResumeBinding doesn't appear to do it
> >> after all, but clearing and reset ing all the bindings does, which is a
> >> not-so-bad workaround in my opinion.
> >>
> >> I figure that I'm probably using this (data binding) in a way it's not
> >> really supposed to be used... my "data source" is basically a struct with
> >> properties wrapping its members (which explains why I didn't want to
> >> fatten
> >> the class up with events!).
> >>
> >> Cheers,
> >> Rua HM.
> >
> >
>
>
>



Relevant Pages

  • Re: Welche Datenstruktur ist am geeignetsten?
    ... Ich nehme mal an das Du die neuen Werte in eine Textbox eingibst. ... String, mal double, mal Date, usw. sein ... "Chris Pick" wrote: ... Feldname, bool ob DF, Wert des Feldes ...
    (microsoft.public.de.german.entwickler.dotnet.vb)
  • Re: ListBox question
    ... other than a String, the compiler would complain. ... Dim myArray As Variant ... Dim myLong As Long ... I tested by entering a 'plain textbox shape' ...
    (microsoft.public.word.vba.general)
  • Re: ListBox question
    ... "Russ" wrote: ... other than a String, the compiler would complain. ... Dim myLong As Long ... I tested by entering a 'plain textbox shape' ...
    (microsoft.public.word.vba.general)
  • Re: Textbox Validation and Associated Checkbox
    ... Notice I have added the test for an empty TextBox (the empty string is not a number which is why you were caught up in a loop). ... Private Sub TextBox1_MouseDown(ByVal Button As Integer, ... DecimalPointSymbol = Format$ ...
    (microsoft.public.excel.programming)
  • Re: Spell Checker
    ... I have a textbox, that he user can type some information. ... Private m_sTextCorrected As String ... Public Property Get TextCorrectedAs String ... you must add a reference to the Word object library. ...
    (microsoft.public.vb.controls)

Loading