IDEAssignment 2

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 5

IDE Practical Assignment 2

This assignment is meant to get you acquainted with


What files Visual Studio is producing
Resources
References

Task 1 Creating an application in Visual Studio

Start Visual Studio and create a new Windows Forms Application with the Name
IDE_week2_Forms_project in Location IDE\Assignment2 and in Solution
IDE_week2_Solution.
Do this by choosing File New Project Windows Forms Application.
Design Form1 in such a way that it looks exactly as the screenshot below (note: all
buttons have size 100;25 and the white area below button Call Class1 is a label):

Run the program (of course, the buttons have no function yet).
Then open its folder (IDE\Assignment2) in Explorer, and show all the files
and folders which are there now.
bin
obj
Properties
Form1.cs
Form1.Designer.cs
Form1
IDE_week2_Forms_project.csproj
Program.cs

IDE Assignment 2

Page 2

Task 2 Inspecting the .csproj project file

Open the .csproj file with Notepad. It is an XML file that contains information
about the project.
In Visual Studio, show the Project Properties (Project Properties).
Compare these options with the information that you found in the .csproj file to
see that they are basically the same.

Task 3 Inspecting the .Designer.cs file

The settings for a form or a component (like the Size, Location, Text, etc) are stored
in a file with extension .Designer.cs We have already seen this file type in
Assignment 1. Open file Form1.Designer.cs and compare the code that you see
there with the settings in the Properties Window of Form1 to see the similarities.
In the text, change the size of the 2 buttons on the top left (Change icon and Reset
icon) in such a way that they have a width of 75 instead of 100.
Verify that the Size of these buttons is now also changed in their Properties Window.
This shows us that Form1.Designer.cs and the Properties Window of Form1 are
always synchronized to each other.

Task 4 Adding a form to the application and making it visible

Add a new form to the application with Add New Item Windows Form.
Verify that a new form (Form2) has indeed been added to the project.
Run the program. You dont see the new window yet. Change the program such that
Form2 becomes visible when the button Open new form is clicked (instantiate class
Form2, and use attribute Visible or method Show of Form2).

Task 5 Adding icons to the application

We will now give each window in the application (Form1


and Form2) its own icon. Open the resource editor (by
double-clicking on Resources.resx) and create 2
different icons (make them yourself, as you like) with the
names ICON1 and ICON2.
Just like in Task 1, show all the files and folders
which are now in the project folder. Mark the files that
were added after Task 1.
bin
obj

IDE Assignment 2

Page 3

Properties
Resources
Form1.cs
Form1.Designer.cs
Form1
Form2.cs
Form2.Designer.cs
IDE_week2_Forms_project
Program.cs

Task 6 Using the icons in the application

Now we want to achieve the following:


ICON2 must be shown in Form2.
When button Change icon is clicked, icon ICON1 must be shown in Form1.
When button Reset icon is clicked, the original icon must be shown again in
Form1.
Add the necessary code and test it.

Task 7 Localizing an application

We now want to localize our application, which means that the texts on the buttons
and the forms title bar are changed to another language.
Read the following introduction to localization, and also this one.
Choose a language you know (not English) and localize your application to that
second language also.
You will see that an extra resource file is created, like Form1.nl.resx in the case
your second language is Dutch. Double-click that file and see what is in there.
As described in the above links: in order to change from one language to the other,
you have to stop the application, make a small change in the code, and then start
again. In this way, try out both languages.
Next we want to be able to change the language without having to restart the
application. For that, use the following method (taken from this article):
private void ChangeLanguage(string lang)
{
foreach (Control c in this.Controls)
{
ComponentResourceManager resources =
new ComponentResourceManager(typeof(Form1));
resources.ApplyResources(c,c.Name,new CultureInfo(lang));
}

IDE Assignment 2

Page 4

}
The argument lang must specify the language like "en", "en-US", "nl", etc.
Add this method to your Form1 class and then use it such that the language
changes from English to the other language when the button Change language is
clicked.
Try to make it such that the language changes back to English when the button is
clicked again.

Task 8 Using references

In Assignment 1, you saw how a reference can be used in the command line.
In that case, we made a program (ReUse) that referenced 2 classes (Class1 and
Class2) from another assembly.
We will now use references inside Visual Studio, which can be done in 2 ways:
a. We can make a reference to an assembly
b. We can make a reference to a project in the same solution
Add the following code to button Call Class1 (we assume that the label has name
label1):
int[] numbers = new int[10];
Class1.MakeRandomNumber(numbers);
label1.Text = numbers[0]+", "+numbers[1]+", "+numbers[2];
This will not work, because Class1 is not available in this project.
We will try out references in both ways:
a. Make a reference to an assembly
Take again the file Histogram.cs (of week 1) from SharePoint and put it
somewhere on your disk. Make the class Class1 public and then compile on
the command line to get the executable file Histogram.exe.
Add a reference to Histogram.exe by right-clicking on References in the
Solution Explorer. Choose tab Browse and browse to Histogram.exe. We
can now use the class Class1 in our project, so the button Call Class1
should now work.
As an experiment, remove the file Histogram.exe and run the application
again. Now, it will not work, which shows us that the class Class1 is indeed
taken from Histogram.exe.

IDE Assignment 2

Page 5

b. Make a reference to a project


In Visual Studio, remove the reference to the assembly which you made in
step a. Make sure that the assembly is also removed from the folder.
Create a new Console Application inside the same solution (which you gave
the name IDE_week2_Solution in Task 1).
Verify that the new project is now shown in the Solution Explorer.
Add the file Histogram.cs to the Console Application with Add Existing
Item and remove Program.cs from that same application.
What happened exactly: was the file Histogram.cs copied to the folder
of the Console Application, or is the Console Application using the
original file Histogram.cs?
The file Histogram.cs was copied to the folder of the Console Application.

Build (dont run) the Console Application. This should not give any error.
Now go back to the first project (which you gave the name
IDE_week2_Forms_project) and in that project, add a reference to the
Console application with Add Reference Project tab.
As a result, you should again be able to run the Forms application and the
button Call Class1 should work.

Show the answers for the questions in Task 1, 5 and


8, and the program of Task 8b to the teacher.

You might also like