Click here to Skip to main content
15,510,087 members
Home / Discussions / Managed C++/CLI
   

Managed C++/CLI

 
GeneralRe: Serial Port BytesToRead always 0 Pin
Richard MacCutchan21-May-21 2:38
mveRichard MacCutchan21-May-21 2:38 
GeneralRe: Serial Port BytesToRead always 0 Pin
cibec75455@frnla.com21-May-21 2:47
cibec75455@frnla.com21-May-21 2:47 
QuestionHow to call System::Windows::Forms::Controls "invoke" method? Pin
Member 1515077819-May-21 20:48
Member 1515077819-May-21 20:48 
AnswerRe: How to call System::Windows::Forms::Controls "invoke" method? Pin
Victor Nijegorodov19-May-21 21:28
Victor Nijegorodov19-May-21 21:28 
GeneralRe: How to call System::Windows::Forms::Controls "invoke" method? Pin
Member 1515077819-May-21 21:35
Member 1515077819-May-21 21:35 
GeneralRe: How to call System::Windows::Forms::Controls "invoke" method? Pin
Richard MacCutchan19-May-21 22:09
mveRichard MacCutchan19-May-21 22:09 
GeneralRe: How to call System::Windows::Forms::Controls "invoke" method? Pin
Member 1515077819-May-21 22:20
Member 1515077819-May-21 22:20 
QuestionWhat is a preferable approach to develop a common class that handles multiple identical groupbox? Pin
Member 1515077818-May-21 1:41
Member 1515077818-May-21 1:41 
Sorry if my question is unclear, but I shall explain a bit more in the following.

Suppose I am making a software to show ONLY 2 employee information (i.e. Bob and Tim), and my implementation method is to create 2 identical group box.
Each of this groupbox will have the same UI elements , textbox, checkbox, label and buttons.

In order to reduce duplicated codes , I want to create a common class for this groupbox.
Will this work?
Would there be memory leaks?
I did some google research and found something about using smart pointers instead of gcnew'ing a pointer, is it relevant to prevent memory leaks?

Thank you very much for reading, i appreciate any constructive help /example code to get this code properly working!

common_employee_groupbox.h
#pragma once
ref class common_employee_groupbox
{
private: System::Windows::Forms::CheckBox^ _cb_isAbsent = gcnew (System::Windows::Forms::CheckBox^ );  //IS this correct way to declare?
private: System::Windows::Forms::CheckBox^ _cb_isOverTime gcnew (System::Windows::Forms::CheckBox^ );  //IS this correct way to declare?

private: System::Windows::Forms::TextBox^ _tb_employeeName gcnew (System::Windows::Forms::TextBox^ );  //IS this correct way to declare?
private: System::Windows::Forms::TextBox^ _tb_employeeSalary gcnew (System::Windows::Forms::TextBox^ );  //IS this correct way to declare?

private: System::Windows::Forms::Button^ _btn_increaseSalary gcnew (System::Windows::Forms::Button^ );  //IS this correct way to declare?
private: System::Windows::Forms::Button^ _btn_decreaseSalary gcnew (System::Windows::Forms::Button^ );  //IS this correct way to declare?

public: 
    common_employee_groupbox(
    System::Windows::Forms::CheckBox^ cb_isAbsent,
    System::Windows::Forms::CheckBox^ cb_isOverTime,

    System::Windows::Forms::TextBox^ tb_employeeName,
    System::Windows::Forms::TextBox^ tb_employeeSalary,
    
    System::Windows::Forms::Button^ btn_increaseSalary,
    System::Windows::Forms::Button^ btn_decreaseSalary

) {
    _cb_isAbsent = cb_isAbsent;
    _cb_isOverTime= cb_isOverTime;
    _tb_employeeName= tb_employeeName;
    _tb_employeeSalary= tb_employeeSalary;
    _btn_increaseSalary= btn_increaseSalary;
    _btn_decreaseSalary= btn_decreaseSalary;


    _btn_increaseSalary->Click += gcnew System::EventHandler(this,&common_employee_groupbox::increaseSalary_Click); //IS this correct way to declare?
    
    _btn_decreaseSalary->Click += gcnew System::EventHandler(this,&common_employee_groupbox::decreaseSalary_Click); //IS this correct way to declare?
    
    
    	}
    
    private: System::Void increaseSalary_Click() {
    // perform increase salary
    
    tb_employeeSalary->Text = "updatedsalary xxxx"; 
    }
    
    private: System::Void decreaseSalary_Click() {
    // perform decrease salary
    
    tb_employeeSalary->Text = "updatedsalary xxxx"; 
    
    
    }


};



MyForm.h
#include "common_employee_groupbox.h"
namespace MyEmployeeGUI {

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

	using namespace System::IO;
	using namespace System::Runtime::Serialization::Formatters::Binary;

    public ref class MyForm : public System::Windows::Forms::Form
	{
    private: System::Windows::Forms::CheckBox^ cb_isAbsent_Tim;
    private: System::Windows::Forms::CheckBox^ cb_isOverTime_Tim;
    private: System::Windows::Forms::TextBox^ tb_employeeName_Tim;
    private: System::Windows::Forms::TextBox^ tb_employeeSalary_Tim;
    private: System::Windows::Forms::Button^ btn_increaseSalary_Tim;
    private: System::Windows::Forms::Button^ btn_decreaseSalary_Tim;
    
    private: System::Windows::Forms::CheckBox^ cb_isAbsent_Bob;
    private: System::Windows::Forms::CheckBox^ cb_isOverTime_Bob;
    private: System::Windows::Forms::TextBox^ tb_employeeName_Bob;
    private: System::Windows::Forms::TextBox^ tb_employeeSalary_Bob;
    private: System::Windows::Forms::Button^ btn_increaseSalary_Bob;
    private: System::Windows::Forms::Button^ btn_decreaseSalary_Bob;
    	public:
    		MyForm(void)
    		{
    			InitializeComponent();
    			//
    			//TODO: Add the constructor code here
    			//
    		}


    
    .
    .
    .
    
    	System::Void MyForm::MyForm_Load(System::Object^ sender, System::EventArgs^ e) {
    
            common_employee_groupbox^ tim_groupbox = gcnew common_employee_groupbox(
                cb_isAbsent_Tim,
                cb_isOverTime_Tim,
                tb_employeeName_Tim,
                tb_employeeSalary_Tim,
                btn_increaseSalary_Tim,
                btn_decreaseSalary_Tim
            );  //IS this correct way to declare?
        
    
            common_employee_groupbox^ bob_groupbox = gcnew common_employee_groupbox(
                cb_isAbsent_Bob,
                cb_isOverTime_Bob,
                tb_employeeName_Bob,
                tb_employeeSalary_Bob,
                btn_increaseSalary_Bob,
                btn_decreaseSalary_Bob
            );  //IS this correct way to declare?
       
        }
    }
}




Update 1:

I have tried this code, and I'm having errors with this line (the bolded ampersand is highlighted as error) :
_btn_increaseSalary->Click += gcnew System::EventHandler(this,&common_employee_groupbox::increaseSalary_Click);


the error message:
invalid delegate initializer - function does not match the delegate type


How to fix this problem? Already tried googling, but the results aren't helpful



Update 2:

I have fixed the problem from update 1.
it turned out that I need to declare the increase/decrease salary function with the following arguments like below

private: System::Void increaseSalary_Click(System::Object^ sender, System::EventArgs^ e).


I have gotten the code to work as expected, however I'm still unsure if there will be any memory leaks.

Thanks all!

modified 19-May-21 22:45pm.

QuestionHow to convert array<System::Byte^>^ to array<unsigned char>^ Pin
Member 1515077812-May-21 19:19
Member 1515077812-May-21 19:19 
QuestionRe: How to convert array<System::Byte^>^ to array<unsigned char>^ Pin
Richard MacCutchan12-May-21 22:05
mveRichard MacCutchan12-May-21 22:05 
AnswerRe: How to convert array<System::Byte^>^ to array<unsigned char>^ Pin
Member 1515077817-May-21 1:06
Member 1515077817-May-21 1:06 
QuestionHow to implement a global variable if C++/CLI doesn't support it at all? Pin
Member 1515077812-May-21 0:29
Member 1515077812-May-21 0:29 
AnswerRe: How to implement a global variable if C++/CLI doesn't support it at all? Pin
Richard MacCutchan12-May-21 0:47
mveRichard MacCutchan12-May-21 0:47 
GeneralRe: How to implement a global variable if C++/CLI doesn't support it at all? Pin
Member 1515077812-May-21 19:30
Member 1515077812-May-21 19:30 
Questioncpp Pin
likith adithya22-Feb-21 0:39
likith adithya22-Feb-21 0:39 
QuestionRe: cpp Pin
Richard MacCutchan22-Feb-21 0:40
mveRichard MacCutchan22-Feb-21 0:40 
AnswerRe: cpp Pin
Richard Deeming22-Feb-21 1:34
mveRichard Deeming22-Feb-21 1:34 
QuestionSee if print queue is empty Pin
Erich Ruth17-Feb-21 7:25
Erich Ruth17-Feb-21 7:25 
AnswerRe: See if print queue is empty Pin
Richard MacCutchan17-Feb-21 22:29
mveRichard MacCutchan17-Feb-21 22:29 
QuestionCompiling a shared library Pin
Member 1504770716-Jan-21 6:57
Member 1504770716-Jan-21 6:57 
AnswerRe: Compiling a shared library Pin
Gerry Schmitz17-Jan-21 4:09
mveGerry Schmitz17-Jan-21 4:09 
GeneralRe: Compiling a shared library Pin
Member 1504770718-Jan-21 4:52
Member 1504770718-Jan-21 4:52 
GeneralRe: Compiling a shared library Pin
Gerry Schmitz18-Jan-21 6:40
mveGerry Schmitz18-Jan-21 6:40 
Question.NET 5? Pin
John Schroedl5-Jan-21 5:13
professionalJohn Schroedl5-Jan-21 5:13 
AnswerRe: .NET 5? Pin
Richard Andrew x6418-Jan-21 13:03
professionalRichard Andrew x6418-Jan-21 13:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.