xtd 0.2.0
timers_timer_synchronizing_object.cpp

Shows how to use xtd::timers::timer::synchronizing_object property.

#include <xtd/forms/application>
#include <xtd/forms/button>
#include <xtd/forms/form>
#include <xtd/forms/message_box>
#include <xtd/forms/save_file_dialog>
#include <xtd/forms/text_box>
#include <xtd/io/stream_writer>
#include <xtd/timers/timer>
#include <xtd/date_time>
#include <xtd/startup>
#include <memory>
using namespace xtd;
using namespace xtd::forms;
using namespace xtd::io;
using namespace xtd::timers;
namespace timer_example {
class form1 : public form {
public:
static void main() {
application::run(form1 {});
}
form1() {
initialize_component();
text("Quick Text Editor");
button1.text("Save");
text_box1.multiline(true);
// Configure the SaveFile dialog
save_file_dialog1.filter("txt files (*.txt)|*.txt|All files (*.*)|*.*");
save_file_dialog1.restore_directory(true);
// Define the event handler
timer.elapsed += {*this, &form1::prompt_for_save};
// Synchronize the timer with the text box
timer.synchronizing_object(*this);
// Start the timer
timer.auto_reset(true);
}
private:
void initialize_component() {
form_closing += {*this, &form1::form1_form_closing};
controls().push_back_range({text_box1, button1});
button1.click += {*this, &form1::button1_click};
text_box1.dock(dock_style::fill);
text_box1.text_changed += {*this, &form1::text_box1__text_changed};
}
void prompt_for_save(object& source, const elapsed_event_args& e) {
if (has_changed && !dialog_is_open) {
elapsed_minutes++;
dialog_is_open = true;
if (message_box::show(string::format("{0} minutes have elapsed since the text was saved. Save it now? ",
elapsed_minutes), "Save Text",
button1_click(*this, event_args::empty);
}
dialog_is_open = false;
}
void button1_click(object& sender, const event_args& e) {
if (string::is_empty(save_file_dialog1.file_name())) {
if (save_file_dialog1.show_dialog() == dialog_result::ok)
sw = new_ptr<stream_writer>(save_file_dialog1.file_name(), false);
}
txt = text_box1.text();
has_changed = false;
}
void form1_form_closing(object& sender, form_closing_event_args& e) {
timer.close();
if (sw != null) {
sw->write(txt);
sw->close();
}
}
void text_box1__text_changed(object& sender, const event_args& e) {
has_changed = true;
}
save_file_dialog save_file_dialog1;
text_box text_box1;
bool dialog_is_open = false;
int elapsed_minutes = 0;
bool has_changed = false;
string txt;
// Create a timer with a 1-minute interval
timers::timer timer {60'000};
};
}
startup_(timer_example::form1::main);
bool is_empty() const noexcept
Definition basic_string.hpp:1503
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
static void run()
Begins running a standard application message loop on the current thread, without a form.
Represents a Windows button control.
Definition button.hpp:49
Provides data for the form_closing event.
Definition form_closing_event_args.hpp:22
Represents a window or dialog box that makes up an application's user interface.
Definition form.hpp:54
static dialog_result show(const iwin32_window &owner)
Displays a message box in front of the specified window.
Prompts the user to select a location for saving a file. This class cannot be inherited.
Definition save_file_dialog.hpp:30
Represents a standard Windows text box.
Definition text_box.hpp:31
Implements a timer that raises an event at user-defined intervals. This timer is optimized for use in...
Definition timer.hpp:38
void stop()
Starts the timer.
void start()
Stops the timer.
Provides data for the xtd::timers::timer::elapsed event.
Definition elapsed_event_args.hpp:18
Generates an event after a set interval, with an option to generate recurring events.
Definition timer.hpp:50
#define startup_(main_method)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition startup.hpp:167
null_ptr null
Represents a null pointer value.
xtd::sptr< type_t > ptr
The xtd::ptr object is a shared pointer.
Definition ptr.hpp:27
ptr< type_t > new_ptr(args_t &&... args)
The xtd::new_ptr operator creates a xtd::ptr object.
Definition new_ptr.hpp:24
@ e
The E key.
Definition console_key.hpp:96
@ question
The message box contains a symbol consisting of a question mark in a circle. The question-mark messag...
Definition message_dialog_icon.hpp:28
@ ok
The dialog box return value is OK (usually sent from a button labeled OK).
Definition dialog_result.hpp:47
@ yes
The dialog box return value is Yes (usually sent from a button labeled Yes).
Definition dialog_result.hpp:57
@ button1
The first button on the message box is the default button.
Definition message_dialog_default_button.hpp:24
@ bottom
The control's bottom edge is docked to the bottom of its containing control.
Definition dock_style.hpp:29
@ fill
All the control's edges are docked to the all edges of its containing control and sized appropriately...
Definition dock_style.hpp:35
@ yes_no_cancel
The message box contains Yes, No, and Cancel buttons.
Definition message_dialog_buttons.hpp:30
The xtd::forms namespace contains classes for creating Windows-based applications that take full adva...
Definition texts.hpp:217
The xtd::io namespace contains types that allow reading and writing to files and data streams,...
Definition binary_reader.hpp:17
Contains classes that represent ASCII and Unicode character encodings; abstract base classes for conv...
Definition basic_string_builder.hpp:16
Provides the xtd::timers::timer component, which allows you to raise an event on a specified interval...
Definition elapsed_event_args.hpp:11
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8