Binding to settings?

  • Thread starter Thread starter Peter Duniho
  • Start date Start date
P

Peter Duniho

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.Default.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
 
is there some simple, direct way to hook up
the two existing objects?

Yes: DataBindings.Add(...) - example below (you need to add a settings
file with a string "TestSetting" for this to work)

Marc

static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
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
// (OnPropertyChanged for quicker feedback for demo)
tb.DataBindings.Add("Text", Settings.Default,
"TestSetting", false,
DataSourceUpdateMode.OnPropertyChanged);
// and track in the form's caption and label just to
prove it works
f.DataBindings.Add("Text", Settings.Default,
"TestSetting");
lbl.DataBindings.Add("Text", Settings.Default,
"TestSetting");
Application.Run(f);
}
}
}
 
Yes: DataBindings.Add(...) - example below (you need to add a settings
file with a string "TestSetting" 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
 
Actually, in retrospect you might be able to... if you use the
(application settings) option in the property grid.

Marc
 
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 "ApplicationSettings" exists and is
separate from the normal "DataBindings" 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top