Chapter 16: Printing Text

 

Overview

We mentioned in Chapter 2 that users have two principal means of communicating with their computer: the keyboard and the mouse. But how does a computer application communicate with the user ? The answer was conceived early in the development of computer software protocols: the message box.

In the days before Windows, the message returned by an application was a couple of lines that appeared on the blackened DOS screen, preceded by a word or words to wake up the user, like DANGER! or ERROR IN FILE FETCH! . But those couple of lines were important because they warned the user that something was wrong inside the application. With the advent of Windows, the message box immediately occupied a spot in the center of the screen, and it could appear as small or as large as the computer programmer wanted.

It wasn t until the early 1990s that message boxes became truly sophisticated ” not only did they warn the user of impending doom, but in some cases they asked the user what should be done to alleviate the problem. This causes one to surmise that the next generation of message boxes will not only appear on the user s screen, but their messages will emanate from the speakers on either side of the display. DOOM, DOOM, DOOM, I SAY, said the speakers as they burst into life. YOU HAVE INTRODUCED SO MUCH TRASH INTO MY DATABASE THAT I AM UNABLE TO CARRY ON. (That sounds like HAL from 2001: A Space Odyssey .)

Meanwhile, totally apart from the message box gurus, a significant number of compiler writers were constantly reinventing software that would debug an ailing software program. You will find that the debug schemes offered by Visual Studio C# are extraordinarily good; they embed their own query code into the code that the programmer is writing. When debugging is needed, the debuggers are ready and willing to take charge!

But the one thing these compiler writers had not counted on was the tenacity of the average programmer who wanted to solve her own problems in her own ways. (Programmers just don t like formal debuggers.)

Then along came the modern message box, and many programmers saw, in that message box, a way to circumvent all the smarts of debuggers. Each programmer became a debugger in his own right, and the tool he used to peer into his software to debug was the message box.

For example, most programmers, as soon as they finish a coding sequence to open and read a file, place a message box at the end of the sequence to read back to the screen what was just read from the file. When the source code approached completion, the cross- weave of message boxes was commented out of the code. When it came time to release the code to a customer the message boxes were removed completely.

Today, you will find that the Visual Studio C# message box is one of the most complete pieces of software you will ever have occasion to use. There are two forms: one that displays a message only and one that allows a decision.

The first does not require the user to make a decision. It is for information only.

 MessageBox.Show("Record " + strRecordName + " placed in file " + strFileName + " ."); 

This would produce a message box with a statement in the center: Record 499L placed in file 24Feb04Ultra.

The second form requires the user to make a decision.

 if(MatchFound > 0) {   DialogResult dr = MessageBox.Show("Duplicate entry found for year-week '"     + strYW + "' . \n Old Entry is: '" + strDDTData + "' ."     + "\n New Entry is: '" + strMasterString     + "' .\n\nDestroy 'Old' entry?", "Duplicate",     MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);   if(dr == DialogResult.Yes) // Replace old entry with new entry.   { etc. 

As MessageBox shows:

  1. The first argument is a statement printed in the center of the message box:

     "Duplicate entry found for year-week '" + strYW + "' . \n Old Entry is: '" +            strDDTData + "' ." + "\n New Entry is: '" + strMasterString +            "' .\n\nDestroy 'Old' entry?" 

    where \n means skip to the next line on the screen.

  2. The second argument is a caption for the message box:

     "Duplicate" 
  3. The third argument specifies which buttons should be presented to the user. In this case there are two buttons , Yes and No:

     MessageBoxButtons.YesNo 
  4. The fourth argument chooses an icon that appears at the top left of the message box:

     MessageBoxIcon.Exclamation 
  5. A fifth argument exists, if needed:

     MessageBoxDefaultButton. 
  6. A sixth argument can also be used if needed:

     MessageBoxOptions. 

There are no limits for arguments 1 and 2 ” any character string is OK. As the string becomes longer the message box becomes wider and longer.

The third argument, MessageBoxButtons, has these choices:

  • AbortRetryIgnore (three buttons)

  • OK (one button)

  • OKCancel (two buttons)

  • RetryCancel (two buttons)

  • YesNo (two buttons)

  • YesNoCancel (three buttons)

The fourth argument, MessageBoxIcon, has these choices:

  • Asterisk (a lowercase letter i in a circle)

  • Error (a white X in a circle with a red background)

  • Exclamation (an exclamation point in a triangle with a yellow background)

  • Hand (same as Error)

  • Information (same as Asterisk)

  • None (no symbols)

  • Question (a question mark in a circle)

  • Stop (same as Error)

  • Warning (same as Exclamation)

The fifth argument, MessageBoxDefaultButton, has these choices:

  • Button1 (first button on the message box is highlighted as the default)

  • Button2 (second button on the message box is highlighted as the default)

  • Button3 (third button on the message box is highlighted as the default)

The sixth argument, MessageBoxOptions, has these choices:

  • DefaultDesktopOnly (message box is displayed on the active desktop)

  • RightAlign (message box text is right-aligned)

  • RtlReading (message box text is displayed backward (right to left)

  • ServiceNotification (same as DefaultDesktopOnly)

The DialogResult dr argument includes:

  • DialogResult.Yes

  • DialogResult.No

  • DialogResult.Cancel

  • DialogResult.OK

  • DialogResult.Abort

  • DialogResult.Retry

  • DialogResult.Ignore

This automated message box scheme should meet 99 percent of your needs as a programmer. But if you look closely at the message box provided by Visual Studio C#, you soon realize that every reply to be received from the user must fit into the vocabulary Yes, No, Cancel, OK, Abort, Retry, or Ignore.

What if the question that has to be asked of the user is In what decibel range do you want the speakers to broadcast? In this case you are forced to build your own message box. This next section shows you how to do so.

 


Unlocking Microsoft C# V 2.0 Programming Secrets
Unlocking Microsoft C# V 2.0 Programming Secrets (Wordware Applications Library)
ISBN: 1556220979
EAN: 2147483647
Year: 2005
Pages: 129

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net