-
September 7th, 2009, 12:41 AM
#1
C++ Windows forms with .cpp
I am still trying to learn how to make windows form applications in visual studio. I have created a very basic windows forms application. But, I am not sure how to call my functions from the header to use for when the button is pushed. There seems to be very little reference tutorials about using the windows form application in visual studio.
As you can see I have a very simple window with a button named "button1" and a label named "label1".
What I want to do is when you click the button it calls the function which will add die1 and die2 together in my random_dice_wfa.cpp part of my code. Which will then return the total dice roll number to be displayed in the label as the answer.
Form1.h
Code:
#pragma once
namespace random_dice_wfa {
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::Button^ button1;
protected:
private: System::Windows::Forms::Label^ label1;
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->button1 = (gcnew System::Windows::Forms::Button());
this->label1 = (gcnew System::Windows::Forms::Label());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(13, 13);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"Roll Dice";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location = System::Drawing::Point(13, 43);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(67, 13);
this->label1->TabIndex = 1;
this->label1->Text = L"[ Dice Data ]";
//
// 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->label1);
this->Controls->Add(this->button1);
this->Name = L"Form1";
this->Text = L"Random Dice";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
int die1 = 5;
int die2 = 2;
int answer = mathadd(die1,die2);
label1->Text = String::Format(L"You rolled: {0} and {1} | Total = {2}", die1, die2, answer);
}
};
}
random_dice_wfa.cpp
Code:
// random_dice_wfa.cpp : main project file.
#include "stdafx.h"
#include "Form1.h"
using namespace random_dice_wfa;
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
// Create the main window and run it
Application::Run(gcnew Form1());
return 0;
}
// Dice adding math
int mathadd(int die1, int die2){
int answer = die1 + die2;
return answer;
}
Last edited by Riven; September 7th, 2009 at 12:58 AM.
-
September 7th, 2009, 02:19 AM
#2
Re: C++ Windows forms with .cpp
Creare file GlobalFunctions.h and put there all global functions prototypes:
Code:
// GlobalFunctions.h
#pragma once
int mathadd(int die1, int die2);
Include GlobalFunctions.h to Form1.h, and you can call mathadd from Form1.
-
September 7th, 2009, 02:42 PM
#3
Re: C++ Windows forms with .cpp
Sweet it worked, that's amazing. Your amazing.
So, ah can you explain to me why the function needs to be in the header file to read the random_dice_wfa.cpp functions? What was I doing wrong when I tried to just #include "random_dice_wfa.cpp" in Form1.h?
-
September 8th, 2009, 12:46 AM
#4
Re: C++ Windows forms with .cpp
C++ compiler needs to know functopn prototype to compile function call. GlobalFunctions.h
contains mathadd function prototype. This is enough for compiler to compiler successfully Form1.cpp. mathadd is marked as external reference in Form1.obj, because function implementation is missing.
Compiling random_dice_wfa.cpp, compiler finds actual mathadd implementation.
Finally, linker links Form1.cpp and random_dice_wfa.cpp. At this stage it resolves external mathadd reference.
-
September 9th, 2009, 08:04 AM
#5
Re: C++ Windows forms with .cpp
Hi
This is similiar to my problem ("all the code in the form1.h")
When I'd rather have functions under the buttons Rather than code Eg.
Code:
private: System::Void checkBox1_CheckedChanged(System::Object^ sender, System::EventArgs^ e)
{
DoTheCode();
}
I've had trouble connecting header files , I wanted to ask in random_dice_wfa.cpp you have
initailised the variables, under the button code you have initalised the same variables and I guess in Global you have initialised them too ...
Do you have to do this? I thought if you "int something" in globals you dont need to 'int it' anywhwere else as its already declared...
Maybe this is why mines dont work !
Thanks for your Time
:>
in
Last edited by Barnabus; September 9th, 2009 at 08:12 AM.
-
September 13th, 2009, 01:59 AM
#6
Re: C++ Windows forms with .cpp
I wanted to ask in random_dice_wfa.cpp you have
initailised the variables, under the button code you have initalised the same variables and I guess in Global you have initialised them too
Which ones are you talking about? The ones in the function prototype? The function expects the type argument to be on the stack. If the prototype is omitted then it might go on to some other part of the stack or something else not in scope. When you include the function prototype your are telling the compiler that the function should take whatever type and it give it a road map to navigate past those scope errors.
Are you talking about something or was that what you were talking about?
Last edited by Riven; September 13th, 2009 at 02:03 AM.
-
July 25th, 2019, 10:34 AM
#7
Re: C++ Windows forms with .cpp
Hi; I had the same problem and your solution almost worked. But I need to pass and return a 2D array (matrix) to a function in such a situation and have problem with returning matrix! Could you please guide me about that?
I have in MyForm.h
________________
Code:
.
.
.
#include"GlobFcn.h"
.
.
.
private: System::Void Analyze_Click(System::Object^ sender, System::EventArgs^ e) {
...
int Max_data = 0
std::vector<std::vector<double>> mat_data0(size_r, std::vector<double>(size_c));
std::vector<std::vector<double>> mat_data(size_r, std::vector<double>(size_c));
...
(calculating new values for Max_data and mat_data0)
...
mat_data=matNormalize(mat_data0,Max_data); //matNormalize is my function..........here I have "no instance of function template "matNormalize" matches the argument list" massage!
________
Also, I have MyForm.cpp include:
Code:
#include "MyForm.h"
#include"GlobFcn.h"
...
template <size_t row_size, size_t clmn_size>
float mat_Normalize(double (X)[row_size][clmn_size], int Max_data) {
//std::vector<std::vector<double>> mat_data;
for (int r = 0; r < row_size; ++r) {
for (int c = 0; c < clmn_size; ++c) {
mat_data[r][c] =X[r][c]/ Max_data;
//cout << mat_data[r][c] << endl;
}
}
return mat_data;
}
_____________
And GlobFcn.h as:
Code:
#pragma once
template <size_t row_size, size_t clmn_size>
float** matNormalize(double(X)[row_size][clmn_size], int Max_data);
Could you please help me to solve the problem!
Last edited by 2kaud; July 25th, 2019 at 11:17 AM.
Reason: Added code tags
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|