TNotifyEvent (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This code example is a C++Builder console application that illustrates the usage of:

Code

#include <vcl.h>
#pragma hdrstop

#include <tchar.h>
#include <stdio.h>
#include <Classes.hpp>
// ---------------------------------------------------------------------------

#pragma argsused

class TNoteBook : public TObject {
private:
	TStringList *noteList;

public:
	TNoteBook() {
		this->noteList = new TStringList();

		// assign method pointer
		TNotifyEvent noteListOnChange = &(this->OnChangeNoteList);
		this->noteList->OnChange = noteListOnChange;
	}

	__property TStringList* NoteList = {read = noteList};

	void __fastcall OnChangeNoteList(System::TObject* Sender) {
		puts("Note list has changed:");
		this->DisplayNoteList();
	}

	void DisplayNoteList() {
		for (int i = 0; i < this->noteList->Count; i++) {
			printf("%ls\n", this->noteList->Strings[i]);
		}
		printf("\t%d notes", this->noteList->Count);
	}
};

int _tmain(int argc, _TCHAR* argv[]) {
	TNoteBook *noteBook = new TNoteBook();

	noteBook->NoteList->Add("TODO: buy some butter"); // this will generate a TNotifyEvent

	delete noteBook;
	return 0;
}

Console Output

Note list has changed:
TODO: buy some butter
        1 notes

Uses