0

Before I begin with my question I've been reading every link imaginable on the subject and I'm still stuck.

I am putting together a WPF using C# and Visual Studio. I want to have a TextBox retain text in it when the application to shut down and restarted and that the text is read/write because the text will change often.

I've set up a new .settings file and have it in my Properties folder in my project. I created this using the nifty grid where I can make a name, type, and value etc etc.

So far after several several hours I think I'm closer yet am not sure. This is the Designer.cs -

namespace PRX.Properties {


    [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "10.0.0.0")]
    internal sealed partial class saveData : global::System.Configuration.ApplicationSettingsBase {

        private static saveData defaultInstance = ((saveData)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new saveData())));

        public static saveData Default {
            get {
                return defaultInstance;
            }
        }

        [global::System.Configuration.UserScopedSettingAttribute()]
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.Configuration.DefaultSettingValueAttribute("mainSource")]
        public string Setting {
            get {
                return ((string)(this["Setting"]));
            }
            set {
                this["Setting"] = value;
            }
        }
    }
}

The .settings file is called saveData.settings and I have the table set up as Name: Setting , Type: String, Value: mainSource. The Scope is set to user. This is in the Properties folder just like the default Settings.settings files.

The TextBox in questions has this connected like so:

                        <ScrollViewer VerticalScrollBarVisibility="Auto">
                            <TextBox DockPanel.Dock="Top" Margin="10" Background="Black" Name="tb1" TextWrapping="Wrap" AcceptsReturn="True" Text="{Binding Source={x:Static p:saveData.Default}, Path=Default.Setting, Mode=TwoWay}"></TextBox>

                        </ScrollViewer>

And for good measure here's how it looks like in the app.config:

  <PRX.saveData>
   <setting name="Setting" serializeAs="String">
    <value>mainSource</value>
   </setting>
  </PRX.saveData>
 </userSettings>

I know it's connected to the .Settings file because when I debug the value 'mainSource' is present in the Textbox

I've read over dozen links on the subject but most notably these two:

http://blogs.msdn.com/b/patrickdanino/archive/2008/07/23/user-settings-in-wpf.aspx http://msdn.microsoft.com/en-us/library/k4s6c3a0.aspx

I am especially confused with this, the documentation I am reading seems very straight-forward yet I can't seem to grasp it. Any help is supremely appreciated. Thanks

nycdude
  • 1
  • 4

1 Answers1

0

The problem is that you are not calling "Save()" on your setting that why its not retained, you could try loading the text when the window initialize and save it to settings when you exit, YOU have to call Save() to persist your setting

Edit

As you said the textbox is connected to the settings but change is not saved, to do this you have to call "Save" on your settings and it doesn't matter where you do it, e.g you can add handler for text changed:

    <TextBox DockPanel.Dock="Top" Margin="10" Background="Black" Name="tb1" TextWrapping="Wrap" AcceptsReturn="True" Text="{Binding Source={x:Static p:saveData.Default}, Path=Default.Setting, Mode=TwoWay} TextChanged="tb1_TextChanged""></TextBox>

and add handler to the code behind:

    private void tb1_TextChanged(object sender, TextChangedEventArgs e)
    {
        Properties.saveData.Default.Save();
    }

check this link: http://blogs.msdn.com/b/patrickdanino/archive/2008/07/23/user-settings-in-wpf.aspx Best way to bind WPF properties to ApplicationSettings in C#?

Community
  • 1
  • 1
mhashim
  • 133
  • 1
  • 8
  • Hmmm, now that is something I'm missing in all this. This is a property for the TextBox in the .cs file? Yes ... I'm pretty new to all this and learning on the fly – nycdude Mar 04 '13 at 18:44
  • no this has to do with the setting. Just go to the window where your textbox is. add event handler for the exit event put this code: Properties.saveData.Default.Setting.Save(); – mhashim Mar 04 '13 at 18:55
  • also make sure your change is propagated e.g add "UpdateSourceTrigger=PropertyChanged" to the binding in Xaml – mhashim Mar 04 '13 at 19:00
  • Hey thanks so much for your time helping me with this. I am able to figure this out: also make sure your change is propagated e.g add "UpdateSourceTrigger=PropertyChanged" to the binding in Xaml however I'm not sure how to do this part: no this has to do with the setting. Just go to the window where your textbox is. add event handler for the exit event put this code: Properties.saveData.Default.Setting.Save(); – nycdude Mar 05 '13 at 20:02