System Requirements and Installation / Adding More Application Functionality
Subtitles of the Movie
Now let's go and do something just a little bit different in our application. This time we're going to change the property of the label text, that sets the color of the text and let the user submit any color they wish and the text will change that color. Now two things to keep in mind, if the user doesn't enter a known color, then nothing will happen, if the user does enter a known color, the text color will change. So first let's change the text property of label two, and have it say 'Please enter a color to change this text to', and we're going to have to enlarge this control just a little bit so that the text will still fit all on one line. Now that we've changed the text property, go into our code behind page, and remove the existing code, in this case we simply want to change the fore color property of label two, to the color that was entered by the user. So we can start by writing label two. and then putting the property in. You can either type the property in, or you can select it from this list, if you select it from the list, you know you're going to get an actual property or method, with no misspellings that belongs to this control, so double click on fore color. And now we've specified the control and the property that we want to change, in order to change it we set it equal to the color entered by the user. Well you might think that we can simply put text box one, text and then that will take care of our problem. But if you notice, we immediately get a blue squiggly line, that is an indication that this isn't going to work. To see the error message put your cursor on top of it, notice what it says, Value of type ‘string', that's the value that's being entered by the user in the text box, cannot be converted to ‘System Drawing Color', and that brings up an interesting point, each control that you put on your form, has a variety of Properties window, and some of these Properties window can be represented as strings, such as the text property, but some of these Properties window are only represented by specialized data types, such as in our case System Font Color, therefore what we need to do, is convert what the user enters into the appropriate data type, and in this particular case, we can do that by specifying a color, which will be generated using the method from name and this method gets it's parameter or it's argument from what the user has entered. So, inside the parenthesis, we enter TextBox1.text, that pulls the string that the user enters into this method, from name, based on this passed color and that is what label two. Fore color is set to when the code runs, so let's see if this code works, go back to the web form, click file, build and browse, takes a second or two while it's recompiling, and now we can enter a color, so let's enter red. Sure enough the text changes to red. Let's enter green, and the text changes to green. Now you might be wondering how do I know what the known colors are, well there's an easy way to find out, and if you need more information you can always go back to the Visual Studio .NET documentation. Just look under Web server controls, let's go ahead and get out of the browse view, and click on label two. Click in the fore color property, and click on web colors, you can see there's quite a few colors in here, choose red, it will show you a valid color name right there. These color names are all valid, so if the user enters any of these names, the text will change to that color, If they enter something that's not in this list, also called an enumeration, it's not in this list, it will go to a default, which is black. And let's close the project, we'll save all this. By the way, here's a quick tip, the next time you open up Visual Studio, you can easily get back to that same place by going to file, recent projects, and finding the most recent project that you've been working on, and it opens up the code behind page, this is how you left the project and going back, it basically takes you back to exactly the same place that you were. So let's go ahead and close it up one more time. In our next segment we'll be studying programming.
Tutorial Information
| Course: | Microsoft Visual Studio .NET |
| Author: | Dave Mercer |
| SKU: | 33420 |
| ISBN: | 1932072276 |
| Release Date: | 2003-04-01 |
| Duration: | 7 hrs / 101 lessons |
| Work Files: |
Yes |
| Captions: | For Online University members only |
| 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 