none
Textbox autosave (like onenote)

    Question

  • Hello, how can i add an autosave function (like onenote) to textbox. 

    Expl. When i exit the app, and i return in few minutes, the text i wrote is still in textbox.

    Thanks

    Tuesday, August 05, 2014 11:08 AM

All replies

  • Handle LostFocus event of TextBox and save the textbox text to IsolatedStorageSettings or database. And retrieve the text and set in TextBox.Text on Loaded event.

    <phone:PhoneApplicationPage
        x:Class="PhoneApp1.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
        xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        FontFamily="{StaticResource PhoneFontFamilyNormal}"
        FontSize="{StaticResource PhoneFontSizeNormal}"
        Foreground="{StaticResource PhoneForegroundBrush}"
        SupportedOrientations="Portrait" Orientation="Portrait"
        shell:SystemTray.IsVisible="True" Loaded="PhoneApplicationPage_Loaded">
    
        <!--LayoutRoot is the root grid where all page content is placed-->
        <Grid x:Name="LayoutRoot" Background="Transparent">
            <!--ContentPanel - place additional content here-->
            <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
                <TextBox Name="txtContent" LostFocus="txtContent_LostFocus" />
            </Grid>
        </Grid>
    </phone:PhoneApplicationPage>

    Code behind

    private void txtContent_LostFocus(object sender, RoutedEventArgs e)
            {
                System.IO.IsolatedStorage.IsolatedStorageSettings settings= System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings;
                if (settings.Contains("TextBoxData") == true)
                {
                    settings["TextBoxData"] = txtContent.Text;
                }
                else
                {
                    settings.Add("TextBoxData", txtContent.Text);
                }
    
            }
    
            private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
            {
                System.IO.IsolatedStorage.IsolatedStorageSettings settings= System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings;
                if (settings.Contains("TextBoxData") == true)
                {
                    txtContent.Text = settings["TextBoxData"].ToString();
                }
    
            }

    Instead of IsolatedStorageSettings you can also save to file or database.


    Gaurav Khanna | Microsoft VB.NET MVP | Microsoft Community Contributor


    Tuesday, August 05, 2014 11:33 AM
  • Hello

    it says this:

    Error 1 Event handler 'txtContent_LostFocus' not found on class 'FiroOS_Mobile_2._0_Banana.Fedit'

    Tuesday, August 05, 2014 12:17 PM
  • Did you add the "txtContent_LostFocus" and "PhoneApplicationPage_Loaded"
    method in code behind?

    Gaurav Khanna | Microsoft VB.NET MVP | Microsoft Community Contributor

    Tuesday, August 05, 2014 12:32 PM
  • I dont know (sorry i am begginer dev)

    my xaml

    <!--ContentPanel - place additional content here-->
            <Grid x:Name="ContentPanel" Grid.Row="1" Margin="24,0,0,0">
                <Grid x:Name="ContentPanel1" Margin="12,0,12,0" Grid.ColumnSpan="2">
                    <TextBox Name="txtContent" LostFocus="txtContent_LostFocus" TextChanged="txtContent_TextChanged" />
                </Grid>
                <Button Content="&lt;-" HorizontalAlignment="Left" Margin="40.955,-108,-7,0" VerticalAlignment="Top" Click="Button_Click_1" Grid.Column="1"/>

            </Grid>

    My c#

     private void txtContent_TextChanged(object sender, TextChangedEventArgs e)
            {
                System.IO.IsolatedStorage.IsolatedStorageSettings settings = System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings;
                if (settings.Contains("TextBoxData") == true)
                {
                    settings["TextBoxData"] = txtContent.Text;
                }
                else
                {
                    settings.Add("TextBoxData", txtContent.Text);
                }
            }

            private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
            {
                System.IO.IsolatedStorage.IsolatedStorageSettings settings = System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings;
                if (settings.Contains("TextBoxData") == true)
                {
                    txtContent.Text = settings["TextBoxData"].ToString();
                }

            }

    Tuesday, August 05, 2014 12:47 PM
  • Instead of "txtContent_TextChanged" have following code

    private void txtContent_LostFocus(object sender, RoutedEventArgs e)
            {
                System.IO.IsolatedStorage.IsolatedStorageSettings settings= System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings;
                if (settings.Contains("TextBoxData") == true)
                {
                    settings["TextBoxData"] = txtContent.Text;
                }
                else
                {
                    settings.Add("TextBoxData", txtContent.Text);
                }
    
            }


    Gaurav Khanna | Microsoft VB.NET MVP | Microsoft Community Contributor

    Tuesday, August 05, 2014 1:30 PM
  • I add code to project start app and leave, when i retun back textbox was clear. Plus when i try to debug once again it shows error Error Error 1 the operation returned because the timeout period expired. 0 0
    Tuesday, August 05, 2014 2:46 PM
  • Did you add Loaded event in your XAML code (underline code below)

    <phone:PhoneApplicationPage
        x:Class="PhoneApp1.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
        xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        FontFamily="{StaticResource PhoneFontFamilyNormal}"
        FontSize="{StaticResource PhoneFontSizeNormal}"
        Foreground="{StaticResource PhoneForegroundBrush}"
        SupportedOrientations="Portrait" Orientation="Portrait"
        shell:SystemTray.IsVisible="True" Loaded="PhoneApplicationPage_Loaded">


    Gaurav Khanna | Microsoft VB.NET MVP | Microsoft Community Contributor

    Tuesday, August 05, 2014 4:15 PM
  • A lot of new errors. Can you make video tutorial ?
    Tuesday, August 05, 2014 4:25 PM
  • Post your XAML and code file which you have done so far. Or you can upload a demo project so that I can try that.

    My first post contains complete XAML and xaml.cs code which you can try.


    Gaurav Khanna | Microsoft VB.NET MVP | Microsoft Community Contributor

    Wednesday, August 06, 2014 6:14 AM