How to link Buttons in C++.NET?
Example:
If I want to link "Button1" which is in "Form1" to "Form2", like if you click "Button1" in "Form1", the program will goto "Form2". What source code should I input?

Recommended Answers

All 3 Replies

Handle the click event for both buttons and in the event handler call the BringToFront method of the form you want to go to. I don't know your form setup, so I can't offer any more advice than that.

I need to link the "Button1" from "Form1" to "Form2"

Form1:

#pragma once

namespace Botz0004 {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Summary for Form1
///
/// 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 Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::MaskedTextBox^ maskedTextBox1;
private: System::Windows::Forms::Button^ button1;
protected:
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->label1 = (gcnew System::Windows::Forms::Label());
this->maskedTextBox1 = (gcnew System::Windows::Forms::MaskedTextBox());
this->button1 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// label1
//
this->label1->AutoSize = true;
this->label1->FlatStyle = System::Windows::Forms::FlatStyle::Popup;
this->label1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 9.75F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->label1->Location = System::Drawing::Point(73, 59);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(147, 16);
this->label1->TabIndex = 0;
this->label1->Text = L"What is your name\? ";
this->label1->TextAlign = System::Drawing::ContentAlignment::MiddleCenter;
//
// maskedTextBox1
//
this->maskedTextBox1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->maskedTextBox1->Location = System::Drawing::Point(30, 127);
this->maskedTextBox1->Name = L"maskedTextBox1";
this->maskedTextBox1->Size = System::Drawing::Size(233, 20);
this->maskedTextBox1->TabIndex = 1;
this->maskedTextBox1->TextAlign = System::Windows::Forms::HorizontalAlignment::Center;
//
// button1
//
this->button1->Location = System::Drawing::Point(105, 179);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(84, 23);
this->button1->TabIndex = 2;
this->button1->Text = L"Next";
this->button1->UseVisualStyleBackColor = true;
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(292, 270);
this->Controls->Add(this->button1);
this->Controls->Add(this->maskedTextBox1);
this->Controls->Add(this->label1);
this->Name = L"Form1";
this->Text = L"Botz0004";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
};
}

Form2:

#pragmaonce

namespace Botz0004 {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>

/// Summary for Form1

///

/// 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 Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//

//TODO: Add the constructor code here

//

}
protected:
/// <summary>

/// Clean up any resources being used.

/// </summary>

~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::MaskedTextBox^ maskedTextBox1;
private: System::Windows::Forms::Button^ button1;
protected:
private:
/// <summary>

/// Required designer variable.

/// </summary>

System::ComponentModel::Container ^components;

#pragmaregion Windows Form Designer generated code
/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

void InitializeComponent(void)
{
this->label1 = (gcnew System::Windows::Forms::Label());
this->maskedTextBox1 = (gcnew System::Windows::Forms::MaskedTextBox());
this->button1 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//

// label1

//

this->label1->AutoSize = true;
this->label1->FlatStyle = System::Windows::Forms::FlatStyle::Popup;
this->label1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 9.75F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->label1->Location = System::Drawing::Point(73, 59);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(147, 16);
this->label1->TabIndex = 0;
this->label1->Text = L"What is your name\? ";
this->label1->TextAlign = System::Drawing::ContentAlignment::MiddleCenter;
//

// maskedTextBox1

//

this->maskedTextBox1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->maskedTextBox1->Location = System::Drawing::Point(30, 127);
this->maskedTextBox1->Name = L"maskedTextBox1";
this->maskedTextBox1->Size = System::Drawing::Size(233, 20);
this->maskedTextBox1->TabIndex = 1;
this->maskedTextBox1->TextAlign = System::Windows::Forms::HorizontalAlignment::Center;
//

// button1

//

this->button1->Location = System::Drawing::Point(105, 179);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(84, 23);
this->button1->TabIndex = 2;
this->button1->Text = L"Next";
this->button1->UseVisualStyleBackColor = true;
//

// Form1

//

this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(292, 270);
this->Controls->Add(this->button1);
this->Controls->Add(this->maskedTextBox1);
this->Controls->Add(this->label1);
this->Name = L"Form1";
this->Text = L"Botz0004";
this->ResumeLayout(false);
this->PerformLayout();
}

#pragmaendregion

};
}

Help will be appreciated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.