Zapisywanie stanu TexBox'ów WPF

0

Chodzi mi o to, że jeżeli wpiszę liczbę np.10 to po zamknięciu programu i ponownym otwarciu w textbox'ie dalej będzie 10.
Da się tak zrobić?

Mój kod:

 
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace A
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        int screen;
        double a;
        double b;
        double c;
        double result = 0;
        private void Dodawanie_Click(object sender, RoutedEventArgs e)
        {
            b = double.Parse(TXB_2.Text);
            result += b;
            TXB_1.Text = (result.ToString());
            TXB_2.Text = "";
        }

        private void Odejmowanie_Click(object sender, RoutedEventArgs e)
        {
            b = double.Parse(TXB_2.Text);
            result -= b;
            TXB_1.Text = (result.ToString());
            TXB_2.Text = "";
        }

        private void Usuwanie_Click(object sender, RoutedEventArgs e)
        {
            TXB_1.Text = "";
        }

        private void B_1_Click(object sender, RoutedEventArgs e)
        {
            TXB_2.Text = TXB_2.Text + "1";
        }

        private void B_2_Click(object sender, RoutedEventArgs e)
        {
            TXB_2.Text = TXB_2.Text + "2";
        }

        private void B3_Click(object sender, RoutedEventArgs e)
        {
            TXB_2.Text = TXB_2.Text + "3";
        }

        private void B4_Click(object sender, RoutedEventArgs e)
        {
            TXB_2.Text = TXB_2.Text + "4";
        }

        private void B5_Click(object sender, RoutedEventArgs e)
        {
            TXB_2.Text = TXB_2.Text + "5";
        }

        private void B6_Click(object sender, RoutedEventArgs e)
        {
            TXB_2.Text = TXB_2.Text + "6";
        }

        private void B7_Click(object sender, RoutedEventArgs e)
        {
            TXB_2.Text = TXB_2.Text + "7";
        }

        private void B8_Click(object sender, RoutedEventArgs e)
        {
            TXB_2.Text = TXB_2.Text + "8";
        }

        private void B9_Click(object sender, RoutedEventArgs e)
        {
            TXB_2.Text = TXB_2.Text + "9";
        }

        private void B0_Click(object sender, RoutedEventArgs e)
        {
            TXB_2.Text = TXB_2.Text + "0";
        }
    }
}

using System;

<image>foo</image>d27b62dba6.png

0

Tak. Możesz to zapisać w ustawieniach

Project -> A Proporties i zakładka settings, ustawiasz co potrzebujesz

Możesz stworzyć metodę w klasie, która będzie zapisywać ustawienia

public static void SaveSettings(double result)
{
Properties.Settings sett = Properties.Settings.Default;
sett.res = result
sett.Save();
}

i analogicznie odczytującą

Więcej na ten temat : https://msdn.microsoft.com/en-us/library/aa730869(v=vs.80).aspx

Czy nie lepiej stworzyć jedno zdarzenie do wszystkich buttonów?? :D

0

@Nomysz
Co robić?
Wyskoczył mi taki błąd:
1ae2447fff.png

0

Przeczytać artykuł który był podlinkowany...

0

Mam nowy problem.
Problem zaczyna się gdy włączę ponownie program. (W TXB_1 wyświetla się poprzedni wynik, więc pół biedy), ale gdy napiszę np.5 i kliknę "+" bądź "-" program kasuje poprzedni wynik i wstawia 5. Powinno być tak, że jeżeli ostatni wynik wynosi 10, to po kliknięciu "5", następnie "+" powinno być wyświetlone "15".
Później już wszystko idzie normalnie, ale zależy mi na ciągłości.
kod:

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace skarbonka
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        double a; //liczba wpisana przez nas       
        double result = 0;
        private void Dodawanie_Click(object sender, RoutedEventArgs e)
        {
            a = double.Parse(TXB_2.Text);
            result += a;
            TXB_1.Text = (result.ToString());
            TXB_2.Text = "";
        }

        private void Odejmowanie_Click(object sender, RoutedEventArgs e)
        {
            a = double.Parse(TXB_2.Text);
            result -= a;
            TXB_1.Text = (result.ToString());
            TXB_2.Text = "";
        }

        private void Usuwanie_Click(object sender, RoutedEventArgs e)
        {
            TXB_1.Text = "";
        }

        private void B_1_Click(object sender, RoutedEventArgs e)
        {
            TXB_2.Text = TXB_2.Text + "1";
        }

        private void B_2_Click(object sender, RoutedEventArgs e)
        {
            TXB_2.Text = TXB_2.Text + "2";
        }

        private void B3_Click(object sender, RoutedEventArgs e)
        {
            TXB_2.Text = TXB_2.Text + "3";
        }

        private void B4_Click(object sender, RoutedEventArgs e)
        {
            TXB_2.Text = TXB_2.Text + "4";
        }

        private void B5_Click(object sender, RoutedEventArgs e)
        {
            TXB_2.Text = TXB_2.Text + "5";
        }

        private void B6_Click(object sender, RoutedEventArgs e)
        {
            TXB_2.Text = TXB_2.Text + "6";
        }

        private void B7_Click(object sender, RoutedEventArgs e)
        {
            TXB_2.Text = TXB_2.Text + "7";
        }

        private void B8_Click(object sender, RoutedEventArgs e)
        {
            TXB_2.Text = TXB_2.Text + "8";
        }

        private void B9_Click(object sender, RoutedEventArgs e)
        {
            TXB_1.Text = TXB_1.Text + "9";
        }

        private void B0_Click(object sender, RoutedEventArgs e)
        {
            TXB_2.Text = TXB_2.Text + "0";
        }

        private void skarbonka_Closed(object sender, System.EventArgs e)
        {
            TXB_1.Text = Properties.Settings.Default.textbox;
            Properties.Settings.Default.Save();
        }

        private void skarbonka_Load(object sender, RoutedEventArgs e)
        {
            TXB_1.Text = Properties.Settings.Default.textbox;
                                                                                         //wydaje mi się że tutaj coś powinno być
        }
    }
}
 

screeny:
7de07fbd97.png
a65d551f66.png

1 użytkowników online, w tym zalogowanych: 0, gości: 1