Multiline messages and timers.
Multi line text boxes
The simple text box we have met in the previous lecture has an interesting property Multiline. This is
a boolean property, which being set to true allows the text box to span multiple lines. The default value is
false. Another boolean property AcceptReturn, if set to true, pressing
Enter creates a new line line the text box, if that text box spans multiple lines. Otherwise, pressing Enter
clicks the default button of the form. For multiple line text boxes we can set scroll bars. The corresponding property
is ScrollBars it can take one of the values none, horizontal, vertical,
or both.
Now please create a form that looks like one shown on the picture below:
Create a form with multiple line text box
where the big square grey area on the left of the buttons is a usual text area with the following properties set:
| Property | Value | Description |
| (Name) | LogBox |
sets the name of the textbox |
| Multiline | true |
can span multiple lines |
| ScrollBars | Vertical |
show the vertical scroll bar |
| ReadOnly | true |
doesn't let the user to type in the text box |
| Text | |
initially shows nothing |
| Width | 400 | width of the text box |
| Height | 250 | height of the text box |
Now, we can use this text box LogBox as a output console. Double click on the Start button
of your form and enter the following code in the body of the buttoc1_Click() function:
static int i = 1;
LogBox->Text = String::Concat(LogBox->Text, S"Message #", i.ToString(), S"\r\n");
i++;
If you now run your new project, then every click on the Start button will add a new line like
"Message #13" to the text box.
Timers
If we do not want to click on the Start button over and over, we can ask the program periodically call a
function that would add the next message (or do something useful, for that matter). In order to do that, choose the
Timer on the Toolbox panel and click on your form. A new Timer element appears in the area below
the form:
Timer element appears below the form
In the Tool panel set the following properties of the timer element:
| Property | Value | Description |
| (Name) | NextStepTimer |
sets the name of the timer |
| Interval | 500 |
set the interval between calls to 500 milliseconds |
| Enabled | false |
initially timer is not active |
Click twice on the icon of the timer element and copy the code from the button1_Click() function into the
NextStepTimer_Tick() function. When the timer starts it will add a new message to the log text box every half
of a second.
To start the timer, all we need to do is to set the value of the property Enabled to true. The
function which is called by the Start button would be a good place to do that. Double click on the Start
button on your form and enter the code:
NextStepTimer->Enabled = true;
Numeric up and down boxes, how to change timer's interval
If we need to print our message more or less often, we need to change the value of the Interval property
of the timer element. We can do that while the program is running. Let's modify the from by adding NumericUpDown
element from the Toolbox
Numeric Up and Down element
Please set the following properties of the element:
| Property | Value | Description |
| (Name) | TimerUpDown |
sets the name of the numeric up and down element |
| Minimum | 50 |
set the minimal value of the variable to 50 (ms) |
| Maximum | 60000 |
set the maximum value of the interval to 60000 (ms) |
| Increment | 25 |
set the increment/decrement of the variable to 25 (ms) |
| Value | 500 |
set the initial value of the variable to 500 (ms) |
| ReadOnly | true |
do not let the user type in this numeric text box, let him use the arrows only |
The last step would be to connect the value in the numeric up and down. Double click on the numeric up/down element on
your form and in the function TimerUpDown_ValueChanged() enter the following code:
NextStepTimer->Interval = Convert::ToInt32( TimerUpDown->Value );
Now, every click on the numeric up/down element will change the displayed value and call the function
TimerUpDown_ValueChanged(), which will read the changed value and reset the timer interval.
Homework assignment
To demonstrate that you understand the material presented in the last two lectures, add a new button Pause
to the form and do the following:
- initially the Pause button is not enabled;
- when the user clicks on the Start button, then you should also enable the Pause button and
disable the Start button (plus start the timer as it was);
- when user clicks on the Pause button, the program should: stop the timer, enable the Start button
and disable the Pause button.