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.
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
-
-
-
-
-
- public void Show(string text, string caption) {
-
- txtMessage.Text = text;
-
- this.Text = caption;
-
- ChooseButtons(MessageBoxButtons.OK);
-
- this.ActiveControl = this.Controls[this.Controls.Count - 1];
- this.ShowDialog();
- }
- public void Show(string text, string caption, MessageBoxButtons buttonType) {
- txtMessage.Text = text;
- this.Text = caption;
- ChooseButtons(buttonType);
- this.ActiveControl = this.Controls[this.Controls.Count - 1];
- this.ShowDialog();
- }
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
- public void ShowFromFile(string filename, string caption, MessageBoxButtons buttonType) {
-
- StreamReader sr = new StreamReader(filename);
- txtMessage.Text = sr.ReadToEnd();
- sr.Close();
- this.Text = caption;
- ChooseButtons(buttonType);
- this.ActiveControl = this.Controls[this.Controls.Count - 1];
- this.ShowDialog();
- }
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
- void ChooseButtons(MessageBoxButtons buttonType)
- {
-
- RemoveButtons();
-
- switch (buttonType)
- {
- caseMessageBoxButtons.OK: AddOKButton();
- break;
- caseMessageBoxButtons.YesNo: AddYesNoButtons();
- break;
- caseMessageBoxButtons.OKCancel: AddOkCancelButtons();
- break;
- default:
- AddOKButton();
- break;
- }
- }
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
- private void AddYesNoButtons() {
- Button btnYes = new Button();
- btnYes.Text = "Yes";
- this.Controls.Add(btnYes);
-
-
- btnYes.Location = new Point(this.Width / 2 - 75, this.txtMessage.Bottom + 5);
- btnYes.Size = new Size(70, 20);
- btnYes.DialogResult = DialogResult.Yes;
- this.AcceptButton = btnYes;
- Button btnNo = new Button();
- btnNo.Text = "No";
- this.Controls.Add(btnNo);
- btnNo.Location = new Point(this.Width / 2 + 5, this.txtMessage.Bottom + 5);
- btnNo.Size = newSize(70, 20);
- btnNo.DialogResult = DialogResult.No;
- this.CancelButton = btnNo;
- }
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
- public Font MessageFont {
- set {
- txtMessage.Font = value;
- }
- get {
- return txtMessage.Font;
- }
- }
- public Color MessageForeColor {
- get {
- return txtMessage.ForeColor;
- }
- set {
- txtMessage.ForeColor = value;
- }
- }
- public Color MessageBackColor {
- get {
- return txtMessage.BackColor;
- }
- set {
- txtMessage.BackColor = value;
- this.BackColor = value;
- }
- }
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,
- private void button1_Click(object sender, EventArgs e)
- {
- MicrogoldWindows.ScrollableMessageBox msgBox = new MicrogoldWindows.ScrollableMessageBox();
-
- msgBox.Show("test", "query");
- msgBox.Show("Everything Okay?", "confirm", MessageBoxButtons.YesNo);
- msgBox.Show("Do you really want to remove the file?", "confirm", MessageBoxButtons.OKCancel);
-
- openFileDialog1.FileName = "*.txt";
- if (openFileDialog1.ShowDialog() == DialogResult.OK) {
- msgBox.ShowFromFile(openFileDialog1.FileName, "confirm", MessageBoxButtons.YesNo);
- }
-
- msgBox.MessageFont = new Font("Ariel", 24);
- msgBox.MessageBackColor = Color.LightGray;
- msgBox.ForeColor = Color.Blue;
- msgBox.Show("test", "Check out the color and font functionality!!");
- }
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.