xtd 0.2.0
Loading...
Searching...
No Matches
event.cpp

Shows how to use xtd::event class.

#include <xtd/as>
#include <xtd/console>
#include <xtd/environment>
#include <xtd/event>
#include <xtd/event_handler>
#include <xtd/string>
class control : public xtd::object {
public:
control() = default;
const xtd::string& text() const {return text_;}
void text(const xtd::string& text) {
if (text_ != text) {
text_ = text;
on_text_changed(xtd::event_args::empty);
}
}
protected:
void on_text_changed(const xtd::event_args& e) {text_changed(*this, e);}
private:
xtd::string text_;
};
class button : public control {
public:
button() = default;
void perform_click() {on_click(xtd::event_args::empty);}
protected:
virtual void on_click(const xtd::event_args& e) {click(*this, e);}
};
auto main() -> int {
auto button1 = button {};
button1.text_changed += [](xtd::object & sender, const xtd::event_args & e) {
xtd::console::out << "text_changed [text=" << xtd::as<control>(sender).text() << "]" << xtd::environment::new_line;
};
button1.click += [] {
};
button1.text("button1");
// click simulation
button1.perform_click();
}
// This code produces the following output :
//
// text_changed [text=button1]
// click on button1
Represents text as a sequence of character units.
Definition basic_string.hpp:79
static std::ostream out
Gets the standard output stream. A std::basic_ostream<char_t> that represents the standard output str...
Definition console.hpp:52
static xtd::string new_line() noexcept
Gets the newline string defined for this environment.
Represents the base class for classes that contain event data, and provides a value to use for events...
Definition event_args.hpp:18
static const event_args empty
Provides a value to use with events that do not have event data.
Definition event_args.hpp:25
Represents an event.
Definition event.hpp:21
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:42
xtd::forms::style_sheets::control button
The buttton data allows you to specify the box of a button control.
Definition button.hpp:25
@ control
The left or right CTRL modifier key.
@ e
The E key.
@ button1
The first button on the message box is the default button.