CSC221 Lecture 1
CSC221 Lecture 1
Author
Tony Gaddis
4
The Application’s Form
When you select an object in the Designer, that object’s properties are
displayed in the Properties window.
10
Property Function
Name The “variable name” of the textbox control
BackColor The background colour of the component
Font The font of the text within the component
ForeColor The foreground colour of this component, affects the colour of the text
Text The text within this control
TextAlign Indicates the alignment of text within the control
Enabled If enabled = “false”, then the control cannot be manipulated during
program execution
MultiLine Controls if text can span more than one line
ReadOnly Controls whether the text can be changed or not
Important properties of a Textbox – They can be manipulated via the Properties Window 11
Property Function
Name The “variable name” of the label control
BackColor The background colour of the component
Font The font of the text within the component
ForeColor The foreground colour of this component, affects the colour of the text
Image The image that will be displayed on the control
ImageAlign The alignment of the image within the control
Text The text within this control
TextAlign Indicates the alignment of text within the control
Enabled If enabled = “false”, then the control cannot be manipulated during
program execution
Important properties of a Label – They can be manipulated via the Properties Window 12
Example 1: Design a Wage Calculator form as shown in the slide 13
Data Sample declaration Sample Usage
Type
int int num_hours num_hours = int.Parse(textBox1.Text);
string string val val = "My book";
double double total_wage = hourly_rate * num_hours;
total_wage
decimal decimal price price = 17.8m;
Another example
int num_hours = int.Parse(textBox1.Text);
price = 17.84980m * Convert.ToDecimal(num_hours);
MessageBox.Show("Price Money = " + price);
float float MessageBox.Show("Price Money = " + 17.84f *
price_money (float)num_hours);
1. It is a must you convert values received from textboxes and labels because they are
string values in their original states.
23
private void button1_Click(object sender, EventArgs e)
{
try
{
double orig_price = double.Parse(textBox1.Text);
double perct = double.Parse(textBox2.Text);
double discount = orig_price * perct / 100.0;
double new_price = orig_price - discount;
label4.Text = new_price.ToString("c");
}
catch(Exception ex)
{
MessageBox.Show("Invalid data was entered.");
}
}
28
The SizeMode property of the PictureBox control can be set to one of the following values:
Values Description
Normal Normal is the default value. The image will be positioned in the upper-left corner of the
PictureBox control. If the image is too big to fit in the PictureBox control, it will be
clipped.
StretchImage StretchImage resizes the image both horizontally and vertically to fit in the PictureBox
control. If the image is resized more in one direction than the other, it will appear
stretched.
AutoSize With AutoSize, the PictureBox control is automatically resized to fit the size of the image.
CenterImage CenterImage centers the image in the PictureBox control without resizing it.
Zoom Zoom uniformly resizes the image to fit in the PictureBox without losing its original
aspect ratio. (Aspect ratio is the image’s width to height ratio.) This causes the image to be
resized without appearing stretched.
pictureBox1
pictureBox3
pictureBox2 name_label
This pertains to the code on the next slide: on the design view, each PictureBox
control is double-clicked to fill the appropriate code that will be triggered when the
control is clicked at runtime.
Properties to be set for the controls on the Form on the previous slide 31
private void pictureBox1_Click(object sender, EventArgs e)
{
name_label.Text = "Pie Chart"; //comment
}
Code 32
Review Questions
Question 1
Look at the following list of some famous books and their authors.
Books Author
1. Gone with the Wind Margaret Mitchell
2. Roots Alex Haley
3. Atlas Shrugged Ayn Rand
Create an application that gives the authors’ name for a particular book.
The form should have three labels, one for each book.
When the user clicks a button, the application should display the name of its author in
another Label control.
Question 2
The cost of fencing a field depends upon its perimeter. Assuming that you
need to calculate the cost of fencing a rectangular field, it can be calculated as:
Create an application that allows the user to enter a field’s length and width,
and unit cost of fencing.
The application should have buttons that display the following:
Area of the field
Perimeter of the field
Cost of fencing the field 35
Question 3
36
Create an application with a form that resembles the
figure in the slide. The PictureBox controls display the
images of four fruits (a banana, an apple, an orange, and
a pear) and each fruit’s calories.
When the user clicks the Reset button, the total calories
should be reset to zero.
Question 4 37
There are three seating categories at an athletic
stadium. For a baseball game, Class A seats cost $15
each, Class B seats cost $12 each, and Class C seats
cost $9 each.
Question 5 38
Ticket Sales Revenue
Class A: 320 Class A: $4,800.00
Class B: 570 Class B: $6,840.00
Class C: 890 Class C: $8,010.00
Total Revenue: $19,650.00 – End of Set 1
Question 5 Cont’d – Test your application with the following sets of test data 39