Message Boxes / Complex Message Boxes
Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.
Learn More
Subtitles of the Movie
When displaying a message box, you may want to be able to display a message which is longer than just one line. For example, if I were to run my sample program that I modified to display a three line message, as you can see, it simply says this is a sample message, second line of message and third line of message. Now, in reality you can have an actual meaningful message there rather than my kind of dummy messages. This is accomplished, or can be accomplished in a number of ways. Looking at the code that I have written, you can see that I have the messagebox.show with the first line of my message. Now I have created a concatenation symbol, which is the ampersand and then I've used a class of code called control characters and I'll retype this in so you can see how it looks when you type it in. Control chars. And here's you're given a choice of various control characters you can insert into your text. I'm going to choose New Line because I wish the next line to be on the next line or a new line. So again, I then concatenate or join together the second line of the message with the ampersand, type in the text within quotation marks, repeat the process between the second and third lines to create a new line. And then here's the third line of the message and then the rest of it is the code for the remainder of the message box itself. But we want to focus on the multiple-line message. Now, one problem with this, as you can see, is that your code tends to scroll off the screen, making it more difficult to work with. This will work, but one of the things you do need to remember is if you reach the end of the line and you wish for the next line to be continued, you need the continuation symbol, which is the underscore. There must be a space before and after the underscore to allow the next line to be considered part of the preceding line. Now, we've seen that this does indeed work, but there is a better way of accomplishing this process. The preferred method would be to first create a variable, such as message string, as a string whose purpose will be to contain the message and then we will display message string in its place. For example, if I let message string equal and then what I will do is I will cut and paste the actual message that's currently in my message box, such as this. Now we have the message inside of the variable called message string, complete with the concatenation symbols, complete with the control characters.new line. The problem is now as you can see is that there is not parameter for the message within the message box to show method. What we need to do, of course, is insert the variable message string within that location which was previously occupied by the message itself. Now, what this allows you to do is this allows you to manipulate and to work on the message itself without having to bother with the messagebox.show statement per se. We'll run this program to make sure it still works before we proceed with further modifications. And as you can see, it does work just as it did before. So to the user it makes no difference which technique you use within your code, but it does make a big difference in terms of your readability and the maintenance of your code. Now, you may have noticed that the three-line message was one line after another. That is they were single spaced. If I want to create an additional new line or additional space, I could simply insert another control character.new line, making sure that I have an ampersand before and after each symbol. Once again, I need to be aware of if I drop down to the next line, I need an underscore below that so that, or after that so that the lines below that will be considered part of that same line of code. So this gives me the ability to work with the message without having to change the messagebox.show statement itself. So now I've inserted two new line characters between the first line and the second line. But I only have one new line between the second and third lines. So we'll see how that looks to the user when we run our program. So now you see the first message line. Then there's a blank line, which is that additional new line character, which I inserted and then here is the second and third line with no additional blank line. So this entirely depends on the way you wish for this to appear. Of course, if I want the blank line to appear between the second and third line, all I have to do is to insert another control characters.new line, again making sure that I am concatenating it properly by using ampersands between each component of the message itself. So currently my message string reads this is a sample message followed by one new line, followed by another new line, followed by the second line of the message, followed by a new line, followed by another new line and then the third line. Let's take a look at this at run time. So we run this. Here is the result. You see the three lines of messages displayed with a blank line between each one. Of course, you could control this by simply modifying your code to achieve the desired result. The key here is using the variable message string. By using the variable message string, you're now creating a much easier method by which to work with the message and creating a much more user-friendly message without having to alter the contents of the messagebox.show statement itself.
Tutorial Information
| Course: | Microsoft Visual Basic 2005 |
| Author: | Arthur Lee |
| SKU: | 33940 |
| ISBN: | 1-935320-10-6 |
| Release Date: | 2008-11-19 |
| Duration: | 7.5 hrs / 97 lessons |
| Work Files: |
Yes |
| Captions: | Available on CD and Online University |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 98,729 Video Tutorials (23,265 free)
- Video Available as Flash or QuickTime
- Over 1026 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 