C++/CLI : how to call a function from a user control?

Trips

Well-known member
Joined
Aug 7, 2010
Messages
2,788
Hello,

I am totally novice in programming with C++/CLI and not very well trained in object oriented programming.
I have started to write a C++/CLI application to control a small industrial type installation.
Some buttons of my HMI are used to close or open valves.
For that purpose, I have put buttons on which the user can click to perform the appropriate action.
Each of the buttons call a function Valve_move (int i, int j); the first parameter is used to specify the action (close or open) and the second one to specify the valve number to be moved.
Everything was working well until I decided to write special User Control .
Introducing this User Control , I get now the following message from the compiler :
"error C3861: Valve_move : identificateur introuvable" ; sorry I am French and my Visual C++ speaks French !!
It means that it cannot find my function Valve_move(i,j) anymore.
The Valve_move function is part of the Form1.h file and is working perfectly when called from a simple button put in Form1.h
Then it seems that calling the function from a User Control is not as simple as calling it from a normal button.
I saw several topics close to that but none was dedicated to C++/CLI ou clear enough for me to guide me in solving the problem.
In order to highlight the problem I wrote a very simple program my_control with Form1.h which includes my User Control as weel as the Valve_move function.
The User Control is very simple and includes two buttons "Close" and "Open" and a numericUpDown to select the valves

Here is the code of Form1.h

<div style="color:Black; background-color:White
<pre>#include <span style="color:#a31515 "my_panel.h"

#pragma once


<span style="color:Blue namespace
my_control {

<span style="color:Blue using
<span style="color:Blue namespace
System;
<span style="color:Blue using
<span style="color:Blue namespace
System::ComponentModel;
<span style="color:Blue using
<span style="color:Blue namespace
System::Collections;
<span style="color:Blue using
<span style="color:Blue namespace
System::Windows::Forms;
<span style="color:Blue using
<span style="color:Blue namespace
System::Data;
<span style="color:Blue using
<span style="color:Blue namespace
System::Drawing;

<span style="color:Green /// <summary>

<span style="color:Green /// Description rÃsumÃe de Form1

<span style="color:Green ///

<span style="color:Green /// AVERTISSEMENT : si vous modifiez le nom de cette classe, vous devrez modifier la

<span style="color:Green /// propriÃtà Nom du fichier de ressources de loutil de compilation de ressource managÃe

<span style="color:Green /// pour tous les fichiers .resx dont dÃpend cette classe. Dans le cas contraire,

<span style="color:Green /// les concepteurs ne pourront pas interagir correctement avec les ressources

<span style="color:Green /// localisÃes associÃes à ce formulaire.

<span style="color:Green /// </summary>

<span style="color:Blue public
<span style="color:Blue ref class
Form1 : <span style="color:Blue public
System::Windows::Forms::Form
{
<span style="color:Blue public
:
Form1(<span style="color:Blue void
)
{
InitializeComponent();
<span style="color:Green //

<span style="color:Green //TODO : ajoutez ici le code du constructeur

<span style="color:Green //

}

<span style="color:Blue protected
:
<span style="color:Green /// <summary>

<span style="color:Green /// Nettoyage des ressources utilisÃes.

<span style="color:Green /// </summary>

~Form1()
{
<span style="color:Blue if
(components)
{
<span style="color:Blue delete
components;
}
}
<span style="color:Blue private
: my_control::my_panel^ my_panel1;
<span style="color:Blue protected
:

<span style="color:Blue private
:
<span style="color:Green /// <summary>

<span style="color:Green /// Variable nÃcessaire au concepteur.

<span style="color:Green /// </summary>

System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
<span style="color:Green /// <summary>

<span style="color:Green /// MÃthode requise pour la prise en charge du concepteur - ne modifiez pas

<span style="color:Green /// le contenu de cette mÃthode avec lÃditeur de code.

<span style="color:Green /// </summary>

<span style="color:Blue void
InitializeComponent(<span style="color:Blue void
)
{
<span style="color:Blue this
->my_panel1 = (<span style="color:Blue gcnew
my_control::my_panel());
<span style="color:Blue this
->SuspendLayout();
<span style="color:Green //

<span style="color:Green // my_panel1

<span style="color:Green //

<span style="color:Blue this
->my_panel1->Location = System::Drawing::Point(12, 27);
<span style="color:Blue this
->my_panel1->Name = L<span style="color:#a31515 "my_panel1"
;
<span style="color:Blue this
->my_panel1->Size = System::Drawing::Size(262, 150);
<span style="color:Blue this
->my_panel1->TabIndex = 0;
<span style="color:Green //

<span style="color:Green // Form1

<span style="color:Green //

<span style="color:Blue this
->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
<span style="color:Blue this
->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
<span style="color:Blue this
->ClientSize = System::Drawing::Size(292, 266);
<span style="color:Blue this
->Controls->Add(<span style="color:Blue this
->my_panel1);
<span style="color:Blue this
->Name = L<span style="color:#a31515 "Form1"
;
<span style="color:Blue this
->Text = L<span style="color:#a31515 "Form1"
;
<span style="color:Blue this
->ResumeLayout(<span style="color:Blue false
);

}
#pragma endregion

<span style="color:Blue void
Valve_move (<span style="color:Blue int
i_function, <span style="color:Blue int
i_valve)
{
<span style="color:Blue if
(i_function == 0)
{
<span style="color:Green // put here the code to close the valve //

}
<span style="color:Blue else
<span style="color:Blue if
(i_function == 1)
{
<span style="color:Green // put here the code to open the valve //

}
}

};
}


[/code]


And here is the code of the User Control my_panel.h


<div style="color:Black; background-color:White
<pre>#pragma once

<span style="color:Blue using
<span style="color:Blue namespace
System;
<span style="color:Blue using
<span style="color:Blue namespace
System::ComponentModel;
<span style="color:Blue using
<span style="color:Blue namespace
System::Collections;
<span style="color:Blue using
<span style="color:Blue namespace
System::Windows::Forms;
<span style="color:Blue using
<span style="color:Blue namespace
System::Data;
<span style="color:Blue using
<span style="color:Blue namespace
System::Drawing;


<span style="color:Blue namespace
my_control {

<span style="color:Green /// <summary>

<span style="color:Green /// Description rÃsumÃe de my_panel

<span style="color:Green /// </summary>

<span style="color:Blue public
<span style="color:Blue ref class
my_panel : <span style="color:Blue public
System::Windows::Forms::UserControl
{
<span style="color:Blue public
:
my_panel(<span style="color:Blue void
)
{
InitializeComponent();
<span style="color:Green //

<span style="color:Green //TODO : ajoutez ici le code du constructeur

<span style="color:Green //

}

<span style="color:Blue protected
:
<span style="color:Green /// <summary>

<span style="color:Green /// Nettoyage des ressources utilisÃes.

<span style="color:Green /// </summary>

~my_panel()
{
<span style="color:Blue if
(components)
{
<span style="color:Blue delete
components;
}
}
<span style="color:Blue private
: System::Windows::Forms::GroupBox^ groupBox1;
<span style="color:Blue protected
:
<span style="color:Blue private
: System::Windows::Forms::NumericUpDown^ numericUpDown1;
<span style="color:Blue private
: System::Windows::Forms::Button^ button2;
<span style="color:Blue private
: System::Windows::Forms::Button^ button1;

<span style="color:Blue private
:
<span style="color:Green /// <summary>

<span style="color:Green /// Variable nÃcessaire au concepteur.

<span style="color:Green /// </summary>

System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
<span style="color:Green /// <summary>

<span style="color:Green /// MÃthode requise pour la prise en charge du concepteur - ne modifiez pas

<span style="color:Green /// le contenu de cette mÃthode avec lÃditeur de code.

<span style="color:Green /// </summary>

<span style="color:Blue void
InitializeComponent(<span style="color:Blue void
)
{
<span style="color:Blue this
->groupBox1 = (<span style="color:Blue gcnew
System::Windows::Forms::GroupBox());
<span style="color:Blue this
->numericUpDown1 = (<span style="color:Blue gcnew
System::Windows::Forms::NumericUpDown());
<span style="color:Blue this
->button2 = (<span style="color:Blue gcnew
System::Windows::Forms::Button());
<span style="color:Blue this
->button1 = (<span style="color:Blue gcnew
System::Windows::Forms::Button());
<span style="color:Blue this
->groupBox1->SuspendLayout();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(<span style="color:Blue this
->numericUpDown1))->BeginInit();
<span style="color:Blue this
->SuspendLayout();
<span style="color:Green //

<span style="color:Green // groupBox1

<span style="color:Green //

<span style="color:Blue this
->groupBox1->BackColor = System::Drawing::SystemColors::GradientInactiveCaption;
<span style="color:Blue this
->groupBox1->Controls->Add(<span style="color:Blue this
->numericUpDown1);
<span style="color:Blue this
->groupBox1->Controls->Add(<span style="color:Blue this
->button2);
<span style="color:Blue this
->groupBox1->Controls->Add(<span style="color:Blue this
->button1);
<span style="color:Blue this
->groupBox1->Location = System::Drawing::Point(17, 12);
<span style="color:Blue this
->groupBox1->Name = L<span style="color:#a31515 "groupBox1"
;
<span style="color:Blue this
->groupBox1->Size = System::Drawing::Size(225, 125);
<span style="color:Blue this
->groupBox1->TabIndex = 0;
<span style="color:Blue this
->groupBox1->TabStop = <span style="color:Blue false
;
<span style="color:Blue this
->groupBox1->Text = L<span style="color:#a31515 "groupBox1"
;
<span style="color:Green //

<span style="color:Green // numericUpDown1

<span style="color:Green //

<span style="color:Blue this
->numericUpDown1->Location = System::Drawing::Point(29, 37);
<span style="color:Blue this
->numericUpDown1->Maximum = System::Decimal(<span style="color:Blue gcnew
cli::<span style="color:Blue array
< System::Int32 >(4) {20, 0, 0, 0});
<span style="color:Blue this
->numericUpDown1->Minimum = System::Decimal(<span style="color:Blue gcnew
cli::<span style="color:Blue array
< System::Int32 >(4) {1, 0, 0, 0});
<span style="color:Blue this
->numericUpDown1->Name = L<span style="color:#a31515 "numericUpDown1"
;
<span style="color:Blue this
->numericUpDown1->Size = System::Drawing::Size(50, 20);
<span style="color:Blue this
->numericUpDown1->TabIndex = 2;
<span style="color:Blue this
->numericUpDown1->Value = System::Decimal(<span style="color:Blue gcnew
cli::<span style="color:Blue array
< System::Int32 >(4) {1, 0, 0, 0});
<span style="color:Green //

<span style="color:Green // button2

<span style="color:Green //

<span style="color:Blue this
->button2->BackColor = System::Drawing::Color::Red;
<span style="color:Blue this
->button2->Location = System::Drawing::Point(133, 70);
<span style="color:Blue this
->button2->Name = L<span style="color:#a31515 "button2"
;
<span style="color:Blue this
->button2->Size = System::Drawing::Size(75, 23);
<span style="color:Blue this
->button2->TabIndex = 1;
<span style="color:Blue this
->button2->Text = L<span style="color:#a31515 "Close"
;
<span style="color:Blue this
->button2->UseVisualStyleBackColor = <span style="color:Blue false
;
<span style="color:Blue this
->button2->Click += <span style="color:Blue gcnew
System::EventHandler(<span style="color:Blue this
, &my_panel::button2_Click);
<span style="color:Green //

<span style="color:Green // button1

<span style="color:Green //

<span style="color:Blue this
->button1->BackColor = System::Drawing::Color::FromArgb(<span style="color:Blue static_cast
<System::Int32>(<span style="color:Blue static_cast
<System::Byte>(0)), <span style="color:Blue static_cast
<System::Int32>(<span style="color:Blue static_cast
<System::Byte>(192)),
<span style="color:Blue static_cast
<System::Int32>(<span style="color:Blue static_cast
<System::Byte>(0)));
<span style="color:Blue this
->button1->Location = System::Drawing::Point(133, 19);
<span style="color:Blue this
->button1->Name = L<span style="color:#a31515 "button1"
;
<span style="color:Blue this
->button1->Size = System::Drawing::Size(75, 23);
<span style="color:Blue this
->button1->TabIndex = 0;
<span style="color:Blue this
->button1->Text = L<span style="color:#a31515 "Open"
;
<span style="color:Blue this
->button1->UseVisualStyleBackColor = <span style="color:Blue false
;
<span style="color:Blue this
->button1->Click += <span style="color:Blue gcnew
System::EventHandler(<span style="color:Blue this
, &my_panel::button1_Click);
<span style="color:Green //

<span style="color:Green // my_panel

<span style="color:Green //

<span style="color:Blue this
->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
<span style="color:Blue this
->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
<span style="color:Blue this
->Controls->Add(<span style="color:Blue this
->groupBox1);
<span style="color:Blue this
->Name = L<span style="color:#a31515 "my_panel"
;
<span style="color:Blue this
->Size = System::Drawing::Size(262, 150);
<span style="color:Blue this
->groupBox1->ResumeLayout(<span style="color:Blue false
);
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(<span style="color:Blue this
->numericUpDown1))->EndInit();
<span style="color:Blue this
->ResumeLayout(<span style="color:Blue false
);

}
#pragma endregion
<span style="color:Blue private
: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
<span style="color:Blue int
i_valve = safe_cast <<span style="color:Blue int
> (numericUpDown1->Value);
Valve_move (1,i_valve); <span style="color:Green // 1 is for opening

}
<span style="color:Blue private
: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
<span style="color:Blue int
i_valve = safe_cast <<span style="color:Blue int
> (numericUpDown1->Value);
Valve_move (0,i_valve); <span style="color:Green // 0 is for closing

}
};
}

[/code]


and the code generated for my_panel.cpp

<div style="color:Black; background-color:White
<pre>#include <span style="color:#a31515 "StdAfx.h"

#include <span style="color:#a31515 "my_panel.h"


[/code]



I hope I made my question as clear as possible and I thank in advance those who will read me and particularly those who will help me in solving this problem.
Regards

Jean-Louis









View the full article
 
Back
Top