Dialogs
Dialogs are focused windows that interrupt the main flow to get input from the user or deliver important information. wxRuby3 provides ready-made dialogs for the most common tasks — file picking, colour selection, font selection, text entry, and messages — so you rarely need to build these from scratch.
This lesson covers the standard dialogs and then shows how to build your own when none of the built-in ones fit.

Start fresh
Create a new file called dialog_demo.rb:
|
|
Run it. Seven buttons, a result label at the top, and no dialog logic yet. We will add one dialog at a time.
Step 1 — Message box
Wx::message_box is the simplest dialog — it shows a message and one or more buttons. Add a handler in bind_events:
|
|
|
|
Run it. Click Message Box — a native information dialog appears. The third argument combines a button flag with an icon flag using |. Common combinations:
| Buttons | Icon | Use for |
|---|---|---|
Wx::OK |
Wx::ICON_INFORMATION |
Informational messages |
Wx::OK |
Wx::ICON_WARNING |
Non-critical warnings |
Wx::OK |
Wx::ICON_ERROR |
Error messages |
Wx::YES_NO |
Wx::ICON_QUESTION |
Confirmation prompts |
Step 2 — Confirm dialog
A confirmation dialog returns which button the user pressed. Add a handler:
|
|
|
|
Run it. Wx::message_box returns the ID of the button pressed — Wx::YES, Wx::NO, Wx::OK, or Wx::CANCEL depending on which buttons you showed. This return value is how you branch on user decisions throughout the series — the save-on-exit prompt in Module 3 uses exactly this pattern.
Step 3 — Text entry dialog
Wx::TextEntryDialog asks the user to type something:
|
|
|
|
Run it. The show_modal call blocks until the user dismisses the dialog, then returns the ID of the button pressed — Wx::ID_OK or Wx::ID_CANCEL. Read the entered text with dialog.value.
Two things to notice that apply to every non-trivial dialog:
show_modal blocks. The event loop continues running (the app stays responsive) but your code pauses at show_modal until the dialog is dismissed. This is the correct behaviour for dialogs that require a decision before continuing.
dialog.destroy. wxRuby3 dialogs are not garbage collected automatically when they go out of scope. Always call destroy after you are done with a dialog’s data. Failing to do so leaks native window resources.
Step 4 — File dialogs
Wx::FileDialog handles both open and save. Add handlers for both buttons:
|
|
|
|
Run both. The wildcard string 'Ruby files (*.rb)|*.rb|All files (*.*)|*.*' defines the file type filter — pairs of display name and pattern separated by |, multiple pairs separated by |. dialog.path returns the full path the user selected or entered.
Common flags:
| Flag | Effect |
|---|---|
Wx::FD_OPEN |
Open mode |
Wx::FD_SAVE |
Save mode |
Wx::FD_FILE_MUST_EXIST |
Disables OK if file does not exist |
Wx::FD_OVERWRITE_PROMPT |
Warns if the save file already exists |
Wx::FD_MULTIPLE |
Allows selecting multiple files (open mode only) |
Step 5 — Colour dialog
Wx::ColourDialog shows a native colour picker:
|
|
|
|
Run it. Wx::ColourData carries the configuration into the dialog and the result back out. After the dialog closes, dialog.colour_data.colour returns a Wx::Colour object with red, green, and blue components.
Step 6 — Font dialog
Wx::FontDialog shows a native font picker:
|
|
|
|
Run it. The font picker opens with the result label’s current font pre-selected. Choose a different font and the label updates immediately — @result_label.font = font applies the chosen font directly to the widget.
Step 7 — Building a custom dialog
When none of the standard dialogs fits, subclass Wx::Dialog. A custom dialog is just a frame with a panel, sizer, and an OK/Cancel button pair — the same building blocks you already know.
Here is a simple login dialog:

|
|
Add this class to dialog_demo.rb above the DialogFrame class — it needs to be defined before DialogFrame references it.
In a real multi-file app, custom dialogs would live in their own files under lib/dialogs/ and be pulled in with require_relative — exactly the file structure covered in Module 3. For now, same file, above the frame class.
Two things make this work as a modal dialog without any extra wiring:
Stock button IDs. Using Wx::ID_OK and Wx::ID_CANCEL for the buttons gives wxRuby3 enough information to handle show_modal’s return value automatically. When the user clicks OK, show_modal returns Wx::ID_OK. When they click Cancel or close the dialog, it returns Wx::ID_CANCEL. No evt_button handlers needed.
fit. Called at the end of initialize, fit resizes the dialog to exactly wrap its contents. Unlike a frame where you specify size:, dialogs should size themselves from their content.
Use the login dialog from the frame:
|
|
Add a button for it and wire it up to see it in action.
Previous: Menus and toolbars | Next: Preferences panel