C# user.config confusion

  • Thread starter Thread starter Bit Twiddler
  • Start date Start date
B

Bit Twiddler

Hi,

I have a windows form app which would benefit from the new application
settings support in VS2005.

Say that my form has a single textbox on it. When the form closes I would
like to save the text in that textbox as part as the user level application
settings. When the application restarts I would like that text to be
restored to the textbox.

Because I am not using a built-in property on my form does this mean that I
cannot use the designer to add my setting?

Or, do I *have* to create a custom class derived from
ApplicationSettingsBase and manage everything myself?

No matter how I approach this I cannot seem to get my settings to
"automagically" restore their values at app startup.

Any pointers would be greatly appreciated!

BT
 
Just to provide more information:

Using the designer I added the following setting (using the designer)
Name: MySetting
Type: string
Scope: User
Value: Initial value

public Form1()
{
InitializeComponent();
// Load the data from user.config
textBox1.Text = Properties.Settings.Default.MySetting;
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
// Save any of my settings
Properties.Settings.Default.Save();
}

My problem is that textBox1.Text MySetting is not being restored correctly -
it always reverts to its "initial" value.

-BT
 
Ok, I figured it out - of course it was user error.

I was not updating the settings when the text of the textbox was actually
changed! :(

private void textBox1_TextChanged(object sender, EventArgs e)
{
Properties.Settings.Default.MySetting = textBox1.Text;
}
-BT
 
BT,

Why not use data binding and just bind the value of the textbox to the
MySetting property on the object? This would probably be much easier.

Hope this helps.
 
Thank you for the suggestion Nicholas and in fact I was just trying to
figure out the correct way to do that.

I am not sure how to create the binding between
Properties.Settings.Default.MySetting and the Text property of the textBox1
textbox.

textBox1.DataBindings.Add("Text", Properties.Settings, "Default.MySetting
");

Is this correct?

Thanks!
BT

Nicholas Paldino said:
BT,

Why not use data binding and just bind the value of the textbox to the
MySetting property on the object? This would probably be much easier.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Bit Twiddler said:
Ok, I figured it out - of course it was user error.

I was not updating the settings when the text of the textbox was actually
changed! :(

private void textBox1_TextChanged(object sender, EventArgs e)
{
Properties.Settings.Default.MySetting = textBox1.Text;
}
-BT
 
BT,

Almost. I think you want:

textBox1.DataBindings.Add("Text", Properties.Settings.Default, "MySetting");


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Bit Twiddler said:
Thank you for the suggestion Nicholas and in fact I was just trying to
figure out the correct way to do that.

I am not sure how to create the binding between
Properties.Settings.Default.MySetting and the Text property of the
textBox1 textbox.

textBox1.DataBindings.Add("Text", Properties.Settings, "Default.MySetting
");

Is this correct?

Thanks!
BT

Nicholas Paldino said:
BT,

Why not use data binding and just bind the value of the textbox to the
MySetting property on the object? This would probably be much easier.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Bit Twiddler said:
Ok, I figured it out - of course it was user error.

I was not updating the settings when the text of the textbox was
actually changed! :(

private void textBox1_TextChanged(object sender, EventArgs e)
{
Properties.Settings.Default.MySetting = textBox1.Text;
}
-BT

Just to provide more information:

Using the designer I added the following setting (using the designer)
Name: MySetting
Type: string
Scope: User
Value: Initial value

public Form1()
{
InitializeComponent();
// Load the data from user.config
textBox1.Text = Properties.Settings.Default.MySetting;
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
// Save any of my settings
Properties.Settings.Default.Save();
}

My problem is that textBox1.Text MySetting is not being restored
correctly - it always reverts to its "initial" value.

-BT


Hi,

I have a windows form app which would benefit from the new application
settings support in VS2005.

Say that my form has a single textbox on it. When the form closes I
would like to save the text in that textbox as part as the user level
application settings. When the application restarts I would like that
text to be restored to the textbox.

Because I am not using a built-in property on my form does this mean
that I cannot use the designer to add my setting?

Or, do I *have* to create a custom class derived from
ApplicationSettingsBase and manage everything myself?

No matter how I approach this I cannot seem to get my settings to
"automagically" restore their values at app startup.

Any pointers would be greatly appreciated!

BT
 
Thank you Nicholas - for YEARS you have been helping countless thousands of
developers and you are much appreciated.

BT

Nicholas Paldino said:
BT,

Almost. I think you want:

textBox1.DataBindings.Add("Text", Properties.Settings.Default,
"MySetting");


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Bit Twiddler said:
Thank you for the suggestion Nicholas and in fact I was just trying to
figure out the correct way to do that.

I am not sure how to create the binding between
Properties.Settings.Default.MySetting and the Text property of the
textBox1 textbox.

textBox1.DataBindings.Add("Text", Properties.Settings, "Default.MySetting
");

Is this correct?

Thanks!
BT

Nicholas Paldino said:
BT,

Why not use data binding and just bind the value of the textbox to
the MySetting property on the object? This would probably be much
easier.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ok, I figured it out - of course it was user error.

I was not updating the settings when the text of the textbox was
actually changed! :(

private void textBox1_TextChanged(object sender, EventArgs e)
{
Properties.Settings.Default.MySetting = textBox1.Text;
}
-BT

Just to provide more information:

Using the designer I added the following setting (using the designer)
Name: MySetting
Type: string
Scope: User
Value: Initial value

public Form1()
{
InitializeComponent();
// Load the data from user.config
textBox1.Text = Properties.Settings.Default.MySetting;
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
// Save any of my settings
Properties.Settings.Default.Save();
}

My problem is that textBox1.Text MySetting is not being restored
correctly - it always reverts to its "initial" value.

-BT


Hi,

I have a windows form app which would benefit from the new
application settings support in VS2005.

Say that my form has a single textbox on it. When the form closes I
would like to save the text in that textbox as part as the user level
application settings. When the application restarts I would like
that text to be restored to the textbox.

Because I am not using a built-in property on my form does this mean
that I cannot use the designer to add my setting?

Or, do I *have* to create a custom class derived from
ApplicationSettingsBase and manage everything myself?

No matter how I approach this I cannot seem to get my settings to
"automagically" restore their values at app startup.

Any pointers would be greatly appreciated!

BT
 
BT,

Thanks for the props. Much appreciated.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Bit Twiddler said:
Thank you Nicholas - for YEARS you have been helping countless thousands
of developers and you are much appreciated.

BT

Nicholas Paldino said:
BT,

Almost. I think you want:

textBox1.DataBindings.Add("Text", Properties.Settings.Default,
"MySetting");


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Bit Twiddler said:
Thank you for the suggestion Nicholas and in fact I was just trying to
figure out the correct way to do that.

I am not sure how to create the binding between
Properties.Settings.Default.MySetting and the Text property of the
textBox1 textbox.

textBox1.DataBindings.Add("Text", Properties.Settings,
"Default.MySetting ");

Is this correct?

Thanks!
BT

in message BT,

Why not use data binding and just bind the value of the textbox to
the MySetting property on the object? This would probably be much
easier.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ok, I figured it out - of course it was user error.

I was not updating the settings when the text of the textbox was
actually changed! :(

private void textBox1_TextChanged(object sender, EventArgs e)
{
Properties.Settings.Default.MySetting = textBox1.Text;
}
-BT

Just to provide more information:

Using the designer I added the following setting (using the designer)
Name: MySetting
Type: string
Scope: User
Value: Initial value

public Form1()
{
InitializeComponent();
// Load the data from user.config
textBox1.Text = Properties.Settings.Default.MySetting;
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
// Save any of my settings
Properties.Settings.Default.Save();
}

My problem is that textBox1.Text MySetting is not being restored
correctly - it always reverts to its "initial" value.

-BT


Hi,

I have a windows form app which would benefit from the new
application settings support in VS2005.

Say that my form has a single textbox on it. When the form closes I
would like to save the text in that textbox as part as the user
level application settings. When the application restarts I would
like that text to be restored to the textbox.

Because I am not using a built-in property on my form does this mean
that I cannot use the designer to add my setting?

Or, do I *have* to create a custom class derived from
ApplicationSettingsBase and manage everything myself?

No matter how I approach this I cannot seem to get my settings to
"automagically" restore their values at app startup.

Any pointers would be greatly appreciated!

BT
 
Back
Top