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

demonstrates how to create a custom control with xtd::forms::text_box control.

Windows

macOS

Gnome

#define TRACE
#include <xtd/forms/application>
#include <xtd/forms/form>
#include <xtd/forms/text_box>
#include <xtd/ctrace>
#include <xtd/environment>
using namespace xtd;
using namespace xtd::forms;
namespace numeric_tex_box_example {
class numeric_text_box : public text_box {
public:
numeric_text_box() = default;
double value() const {
auto result = .0;
try_parse(text(), result);
return result;
}
void value(double value) {text(xtd::to_string(value, "G"));}
protected:
void on_key_press(key_press_event_args& e) override {
text_box::on_key_press(e);
e.handled((!isdigit(e.key_char()) && e.key_char() != '.') || (e.key_char() == '.' && as<string>(text()).index_of('.') != string::npos));
}
void on_text_changed(const event_args& e) override {
text_box::on_text_changed(e);
on_value_changed(e);
}
void on_value_changed(const event_args& e) {
value_changed(*this, e);
}
private:
using text_box::text;
};
class form1 : public form {
public:
form1() {
text("Numeric text box example");
numeric_text_box1.parent(*this);
numeric_text_box1.value(42);
numeric_text_box1.location({10, 10});
numeric_text_box1.value_changed += [&] {
ctrace << string::format("value_changed [value={}]", numeric_text_box1.value()) << environment::new_line;
};
}
private:
numeric_text_box numeric_text_box1;
};
}
auto main() -> int {
application::run(numeric_tex_box_example::form1 {});
}
Represents the base class for classes that contain event data, and provides a value to use for events...
Definition event_args.h:18
Represents an event.
Definition event.h:21
Represents a window or dialog box that makes up an application's user interface.
Definition form.h:54
Provides data for the xtd::forms::control::key_press event.
Definition key_press_event_args.h:26
Represents a standard Windows text box.
Definition text_box.h:31
std::string to_string(const value_t &value, const std::string &fmt, const std::locale &loc)
Convert a specified value into a string with specified format and locale.
Definition to_string.h:41
The xtd::forms namespace contains classes for creating Windows-based applications that take full adva...
Definition xtd_about_box.h:12
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10