Create a button in c++

madinino Posted messages 12 Registration date   Status Member Last intervention   -  
KX Posted messages 19031 Status Moderator -
Hello, I created an application with Visual C++, I created buttons that will allow me to close the application, open a window while keeping the main window active, open a window while closing the main window, but the problem is that I do not know the code to insert in this part:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
}
};

I'm also looking to display the results of a query in a DataGrid, but I don't know how to do it, thank you for your help because I've been looking but I can't find anything.

9 answers

KX Posted messages 19031 Status Moderator 3 020
 
It depends, what should Button1 do specifically?
For example, to close the window, the code is: this->Close();

However, you cannot close the main window without stopping the program.
As a result, you cannot "open a window while closing the main window", however you can hide it with: this->Visible=false;
Trust does not exclude control.
0
KX Posted messages 19031 Status Moderator 3 020
 
Note: Windows Forms is no longer quite C++; we are rather talking about C++/CLI.
0
madinino Posted messages 12 Registration date   Status Member Last intervention  
 
Merci beaucoup, peux-tu me donner le script pour ouvrir une fenêtre s'il te plaît ?
0
KX Posted messages 19031 Status Moderator 3 020
 
For example, if you created the Form2 class which represents the window you want to open with button 1 of Form1, then your function will be:

#include "Form2.h" private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { Form2^ window = gcnew Form2(); // create a window window->Visible=true; // display it }

Note: in C++ we talk about code and not script!
--
Confidence does not exclude control.
0
madinino Posted messages 12 Registration date   Status Member Last intervention  
 
Thank you for the KX code! ;)
0
madinino Posted messages 12 Registration date   Status Member Last intervention  
 
```cpp
#pragma once
#include "Form3.h"

namespace truc {
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>
/// Description résumée de Form1
///
/// AVERTISSEMENT : si vous modifiez le nom de cette classe, vous devrez modifier la
/// propriété 'Nom du fichier de ressources' de l'outil de compilation de ressource managée
/// pour tous les fichiers .resx dont dépend cette classe. Dans le cas contraire,
/// les concepteurs ne pourront pas interagir correctement avec les ressources
/// localisées associées à ce formulaire.
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form {
public:
Form1(void) {
InitializeComponent();
// //TODO : ajoutez ici le code du constructeur
}

protected:
/// <summary>
/// Nettoyage des ressources utilisées.
/// </summary>
~Form1() {
if (components) {
delete components;
}
}

private:
System::Windows::Forms::Button^ button1;
System::Windows::Forms::Button^ button2;
protected:
private:
/// <summary>
/// Variable nécessaire au concepteur.
/// </summary>
System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
/// <summary>
/// Méthode requise pour la prise en charge du concepteur - ne modifiez pas
/// le contenu de cette méthode avec l'éditeur de code.
/// </summary>
void InitializeComponent(void) {
this->button1 = (gcnew System::Windows::Forms::Button());
this->button2 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(150, 46);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(81, 33);
this->button1->TabIndex = 0;
this->button1->Text = L"Fermer";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// button2
//
this->button2->Location = System::Drawing::Point(150, 115);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(80, 34);
this->button2->TabIndex = 1;
this->button2->Text = L"Form3";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 264);
this->Controls->Add(this->button2);
this->Controls->Add(this->button1);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);
}
#pragma endregion

private:
System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
this->Close();
}

private:
System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
Form3^ fenetre = gcnew Form3(); // on créé une fenêtre
fenetre->Show(); // on l'affiche
}
};
}
```
0
madinino2
 
What is Form3.h ????
0
KX Posted messages 19031 Status Moderator 3 020
 
Form3.h is the file that defines the window that should open when button2 is clicked in the Form1 window (these are the very last lines of code):

private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) { Form3^ window = gcnew Form3(); // we create a window window->Visible=true; // we display it }

Note: this issue has been resolved for several months ;-)
0
madinino Posted messages 12 Registration date   Status Member Last intervention  
 
I misplaced the brace in the program, it's good now, thanks, sorry.
0
madinino Posted messages 12 Registration date   Status Member Last intervention  
 
Do you know a good tutorial that explains how to perform queries in an Access database, or can you give me the procedure to follow please? I would like to be able to delete, add, and display data from the database.
If I manage to get through this step, I can say that half the work is already done, that would be super cool.
0
KX Posted messages 19031 Status Moderator 3 020
 
No sorry, I've never used Access.
0
madinino Posted messages 12 Registration date   Status Member Last intervention  
 
What do you use as a base for a program in C++?
0
KX Posted messages 19031 Status Moderator 3 020
 
I have never programmed a database in C++
I used MySQL but it was Java EE programming.
0
madinino Posted messages 12 Registration date   Status Member Last intervention  
 
And where do you get the information you've given me? Not to be intrusive.
0
KX Posted messages 19031 Status Moderator 3 020
 
Even though I have never used databases in C++, that doesn't stop me from knowing how to use Visual Studio to create other types of applications...
Moreover, what I gave you is the basics! If I didn't know how to create a button or display a form, it means I would have never really worked with Windows Forms!
0
madinino Posted messages 12 Registration date   Status Member Last intervention  
 
Ok, well this is actually my very first app, thanks again for the info you gave me.
0