Practical Programming Practicals Practical 1 First Programs in VB.NET (2005) This practical will introduce you using the Visual Basic system, and building your first programs. The specific topics covered in programming are: Putting controls onto forms, using ‘event’ routines, and changing ‘property values’. Before the Lab Session No special preparation is required for this laboratory session. If you have not used any of the labs in R Block before, you should first read and work through Practical 0: Laboratory introduction to learn how to use the lab environment. Getting Started with Visual Basic .Net In your home folder you might like to create a new folder called 153. You can save all of your work for this course in this folder. You will find that you end up with a large number of files during the course of the year, and it is helpful to have them organised. You might like to create a folder Lab1 within the 153 folder for this week’s work. 1. To start Visual Basic .net select Start Programs Microsoft Visual Studio 2005 Microsoft Visual Studio 2005 You will need to be patient. The Visual Basic .Net system takes quite a while to ‘launch’. Much of this delay is because you are using it for the first time and it needs to configure itself. If you stop it, starting again is much quicker, although you will always find that the first time you start it in a new lab session is quite slow. 2. To start writing a program. Click on the File menu then New then ‘New Project’. You should see a window like Figure 2. The Visual Studio system provides a great deal of assistance when writing programs. For this reason, it starts by asking exactly what kind of program you are writing. The list at the left of the window shows programming languages you can use – you want to use Visual Basic. 27 Figure 1. VB.Net after selecting File -> New Project Figure 2. Ready to start a new project Practical Programming Practicals Because Visual Studio knows from your profile that you are a Visual Basic Developer, it should have highlighted that option, as shown in Figure 2. If not, click on it. The panel to the right shows different kinds of programs and pieces of software you can build in Visual Basic. You are going to build a ‘Window Application’ which is the top left option in the right hand panel. If it is not highlighted, click on it. All that remains is to give your project a name, and say where you want it saved. The name is best written as letters and digits without spacing or other punctuation. Try using Lab1a, then later programs you write in this lab can be Lab1b, etc. Normally you would give a program a more descriptive name, but these programs are just exercises. Click in the ‘name’ box and type in Lab1a. To choose a location to save the program click on the ‘Browse …’ button. Navigate to your home directory the 153 (and possibly Lab1 folder). To close the file browser window, click the ‘Open’ button. (That doesn’t make much sense, but it will work.) Click on the ‘OK’ button on the New Project window. You should see something like Fig 3. Figure 3. Just started a new project. 3. At this point you have created a new program and you can test it. To test you must ‘compile’ (translate the program into machine instructions and store it in a file, ready to be run), and ‘run’ it. Select ‘Build Solution’ from the Build menu. It will take a moment or two to do (tends to take longer the first time you do this) and at the bottom of the screen you should see Build: 1 succeeded, 0 failed, 0 skipped 29 You can know ‘run’ the program. Try selecting ‘Start without Debugging’ from the ‘Debug’ menu. After a bit of reorganisation you should see your program running – just an empty window with ‘Form1’ in its title bar. Not very interesting, but you didn’t have to do much programming to get to this point. Visual Studio has built you a minimal program that is just capable of getting a window displayed on the screen. Note: You will find that Visual Studio will not do much until you close your program. It is always necessary to close down the program you are testing before you can do more work. 4. Exploration: Look at your files. You will find that your new programming project has put a lot of new files into your space. Find the ‘bin’ folder. Inside that you should see a Debug folder and inside that you should be able to find ‘Lab1a.exe’ (the name will be different if you chose a different name for your project). That is your finished program. If you double click on it you should be able to run it. Answer question 1 on the review page. Controls, Properties, Events. In Visual Basic you create a program by dragging and positioning ‘Controls’ onto your form; setting ‘Properties’ of those controls; and writing program instructions to handle ‘Events’ associated with each control. Controls are things like buttons, text labels, and boxes into which we can type text – in other words the items we find on windows, and that we interact with when using programs. Properties are details which we can vary – for example the size and position of a control on our window. Events allow us to perform some program instructions when something happens, like a person clicking on a button. Controls On the left side of your screen, in a small button with the writing running down the screen you should see the word ‘Toolbox’. If you move the mouse over the word you should see a panel ‘slide out’ from the left edge of the screen. If the word or the ‘Toolbox’ panel is not present, select ‘Toolbox’ from the ‘View’ menu. 5. Exploration: See if you can work out how the Toolbox panel display works. You can have the toolbox panel slide in and out or be permanently out (use the pin icon at the top of the panel). Find out when it slides in and out. You can have the panel or not – use the X to close it. Use the ‘View’ menu to get it back. EXPERIMENT until you are comfortable with its behaviour. Answer review question 2. In this lab we will make use of three controls: They are ‘Button’, ‘Label’, and ‘Textbox’. All three occur near the top of the toolbox panel. 6. To put a control onto a form. You can either: double click on the control, which will put one into the top left hand corner of your form; or single click to select the control and then click and drag on the form to place a control and drag it to the size you want. Try putting some buttons onto your form. Can you get something like this (exact size and position is not important)? Try running your program. Practical Programming Practicals Figure 4: Some buttons on a form 7. Try putting labels and text boxes on a form. Properties On the right hand side of your screen should be the word ‘Properties’, this gives access to a panel just like the toolbox panel. It will probably share the height of the screen with the ‘Solution Explorer’ panel. (You can alter the way the height is shared by clicking on the middle line and dragging it.} Experiment with the panel display. When a particular control is highlighted, its ‘properties’ will be shown in the property panel. There are many properties for each control. You will find that you become familiar with some and learn to ignore the rest. 8. Exploration: Experiment with the following properties of buttons: Text, TextAlign, Visible, Enabled, Location, Font. Try the Anchor property for more of a challenge. You may need to run your program to see an effect. If you are unsure about a property – highlight it and press button F1 – what happens? 9. Try setting properties of the Form itself (how do you do that?) What does the Text property do? 10. Deleting controls from your form: Highlight the control and press the Del key. 11. Undoing actions: You will find that selecting ‘Undo’ from the Edit menu, or holding down a ‘Ctrl’ key and pressing ‘z’, will undo your last action, whether it be placing a control or changing a control property. Experiment. What happens when you ‘undo’ more than once? 31 A special property of all controls (and of the form itself) is the control ‘(Name)’. 12. Find the ‘name’ of one of your button controls. You will find that changing the ‘name’ of a control has no visible effect. The ‘name’ is the way in which we refer to a control when we write program instructions. Events 13. Make sure that your form has two buttons. Name one button ‘One’ and the other ‘Two’. Double click on the button called ‘One’. You should get something like that shown in Fig 5. Figure 5: The ‘code’ window.- where we type program instructions This is a procedure (some instructions to be run) when button One is pressed. You can refer to controls by name and alter properties in this procedure. Try typing an instruction into the procedure – so that it reads as follows: Private Sub One_Click(. . .) Handles One.Click Two.Text = “I’ve been changed” Two.Width = 200 End Sub ie: You can access any property of a named control and change it. The change happens when the button is clicked. Try it. Notes: This is the first kind of statement we will see – the ‘assignment statement’ – more in lectures on the detail. For the moment it takes the form control name . property = value Practical Programming Practicals where value can be text – enclosed on double quote marks, or a number, or a property value from another control. 14. Challenge: Can you work out how to change button two to have the same width as button one when button one is clicked. 33 Practical 1: REVIEW PAGE Name: ______________________ Questions: Complete the following questions. (Note: this must be done before you seek a verification). 1. Where is your finished program, what is it called and how do you run it? Demonstrate running it to your demonstrator. 2. What is the role of the ‘pin’ icon at the top of the toolbox panel (or any other panel)? 3. Explain and demonstrate putting a label onto a form with the text ‘Hello’. 4. How do you change the width of a button using a property (instead of just dragging it)? 5. What does the ‘Text’ property do for the Form itself? 6. What does the sample program in section 14 do? 7. How do you make the two buttons have the same width? Section 15. Verification: To assess your competency of this material your demonstrator will verify that you have answered the set questions. Grade: 0 – 4 and Signature – for Demonstrator to Fill In