xtd 0.2.0
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 {
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 {
on_value_changed(e);
}
void on_value_changed(const event_args& e) {
value_changed(*this, e);
}
private:
};
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 {});
}
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
Represents an event.
Definition event.hpp:21
static void run()
Begins running a standard application message loop on the current thread, without a form.
virtual void on_key_press(key_press_event_args &e)
Raises the xtd::forms::control::key_press event.
Represents a window or dialog box that makes up an application's user interface.
Definition form.hpp:54
Provides data for the xtd::forms::control::key_press event.
Definition key_press_event_args.hpp:26
Represents a standard Windows text box.
Definition text_box.hpp:31
const xtd::string & text() const noexcept override
Gets the text associated with this control.
void on_text_changed(const event_args &e) override
Raises the xtd::forms::control::text_changed event.
std::ostream ctrace(nullptr)
Provides an std::ostream for xtd::diagnostics::trace.
type_t as(any_object &o)
Casts a type into another type.
Definition __as_any_object.hpp:59
bool try_parse(const std::basic_string< char > &str, value_t &value) noexcept
Convert a string into a type.
Definition parse.hpp:416
@ e
The E key.
Definition console_key.hpp:96
The xtd::forms namespace contains classes for creating Windows-based applications that take full adva...
Definition texts.hpp:217
Contains classes that represent ASCII and Unicode character encodings; abstract base classes for conv...
Definition basic_string_builder.hpp:16
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
string to_string() const noexcept override
Returns the string representation of this xtd::read_only_span <type_t> object.
Definition read_only_span.hpp:375