How to reference a custom class object in EVT_BUTTON Command Event function? Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
hbreed
In need of some credit
In need of some credit
Posts: 2
Joined: Tue Jun 29, 2021 1:17 am

How to reference a custom class object in EVT_BUTTON Command Event function?

Post by hbreed »

My class "Student" contains a few variables about the student

I have a wxGrid in my main()

I also have an object array of class Student initialized in main()
Student myStudent [81];

How would I use the button click event to manipulate the variables of my Student object array?
I already have the event table set up and the button works for other things
It currently says "Identifier myStudent is undefined"
If I try to define it like other variables in the main header file (i.e wxGrid *grid = nullptr;) I can't get it to work.
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to reference a custom class object in EVT_BUTTON Command Event function?

Post by doublemax »

Code: Select all

Student myStudent [81];
What's the scope of this variable? Please show some code.

The usual structure would be:
You derive your own class from wxFrame.
This class would have a member variable Student myStudent [81];
The event handler would be a method of that class and you could access the member variable from there.
Use the source, Luke!
hbreed
In need of some credit
In need of some credit
Posts: 2
Joined: Tue Jun 29, 2021 1:17 am

Re: How to reference a custom class object in EVT_BUTTON Command Event function?

Post by hbreed »

Forgive me if I don't make complete sense. I'm quite new to Visual C++/wxWidgets.

So it currently looks like my frame is a part of Main?

Code: Select all

m_frame1 = new cMain();
m_frame1->Show();
And in Main.h where the main class is, I've also added my Student Class. I previously had it just in Main.cpp but that didnt help either.
I guess my question now is how do I derive my class Student from wxFrame

Function "OnButtonClicked2" is the function that needs to reference myStudent[81];

Here is Main.h

Code: Select all

#pragma once
#include "wx/wx.h"
#include <wx/grid.h>
#include <string>

using namespace std;

class cMain : public wxFrame
{
public:
	cMain();
	~cMain();

public:
	wxButton *m_btn1 = nullptr;
	wxTextCtrl *myText = nullptr;
	wxListBox *m_list1 = nullptr;
	wxGrid *grid = nullptr;
	wxButton *myButton = nullptr;
	wxButton *myButton2 = nullptr;
	wxBoxSizer *sizer = nullptr;
	wxPanel *myPanel = nullptr;
	wxStaticText *Title = nullptr;
	bool minimumFull;
	



	void OnButtonClicked(wxCommandEvent &evt);
	void OnButtonClicked2(wxCommandEvent &evt);
	wxDECLARE_EVENT_TABLE();
};




class Student {
public:
	const int NUM_BLOCKS = 34;
	int classCount(int);
	int blocklength() const;
	int blockCount(int);
	void AddClass(int);
	void AddBlock(int);
	int classSize() const;


	string name;
	vector<int> classes{};
	vector<int> blocks{};


};


Here is the significant parts of main.cpp

Code: Select all

#include "cMain.h"
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
#include <time.h>
#include <algorithm>

using namespace std;

wxBEGIN_EVENT_TABLE(cMain, wxFrame)
EVT_BUTTON(10001, OnButtonClicked)
wxEND_EVENT_TABLE()





int Student::blocklength() const {
	return blocks.size();
}

void Student::AddBlock(int myBlock){
	blocks.push_back(myBlock);
	return;
}

int Student::blockCount(int numBlock) {
	return count(blocks.begin(), blocks.end(), numBlock);
}

int Student::classCount(int numClass) {
	return count(classes.begin(), classes.end(), numClass);
}

void Student::AddClass(int myClass) {
	classes.push_back(myClass);
	return;
}

int Student::classSize() const {
	return classes.size();
}





cMain::cMain() : wxFrame(nullptr, wxID_ANY, "UTCVM Schedule", wxPoint(300, 50), wxSize(1600, 900))
{
	SetBackgroundColour(wxColor("#FFAE36"));

	sizer = new wxBoxSizer(wxHORIZONTAL);

	SetSizer(sizer);



	Student myStudent[81];
	
	grid = new wxGrid(this, wxID_ANY, wxPoint(0, 100), wxSize(1280, 720));
	grid->CreateGrid(47, 34);
	
	srand(time(NULL));
	
	cMain::~cMain()
{

}





void cMain::OnButtonClicked(wxCommandEvent &evt) {


	for (int n = 0; n < 41; n++) {
		for (int m = 0; m < 34; m++) {
			grid->SetCellBackgroundColour(n, m, wxColor("#FFFFFF"));
		}
	}


	for (int p = 0; p < 41; p++) {
		for (int q = 0; q < 34; q++) {
			if (grid->GetCellValue(p, q).find(myText->GetValue()) != string::npos) {
				grid->SetCellBackgroundColour(p, q, wxColor("#FFAE36"));
			}
		}
	}

	grid->Refresh();

	evt.Skip(); //Tells system event has been handled and finished
}


void cMain::OnButtonClicked2(wxCommandEvent &evt) {


	for (int i = 0; i < 81; i++) {
		int classMinimum[41];
		for (int j = 0; j < 34; j++) {
			int currentClass;
			int cMinimum;
			int numRepeat;
			do {

				currentClass = rand() % 41;

				switch (currentClass) {
				//LONG SWITCH STATEMENT HERE
				}
			} while ((myStudent[i].classCount(currentClass) > numRepeat) || (classMinimum[currentClass] >= cMinimum));
			myStudent[i].AddClass(currentClass);
			classMinimum[currentClass] = (classMinimum[currentClass] + 1);

			if (grid->GetCellValue(currentClass, j).find("\n") != string::npos) {
				grid->SetCellValue(currentClass, j, grid->GetCellValue(currentClass, j) + myStudent[i].name + "\n");
			}
			else {
				grid->SetCellValue(currentClass, j, myStudent[i].name + "\n");
			}
		}
	}



	grid->Refresh();

	evt.Skip(); //Tells system event has been handled and finished
}
	
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to reference a custom class object in EVT_BUTTON Command Event function?

Post by doublemax »

Code: Select all

Student myStudent[81];
You're defining this as a local variable, therefore it's only accessible inside that method. You need to turn it into a member variable.

Add it here:

Code: Select all

public:
	wxButton *m_btn1 = nullptr;
	wxTextCtrl *myText = nullptr;
	wxListBox *m_list1 = nullptr;
	wxGrid *grid = nullptr;
	wxButton *myButton = nullptr;
	wxButton *myButton2 = nullptr;
	wxBoxSizer *sizer = nullptr;
	wxPanel *myPanel = nullptr;
	wxStaticText *Title = nullptr;
	bool minimumFull;
Use the source, Luke!
Post Reply