Binding to settings?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peter Duniho

    #1

    Binding to settings?

    Okay, I'm hoping I'm missing something obvious here.

    Assume I've got a TextBox in my Form. I also have a string type user
    setting in the usual way (say, Settings.Defaul t.MyText).

    Is there some way to use data binding to connect the two?

    I tried the direct route, adding the Properties object to my data binding
    sources, but nothing was in it, never mind could I drill down far enough
    to get to the MyText property.

    What am I missing? Do I have to create some sort of special binding
    wrapper for this to work? Or is there some simple, direct way to hook up
    the two existing objects?

    Pete
  • Marc Gravell

    #2
    Re: Binding to settings?

    is there some simple, direct way to hook up
    the two existing objects?
    Yes: DataBindings.Ad d(...) - example below (you need to add a settings
    file with a string "TestSettin g" for this to work)

    Marc

    static class Program
    {
    [STAThread]
    static void Main()
    {
    Application.Ena bleVisualStyles ();
    using (Form f = new Form())
    using (TextBox tb = new TextBox())
    using (Label lbl = new Label())
    {
    lbl.Dock = tb.Dock = DockStyle.Top;
    f.Controls.Add( tb);
    f.Controls.Add( lbl);

    // data-bind the textbox to a setting
    // (OnPropertyChan ged for quicker feedback for demo)
    tb.DataBindings .Add("Text", Settings.Defaul t,
    "TestSettin g", false,
    DataSourceUpdat eMode.OnPropert yChanged);
    // and track in the form's caption and label just to
    prove it works
    f.DataBindings. Add("Text", Settings.Defaul t,
    "TestSettin g");
    lbl.DataBinding s.Add("Text", Settings.Defaul t,
    "TestSettin g");
    Application.Run (f);
    }
    }
    }

    Comment

    • Peter Duniho

      #3
      Re: Binding to settings?

      On Thu, 20 Dec 2007 01:15:14 -0800, Marc Gravell <marc.gravell@g mail.com>
      wrote:
      >is there some simple, direct way to hook up
      >the two existing objects?
      >
      Yes: DataBindings.Ad d(...) - example below (you need to add a settings
      file with a string "TestSettin g" for this to work)
      Great! Thanks very much. :)

      Next question: the code looks to me pretty similar to what going through
      the designer to add a binding would result in. Any idea why I wasn't able
      to implement this in the designer? Why do I have to hand-code it? I've
      been able to add bindings for other controls without all the trouble (such
      as it is...the code's obviously not complicated :) ).

      I have a feeling I'm missing some important step in the designer, but what
      exactly that is doesn't come to mind at the moment.

      Pete

      Comment

      • Marc Gravell

        #4
        Re: Binding to settings?

        Actually, in retrospect you might be able to... if you use the
        (application settings) option in the property grid.

        Marc

        Comment

        • Peter Duniho

          #5
          Re: Binding to settings?

          On Thu, 20 Dec 2007 11:55:06 -0800, Marc Gravell <marc.gravell@g mail.com>
          wrote:
          Actually, in retrospect you might be able to... if you use the
          (application settings) option in the property grid.
          Duh. That works just fine.

          I think it's a little bizarre that the "ApplicationSet tings" exists and is
          separate from the normal "DataBindin gs" section. But it's not like it's
          obscurely named or less prominent; I definitely should've been able to
          notice that myself. I was just so focused on using the technique I'd
          already learned, I never noticed it.

          Thanks for the kick in the pants...I needed that. :)

          Pete

          Comment

          Working...