Загрузка данных


#pragma once

namespace PasswordApp {

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Windows::Forms;
    using namespace System::Drawing;
    using namespace System::IO;

    public ref class Form1 : public System::Windows::Forms::Form
    {
    public:
        Form1(void)
        {
            InitializeComponent();

            attempts = 0;
            savedPassword = "1234";
        }

    protected:
        ~Form1()
        {
            if (components)
            {
                delete components;
            }
        }

    private:
        System::Windows::Forms::TextBox^ textBox1;
        System::Windows::Forms::Button^ button1;

        int attempts;
        String^ savedPassword;

        System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
        void InitializeComponent(void)
        {
            this->textBox1 = (gcnew System::Windows::Forms::TextBox());
            this->button1 = (gcnew System::Windows::Forms::Button());

            this->SuspendLayout();

            // textBox1
            this->textBox1->Location = System::Drawing::Point(40, 30);
            this->textBox1->PasswordChar = '*';
            this->textBox1->Size = System::Drawing::Size(120, 20);

            // button1
            this->button1->Location = System::Drawing::Point(40, 70);
            this->button1->Size = System::Drawing::Size(120, 30);
            this->button1->Text = L"Войти";
            this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);

            // Form
            this->ClientSize = System::Drawing::Size(200, 130);
            this->Controls->Add(this->textBox1);
            this->Controls->Add(this->button1);
            this->Text = L"Введите пароль";

            this->ResumeLayout(false);
            this->PerformLayout();
        }
#pragma endregion

    private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
    {
        if (textBox1->Text == savedPassword)
        {
            MessageBox::Show("Пароль верный");

            // новое окно
            Form^ f = gcnew Form();
            f->Text = "Заставка программы";
            f->Size = Drawing::Size(250,150);

            TextBox^ tb = gcnew TextBox();
            tb->Location = Drawing::Point(30, 60);
            tb->Size = Drawing::Size(150, 20);

            Button^ btn = gcnew Button();
            btn->Text = "Сменить пароль";
            btn->Location = Drawing::Point(30, 20);

            // сохраняем TextBox внутри кнопки
            btn->Tag = tb;

            btn->Click += gcnew EventHandler(this, &Form1::ChangePassword);

            f->Controls->Add(tb);
            f->Controls->Add(btn);

            f->Show();
            this->Hide();
        }
        else
        {
            attempts++;

            MessageBox::Show("Неверный пароль");

            if (attempts >= 3)
            {
                MessageBox::Show("Программа закрывается");
                Application::Exit();
            }
        }
    }

    private: System::Void ChangePassword(System::Object^ sender, System::EventArgs^ e)
    {
        Button^ btn = (Button^)sender;
        TextBox^ tb = (TextBox^)btn->Tag;

        savedPassword = tb->Text;

        File::WriteAllText("pass.txt", savedPassword);

        MessageBox::Show("Пароль изменён");
    }
    };
}