




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Introduction to C# programming
Typology: Exercises
1 / 8
This page cannot be seen from the preview
Don't miss anything!





Programming is not difficult, but it does require practice and an attention to detail. In addition, programming comes easily to some, but not to all, but everyone can learn to program! We learn the principles of designing good programs in the class/lecture sessions; we now implement these in a programming language in the lab sessions. We will use C# for this purpose. C# is a programming language that is used in business more often than others that tend to be used for more technical applications. We will have a lab session each week, lasting up to 3 hours, where you take the class example produced in the lecture in the previous week and create the C# solution for that example. Before the pandemic, you would have been able to do the work in Wits’ computer labs, with the physical presence and assistance of tutors; you will now have to do the work more independently. This is not entirely new, as many students would often choose to do the practical lab work at home or in their own time. However, there are tutors available to assist remotely on a Monday afternoon. The 1st^ session has step-by-step instructions, but thereafter there will be a short brief only, with some notes to help you along for each session. Besides doing what the briefs say, you might also want to play around and see what else is available in C#, but I will be expecting you to do what each brief says as a minimum. You are able to use the internet (however, see my comment in the course outline about using online resources) as well as ask each other, as sometimes a different explanation can help. Use the briefs and notes I give you as the starting point. Then if you need more assistance, speak to the tutors, to me or to each other. Remember that each lab submission has a 1% allocation. The submission does not have to be perfect, but a reasonable and decent attempt must be made to earn the mark. The earlier labs are (obviously) easier and quicker to do, but try your best to keep up so that you do not have any issues just before the tests. Although I will give you 10 days within which to submit each lab, I will be posting feedback of submissions earlier than that hopefully, and particularly before the tests, which, if you haven’t yet done the work, will be less useful and might not include feedback on your submission. This is another reason to rather do the lab on the day (or at least within the week) that the work is scheduled. As this is the 1st^ time I will be doing the ISIB practical work remotely, I will be providing more guidance (in the form of notes together with the briefs) than I would if we were doing the practical work in a lab with tutors to guide you, so we may find that this is too much or too little, but please give me feedback as you go along. It does mean there will be more reading for you. Remember also that because attention to detail is important, reading carefully instead of just scanning over the notes is important. Another complication is that, while I know how the software works in the labs, and all software is loaded onto the machines in the same way, it is more difficult when each person may have the software loaded in a particular way and may have a different device with different constraints and settings. INTRODUCTION TO C# Choose/click on Visual Studio (this depends on where you have saved the software and whether you have created a shortcut). Then choose Create a new project. In the drop down lists at the top, choose C# - Windows - Desktop for Languages, Platforms and Project types respectively. Then choose Windows Forms App and click Next (this means we are developing a program using forms and windows). Once you click Next, you will get a screen that asks for the details of your new project (see screenshot below – your location will be different). Overtype WinFormsApp1 with ClassExample1 for the Name of your project/solution. If you do not type in anything, it will default to calling the project WinFormsApp1, and each subsequent project will be …App2, … App3, etc. This is very sloppy and will not help you when you are looking for a particular project/solution later, so please give the project a meaningful name. There should be no spaces in the name. Underneath that type in the location where you want your projects to be saved. This location is VERY IMPORTANT for finding the project later so make a note of the location so you can find your project later.
Then click Next and Create on the following screen. You should have a screen similar to the one below. Under the image is an explanation of the different parts of the screen and what they are used for. Every item that we place on the form is a type of control. The Toolbox is the area where we get the different controls to put on the form. If the Toolbox on the left of the screen is not visible (but it says Toolbox vertically along the left of the screen), click where that vertical tab says Toolbox (on extreme left). The Toolbox should become visible. To make it stay visible, click on the pushpin (Auto Hide). Do the same if the Solution Explorer and/or Properties parts of the screen are not visible on the right hand side of the screen. The Solution Explorer is where you can view the various files of the project and solution. The Properties section is where you can view and change the descriptions and appearance of each of the controls. If the Toolbox, Solution Explorer or Properties parts of the screen are not visible at all, you should go to the main menu bar at the top of the screen and click View, then Toolbox, Solution Explorer and/or Properties Window. In the Toolbox, you can click on the various arrow heads to show or hide the controls under each “heading”. To start with, we will be using the controls under Common Windows Forms, so perhaps hide the rest for now. You will now develop the solution to Class Example 1.
Go to the Properties window, find (Name) under Design, double click on (Name) and type in txtPhoneNo (this names our text box). Again, this is for input, for the user to enter data on the form, for input to the program. Go to the Toolbox, click on Label, then click on the form (adding another label for our message). Go to the Properties window, double click on Text and type in XXXXX (this places the XXXXX where our message will display). Also in the Properties window, find (Name), double click on (Name) and type in lblMessage (this names our message area) (note the prefix is “ell bee ell” – not ones!) Labels are used for titles, headings, etc., but are also used for output. This output means that the user cannot change it once it is shown. Go to the Toolbox, click on Button, then click on the form (this will place a button on the form). Each time you can drag to (re-)size the controls you add to the form. Go to the Properties window, find (Name), double click on (Name) and type in btnProcess (this names our button). Also in the Properties window, double click on Text and type in Process (this is the text that will appear on the button). What we have done is placed “controls” on our form and given them names and/or text where appropriate (the names and/or text are the properties or descriptions for each of the controls). Labels that we do not need to reference in the program do not need names (for example, the heading and the label for the prompts). Note that I have suggested you use prefixes for all control names (txt for textboxes, lbl for labels, btn for buttons) which is good programming practice. You can arrange the controls (by dragging them) so that they are well spaced and neat (remember that this is the form that the user will see, so it should look “professional”, but don’t spend too long on this). You should have a form that looks similar to the one below. Perhaps Save All again now, in case you haven’t. What we have done up to now is DEVELOP THE FORM. This is step FIVE a) of The Process of Producing Programs. We now will CODE THE SOLUTION. This will be step FIVE b). A quick note: when we develop the solutions in pseudocode (and later in our method outlines), we use the words Accept and Display to input and output data and information. C# does not have these words so we use the forms and the code (in a different way) to carry out these functions. Writing code for the program Double click on the Process button (this opens up the editor so you can write the code for the process button).
Within the curly brackets (where the cursor was placed), type in (EXACTLY as given below)
You should have the following code
This is displaying the message together with the name that was input (accepted) into the text box on the form. Note that there are no words saying accept or display. There is probably a warning message about using a small “b”in btnProcess (some dots under the b) – you can ignore that. They prefer to start names with uppercase, but I prefer to use lowercase for the prefix. Note the syntax. Each statement in C# ends with a semi-colon, and groups of statements are in curly brackets. For now the curly brackets will enclose the entire method that you code (for each button), but later we will use curly brackets for ifs and whiles as well. The code starting with private void is a method and it starts with that line of heading, followed by curly brackets – within these brackets will be the code you write for this method. Click on the Design tab above your form (called Form1.cs [Design] ) to go back to the form. Note there are now 2 tabs, the Design tab and the code tab. You can move between them by clicking the tabs. Note also that when there is an asterisk next to the name(s), the form and/or form design have not been saved. Click on the Save All icon as before to save.
avoid having many empty methods (which again is sloppy and unprofessional), as you will find you double click often by mistake. Save All at this point and then you can play around with various properties. Changing more properties of the form Change the Background colour (BackColor under Appearance) of the form to any other colour (make sure you are on the form and not within any control). Change the StartPosition (under Layout) on the form to CenterScreen. The form then appears in the centre of the screen when we run it. Change the Foreground colour of the buttons (ForeColor under Appearance) to any other colour). Include a PictureBox (from the Toolbox) on your form. Find a picture on Google (or elsewhere) that would be suitable for your form. Include it by clicking on the Image property of the PictureBox, then clicking on the ellipsis (the 3 dots on the extreme right). This will allow you to Import the picture onto your form. You will have to provide its location. You may also need to re-size/move parts of your form and/or re-size the picture, depending on the size of the image. If you struggle to size or include the picture, do not worry; this is just for some extra “playing around” and is not critical for our programs. You can also play with the properties of all of the controls, by changing them to see what happens. Save All and run again each time, by entering different names and clicking Process, then clicking Clear. Finally to stop running the program, you can click the Exit button. Also close the project/solution by clicking on the cross in the top right of the screen. Make sure you can retrieve/open it again, by looking in the location that you saved it. Zipping and uploading to Ulwazi Besides the instructions given below, you should also read the document “How to upload and download C# projects” for a more detailed description of the process. Close the project, then go to the highest level folder of your project – there should be a folder called ClassExample1. If there is a file with the type Microsoft Visual Studio Solution under the same folder, go back a level so there is only the one folder name in the list. Right click on the folder. Choose 7 - Zip then choose Add to “ClassExample1.zip”. Please DO NOT use .rar or .7z compression formats. A compressed (zipped) folder should appear in the same location where you are working. Right click on the folder and Rename it to your student number. Upload the zipped folder to Assignments/Lab 1 in Ulwazi. You have to upload zipped folders that include all the files, so this process is very important, else I cannot access and/or run your programs. NOTE WELL: There are many instructions that are given in this document that you will need to follow/carry out for ALL your future programs. These include the following: naming of the program and controls saving the program (both using Save All and specifying the location in which you will save your program) the fact that no given code should be overtyped or deleted the zipping and uploading of your program You will not be given step-by-step instructions for future examples, so you will need to create, develop, save, run and upload future programs following these guidelines. Re-do this example again if necessary to make sure you know how to do these things. After a few sessions I will show you how to copy an entire project/solution, but for now you need to be able to create and develop a solution from scratch. Some additional tips/notes: C# is case sensitive, so if Text needs a capital T, it must be a capital!
C# makes use of Intellisense, which is a type of code completer. If it knows the possible completions for your code, the suggestions will pop up as you type. As you continue typing, the list of the possibilities is filtered to include only what is possible. Pressing Tab or Enter will insert the selected possibility, so you don’t have to type it. Syntax is VERY important in C#; this includes brackets, semi-colons, curly brackets, etc. Again, this means an attention to detail is vital. It also means you should not delete any code that is given. Remember “do not be afraid to fail, be afraid to try!”. Have fun!