Exploring the TypingBuddy Application (Part 5)

At this point, you have classes you can use to create and store messages in the user’s folder on the current system as described in Exploring the TypingBuddy Application (Part 4). These messages appear whenever the typing time is over in TypingBuddy. Of course, you need some way to display the messages. That’s the responsibility of frmMessage described in Exploring the TypingBuddy Application (Part 2).

One of the requirements for frmMessage is flexibility. The developer has a number of requirements for formatting the message box to convey information in the way the user wants to see it. In order to meet this requirement, frmMessage includes a number of constructors as shown here.

// Contains the current resting time.
private Int32 RemainingTime;
 
public frmMessage(Int32 RestTime)
{
   // Perform the default task.
   InitializeComponent();
 
   // Configure the resting timer.
   RemainingTime = RestTime;
 
   // Configure the Continue button.
   btnContinue.Enabled = false;
   btnContinue.Text = RemainingTime.ToString() + " Secs";
 
   // Start the timer.
   RestTimer.Start();
}
 
public frmMessage(Int32 RestTime, Boolean ShowIcon)
{
   // Perform the default task.
   InitializeComponent();
 
   // Configure the resting timer.
   RemainingTime = RestTime;
 
   // Configure the Continue button.
   btnContinue.Enabled = false;
   btnContinue.Text = RemainingTime.ToString() + " Secs";
 
   // Configure the window.
   this.ControlBox = ShowIcon;
 
   // Start the timer.
   RestTimer.Start();
}
 
public frmMessage(Int32 RestTime, String Message)
{
   // Perform the default task.
   InitializeComponent();
 
   // Configure the resting timer.
   RemainingTime = RestTime;
 
   // Configure the Continue button.
   btnContinue.Enabled = false;
   btnContinue.Text = RemainingTime.ToString() + " Secs";
 
   // Add a message.
   txtMessage.Text = Message;
 
   // Start the timer.
   RestTimer.Start();
}
 
public frmMessage(Int32 RestTime, String Title, String Message)
{
   // Perform the default task.
   InitializeComponent();
 
   // Configure the resting timer.
   RemainingTime = RestTime;
 
   // Configure the Continue button.
   btnContinue.Enabled = false;
   btnContinue.Text = RemainingTime.ToString() + " Secs";
 
   // Set the window titlebar.
   this.Text = Title;
 
   // Add a message.
   txtMessage.Text = Message;
 
   // Start the timer.
   RestTimer.Start();
}
 
public frmMessage(Int32 RestTime, String Message, Boolean ShowIcon)
{
   // Perform the default task.
   InitializeComponent();
 
   // Configure the resting timer.
   RemainingTime = RestTime;
 
   // Configure the Continue button.
   btnContinue.Enabled = false;
   btnContinue.Text = RemainingTime.ToString() + " Secs";
 
   // Configure the window.
   this.ControlBox = ShowIcon;
 
   // Add a message.
   txtMessage.Text = Message;
 
   // Start the timer.
   RestTimer.Start();
}
 
public frmMessage(Int32 RestTime, String Title,
   String Message, Boolean ShowIcon)
{
   // Perform the default task.
   InitializeComponent();
 
   // Configure the resting timer.
   RemainingTime = RestTime;
 
   // Configure the Continue button.
   btnContinue.Enabled = false;
   btnContinue.Text = RemainingTime.ToString() + " Secs";
 
   // Configure the window.
   this.Text = Title;
   this.ControlBox = ShowIcon;
 
   // Add a message.
   txtMessage.Text = Message;
 
   // Start the timer.
   RestTimer.Start();
}

All of the constructors require RestTime as input. This is the amount of time that the user must do something other than working at the computer (based on whatever messages the user has configured or using the default message). The code uses this input to initialize RemainingTime, which contains the amount of time remaining to rest. When working with the simplest constructor, the code sets btnContinue.Enabled to false so that the user must wait to clear the message box. It also sets btnContinue.Text to the initial resting time value. The code then starts the timer by calling RestTimer.Start().

Beyond this simple setup, the developer can also choose to provide additional information. For example, supplying the Boolean value, ShowIcon, determines whether frmMessage displays an icon when displayed. The String value, Message, contains a custom message that the user has defined. Otherwise, frmMessage displays a default message to the user. The developer can also choose to provide a custom title for the message box by supplying the String value, Title. The constructors can accept various combinations of these three inputs, or provide all of them.

Once the timer is started, it counts down the time for the rest period. The Timer control, RestTime performs this task for you. The Tick() event handler code is as shown here.

private void RestTimer_Tick(object sender, EventArgs e)
{
   // Check the remaining time.
   if (RemainingTime == 0)
   {
      // Stop the timer.
      RestTimer.Stop();
 
      // Configure the Continue button.
      btnContinue.Text = "&Continue";
      btnContinue.Enabled = true;
   }
   else
   {
      // Count down another second.
      RemainingTime--;
 
      // Display the update.
      btnContinue.Text = RemainingTime.ToString() + " Secs";
   }
}

During the countdown phase, the value of RemainingTime is decremented. The code then displays the new number of seconds in btnContinue.Text so the user can see it on screen. When RemainingTime reaches 0, the code calls RestTimer.Stop() to stop the timer. It then changes btnContinue.Text to “&Continue” (with the C underlined) and btnContinue.Enable to true so that the user can click the Continue button.

The code for the Continue and Quit Click() event handlers simply closes the form. The reaction differs based on the button the user clicks, but this is handled by frmMain.

Next we’ll you’ll see the code used to add and edit messages. This information is handled by frmAddEdit. In the meantime, let me know if you have questions about the TypingBuddy application code you’ve seen today at John@JohnMuellerBooks.com. You can see the next post in this series at Exploring the TypingBuddy Application (Part 6).