Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Разработка Windows Forms > Переход на другую форму через кнопку


Автор: Nordway 14.10.2010, 17:14
Добрый день! Недавно начал работать с Visual Studio C++ 2008. В данный момент осваиваю формы и задался целью сделать приложение с небольшим меню, в котором пользователь по кнопочкам будет открывать разные формы.

Например, как меню на сайтах, в которых каждая ссылка ведёт на свою страницу, только всё это в приложении.

Я разобрался как с одной формы (Form1) открывать другую форму (Auth). Project - CLR - Windows Forms Application.
Мой код:

Листинг Form1.h
Код

#pragma once
#include "Auth.h"

namespace Window_Test {

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;

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

    protected:
        ~Form1()
        {
            if (components)
            {
                delete components;
            }
        }
    private: System::Windows::Forms::Button^  button1;
    protected: 

    private:
        System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code

        void InitializeComponent(void)
        {
            this->button1 = (gcnew System::Windows::Forms::Button());
            this->SuspendLayout();
            // 
            // button1
            // 
            this->button1->Location = System::Drawing::Point(109, 102);
            this->button1->Name = L"button1";
            this->button1->Size = System::Drawing::Size(75, 48);
            this->button1->TabIndex = 0;
            this->button1->Text = L"Перейти к форме входа";
            this->button1->UseVisualStyleBackColor = true;
            this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
            // 
            // Form1
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(292, 266);
            this->Controls->Add(this->button1);
            this->Name = L"Form1";
            this->Text = L"Главная";
            this->ResumeLayout(false);

        }
#pragma endregion
    private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
                 Auth ^auth = gcnew Auth();
                 auth->Show();
                 this->Hide();
             }
    };
}


Листинг Auth.h
Код

#pragma once

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;


namespace Window_Test {

    /// <summary>
    /// Summary for Auth
    ///
    /// WARNING: If you change the name of this class, you will need to change the
    ///          'Resource File Name' property for the managed resource compiler tool
    ///          associated with all .resx files this class depends on.  Otherwise,
    ///          the designers will not be able to interact properly with localized
    ///          resources associated with this form.
    /// </summary>
    public ref class Auth : public System::Windows::Forms::Form
    {
    public:
        Auth(void)
        {
            InitializeComponent();
            //
            //TODO: Add the constructor code here
            //
        }

    protected:
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        ~Auth()
        {
            if (components)
            {
                delete components;
            }
        }
    private: System::Windows::Forms::TextBox^  textBox1;
    protected: 
    private: System::Windows::Forms::TextBox^  textBox2;
    private: System::Windows::Forms::Label^  label1;
    private: System::Windows::Forms::Label^  label2;
    private: System::Windows::Forms::Button^  button1;
    private: System::Windows::Forms::Button^  button2;

    private:
        /// <summary>
        /// Required designer variable.
        /// </summary>
        System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
                void InitializeComponent(void)
                {
                        this->button1 = (gcnew System::Windows::Forms::Button());
                        this->SuspendLayout();
                        // 
                        // button1
                        // 
                        this->button1->Location = System::Drawing::Point(86, 50);
                        this->button1->Name = L"button1";
                        this->button1->Size = System::Drawing::Size(75, 23);
                        this->button1->TabIndex = 0;
                        this->button1->Text = L"button1";
                        this->button1->UseVisualStyleBackColor = true;
                        this->button1->Click += gcnew System::EventHandler(this, &Auth::button1_Click);
                        // 
                        // Auth
                        // 
                        this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
                        this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
                        this->ClientSize = System::Drawing::Size(292, 268);
                        this->Controls->Add(this->button1);
                        this->Name = L"Auth";
                        this->Text = L"Auth";
                        this->ResumeLayout(false);
 
                }
#pragma endregion
        private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
                     Application::Exit();
                         }
        };
}


А теперь вопрос: как заставить открывающуюся форму открываться в том же окне, т.е. добиться такого же эффекта как открывается новая старница в браузере по ссылке? (сейчас: 2-ая форма появляется просто рядом)

Автор: Экскалупатор 14.10.2010, 21:11
а при чем тут .net?

Автор: Nordway 14.10.2010, 22:02
Цитата(Экскалупатор @ 14.10.2010,  21:11)
а при чем тут .net?

Увидел "Разработка Windows Forms", а это как раз то с чем работаю. Если не тот раздел, то переместите в нужный.

По делу будут какие-нибудь предложения?

Автор: Экскалупатор 14.10.2010, 22:28
Цитата(Nordway @  14.10.2010,  16:14 Найти цитируемый пост)
А теперь вопрос: как заставить открывающуюся форму открываться в том же окне, т.е. добиться такого же эффекта как открывается новая старница в браузере по ссылке? (сейчас: 2-ая форма появляется просто рядом) 

страница в браузере открывается как tabpage т.е. это по сути то же самое окно, но с дополнительной панелькой. а если надо доп форму то можно наверное почитать про MDI Form. можно к примеру http://msdn.microsoft.com/ru-ru/library/xyhh2e7e.aspx посмотреть.

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)