A Scrollable MessageBox In C#

Scrollable MessageBox
Figure 1 - Scrollable MessageBox

Introduction

Most of the time a message box is a simple sentence to display some information to the user or ask a simple yes/no question to the user. However, there are cases you may run across where you want to display a lot of information to your user, and it just won't seem to fit in that little piece of real estate on your screen. Currently the .NET message box expands to fit the message, but this starts getting undesirable when the message is larger than the screen. At the point when the message box fills the entire vertical limits of your screen, you may find it difficult to even grab the title bar to move the message box around. This is when you may decide that perhaps a scrollable message box would come in handy. The message box I created takes most of the parameters of the existing message box, such as caption, text, and MessageButtons. I have also added an additional feature to allow the message box to read the text from a file before displaying the message. Additionally, you can manipulate the font and color properties of the ScrollableMessageBox.

Design

The ScrollableMessageBox Design Consists of a Form which contains a Multiline ReadOnly TextBox and a dynamically created button(s) set. Text is written to the Text property of the TextBox. The ChooseButtons method places and arranges he buttons in the form according to which MessageBoxButtons we want. The default button is the OK button seen in most message boxes. Several Show methods allow us to show the ScrollableMessageBox using different parameter combinations (just like the existing MessageBox). The MessageFont, MessageForeColor, and MessageBackColor Properties allow us to change the font and colors of our text message box by propagating the TextBox properties. This scrollable message box works a bit differently than the current message box, in that the ScrollableMessageBox does not have static methods so you must instantiate a ScrollableMessageBox before using it.

UML Design of the ScrollableMessageBox (Reverse engineered using WithClass
Figure 2 - UML Design of the ScrollableMessageBox (Reverse engineered using WithClass)

The Code

The various Show Methods allow us to populate the titlebar, the TextBox, and the buttons we wish to use for the dialog. Below are two of the Show methods. The first Show method just takes a message parameter and a caption for the title bar of the message. The second Show method also takes a type of MessageBoxButton as a parameter (You can also choose MessageBoxButtons.YesNo and MessageBoxButtons.OKCancel).

Listing 1 - Different Show Methods for the ScrollableMessageBox

  1. ///<summary>    
  2. ///    
  3. ///</summary>    
  4. ///<param name="text">text inside the message box</param>    
  5. ///<param name="caption">caption on the title bar</param>    
  6. public void Show(string text, string caption) {    
  7.     // populate the text box with the message    
  8.     txtMessage.Text = text;    
  9.     // populate the caption with the passed in caption    
  10.     this.Text = caption;    
  11.     //Assume OK Button, by default    
  12.     ChooseButtons(MessageBoxButtons.OK);    
  13.     // the active control should be the OK button    
  14.     this.ActiveControl = this.Controls[this.Controls.Count - 1];    
  15.     this.ShowDialog(); // show the message box as a modal dialog    
  16. }    
  17. public void Show(string text, string caption, MessageBoxButtons buttonType) {    
  18.     txtMessage.Text = text;    
  19.     this.Text = caption;    
  20.     ChooseButtons(buttonType);    
  21.     this.ActiveControl = this.Controls[this.Controls.Count - 1];    
  22.     this.ShowDialog();    
  23. }  
We also have a show method that allows us to bring in a file. This method uses the StreamReader class to read the file into the ScrollableMessageBox. The file is read into a string and then assigned to the TextBox.Text inside the scrollable message box. The remaining code in the ShowFromFile method performs the same tasks as the previous Show methods in listing 1.

Listing 2- Scrollable MessageBox Populated from a File

  1. public void ShowFromFile(string filename, string caption, MessageBoxButtons buttonType) {    
  2.     // read the file into the message box    
  3.     StreamReader sr = new StreamReader(filename);    
  4.     txtMessage.Text = sr.ReadToEnd();    
  5.     sr.Close();    
  6.     this.Text = caption;    
  7.     ChooseButtons(buttonType);    
  8.     this.ActiveControl = this.Controls[this.Controls.Count - 1];    
  9.     this.ShowDialog();    
  10. }   
Dynamic Buttons

Buttons are loaded dynamically in the Show methods by calling ChooseButtons. The method for ChooseButtons is shown in listing 3. This method switches on the different types of buttons and draws the appropriate set of buttons for the ScrollableMessageBox.

Listing 3 - Adding the chosen MessageBoxButton

  1. void ChooseButtons(MessageBoxButtons buttonType)  
  2. {  
  3.     // remove existing buttons  
  4.     RemoveButtons();  
  5.     // decide which button set to add from buttonType, and add it   
  6.     switch (buttonType)  
  7.     {  
  8.         caseMessageBoxButtons.OK: AddOKButton();  
  9.         break;  
  10.         caseMessageBoxButtons.YesNo: AddYesNoButtons();  
  11.         break;  
  12.         caseMessageBoxButtons.OKCancel: AddOkCancelButtons();  
  13.         break;  
  14.         default:  
  15.             AddOKButton(); // default is an OK button  
  16.             break;  
  17.     }  
  18. }  

The buttons are added dynamically, just as they would be in the form designer. In fact I copied the code from the form designer to figure out every property I needed to set for each button. Listing 4 shows how we added the Yes No buttons:

Listing 4 - Adding Yes and No Buttons to the ScrollableMessageBox

  1. private void AddYesNoButtons() {    
  2.     Button btnYes = new Button();    
  3.     btnYes.Text = "Yes";    
  4.     this.Controls.Add(btnYes); // add the yes button to the form    
  5.     // calculate the location of the buttons so that they are centered    
  6.     // at the bottom    
  7.     btnYes.Location = new Point(this.Width / 2 - 75, this.txtMessage.Bottom + 5);    
  8.     btnYes.Size = new Size(70, 20);    
  9.     btnYes.DialogResult = DialogResult.Yes; // set the results for a modal dialog    
  10.     this.AcceptButton = btnYes; // set the yes button as a dialog accept button    
  11.     Button btnNo = new Button();    
  12.     btnNo.Text = "No";    
  13.     this.Controls.Add(btnNo); // add the no button to the form    
  14.     btnNo.Location = new Point(this.Width / 2 + 5, this.txtMessage.Bottom + 5);    
  15.     btnNo.Size = newSize(70, 20);    
  16.     btnNo.DialogResult = DialogResult.No; // set the results for a modal dialog    
  17.     this.CancelButton = btnNo; // set the no button as a dialog cancel button    
  18. }  
Listing 5 shows the properties for fonts and colors. These properties simply propagate their property information to the same properties inside the TextBox of the ScrollableMessageBox

Listing 5 - Public Font and Color Properties Inside the Scrollable Picture Box

  1. public Font MessageFont {    
  2.     set {    
  3.         txtMessage.Font = value;    
  4.     }    
  5.     get {    
  6.         return txtMessage.Font;    
  7.     }    
  8. }    
  9. public Color MessageForeColor {    
  10.     get {    
  11.         return txtMessage.ForeColor;    
  12.     }    
  13.     set {    
  14.         txtMessage.ForeColor = value;    
  15.     }    
  16. }    
  17. public Color MessageBackColor {    
  18.     get {    
  19.         return txtMessage.BackColor;    
  20.     }    
  21.     set {    
  22.         txtMessage.BackColor = value;    
  23.         this.BackColor = value;    
  24.     }    
  25. }  
Using the ScrollableMessageBox Control.

Unlike the MessageBox in .NET, the ScrollableMessageBox is instantiated. After it is instantiated, the ScrollableMessageBox is pretty much used the same way as a MessageBox, by calling Show. Listing 6 illustrates some different ways to use the ScrollableMessageBox,

  1. private void button1_Click(object sender, EventArgs e)    
  2. {    
  3.     MicrogoldWindows.ScrollableMessageBox msgBox = new MicrogoldWindows.ScrollableMessageBox();    
  4.     // use of the different Show methods     
  5.     msgBox.Show("test""query");    
  6.     msgBox.Show("Everything Okay?""confirm", MessageBoxButtons.YesNo);    
  7.     msgBox.Show("Do you really want to remove the file?""confirm", MessageBoxButtons.OKCancel);    
  8.     // use of the ShowFromFile method    
  9.     openFileDialog1.FileName = "*.txt";    
  10.     if (openFileDialog1.ShowDialog() == DialogResult.OK) {    
  11.         msgBox.ShowFromFile(openFileDialog1.FileName, "confirm", MessageBoxButtons.YesNo);    
  12.     }    
  13.     // altering fonts and colors of the messagebox    
  14.     msgBox.MessageFont = new Font("Ariel", 24);    
  15.     msgBox.MessageBackColor = Color.LightGray;    
  16.     msgBox.ForeColor = Color.Blue;    
  17.     msgBox.Show("test""Check out the color and font functionality!!");    
  18. }   
Conclusion

.NET makes it easy to construct controls that go beyond the capability of existing controls inside the platform. The ScrollableMessageBox as an example of how you can quickly code a control that gives you much of the functionality of the existing message box and more. Anyway, the next time you decide to create those lengthy messages in your message box, take a break and have a scroll around the block using this scrollable solution in C# and .NET.