Now that we have created the first application in VC++, let’s continue exploring different controls and features of VC++.
First lets explorer different properties of textbox.
In the HelloWorld application you have created, double click the hello button to get its code section. Now add the following code after the existing code.
textBox1->ForeColor = Color::Red;
The ForeColor property of textbox (or any control for that matter) will change the font (or anything that appears in the front of control) to the color specified. Notice the right hand side of assignment operator =. We are using inbuilt class in VC++ to asign forecolor to textbox. When you press Color:: you will get drop down menu showing many options (colors) available for you to choose from.
Run the program and click on the hello button. You will see the difference that the text color has changed to red.
In this way you can change forecolor property of any control dynamically ( that is in response to some event like button click). You can also change background color of any control similarly by changing the BackColor property of the control. A good way of learning VC++ is trying different properties that appear in the drop down menu. If you don’t understand what each of the property means then just search them in the help. You can manually search the help section or just select the property and press F1.
The help provided in VC++ is very good. You get detailed description of the help you need with examples to illustrate the problem (most of them are). So it’s a good way of learning the tool. Just master using the help and you can solve any problem you encounter while using VC++.
Ok after knowing a bit about using the help in VC++, let us try some other controls available to us.
Let’s take a look at Label control. Label control is control that is usually used to describe some other control for the user benefits. Let’s add a label to describe the textbox control in our HelloWorld application. Click on the Label control from Toolbox and drop it left of textbox control. Align it using the snap lines. You may notice red line at one time. This means that label will be aligned vertically to match the text alignment of the textbox. The form must look something like this after the process.
Change the text of the label to Textbox so that it describes the text box in front of it. We notice that the label control cannot be resized. This is because the AutoSize property of the control is true. Change it to False to enable resizing of the label. But it is better to have it True as the IDE will resize it automatically to fit the text of the control. Still you can resize the control by changing the AutoSize property to false.
Run the project and see the result. Usually label controls don’t have any code associated with them. But you can have code related to them if the application demands it.
Now lets explorer another window control : ComboBox. A combobox is a control that offers us drop down menu (similar to the like Intellisense). First drag and drop a ComboBox from the Toolbox to the form below textbox. Add a label in front of it and change its text to “Color”. Move the controls and align it in a way you want. The form may look something like this.
Now add few items in the combobox. For this, search for the Items property in the property window and click on the small button with three dots at the right of the property. Add following items in the list. After each item hit enter.
Red
Blue
Green
Yellow
The screen will look like.
Click Ok to finish adding menus.
Now add another button to the form and change its text property to Change Color.
Now double click on the Change Color button. Add following code inside the braces.
if(comboBox1->SelectedIndex == 0)
textBox1->ForeColor = Color::Red;
else if(comboBox1->SelectedIndex == 1)
textBox1->ForeColor = Color::Blue;
else if(comboBox1->SelectedIndex == 2)
textBox1->ForeColor = Color::Green;
else if(comboBox1->SelectedIndex == 3)
textBox1->ForeColor = Color::Yellow;
Delete the previous code you inserted in Hello Button ( i.e. textBox1->ForeColor = Color::Red;).
Run the project. When you click hello button you will get the text in default black. Now click on the combobox you added. Click on the small inverted triangle on the right. You will get a drop down menu. Choose any of the option. Lets say you choose Blue. Now click on Change Color Button. The text of the textbox changes to blue!.
More detailed usage of VC++ is made in next Update!! Read on!! :)
If you have any queries then post here in the blog.












Nice!!! good work!!
ReplyDelete