Lab1

279
-1

Published on

vb.net

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
279
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Lab1

  1. 1. Rapid Application Development 1 (F21RA1) Rick Dewar Lab Sheet 1 – a Gentle Introduction to Visual Basic Introduction To complete this lab you’ll need to first visit the problem manager in room 1.77 and ensure you can access the XP network. Next you’ll need to use the machines in G.46/47 or 1.53/54. Please note that at some points in the lab you will be required to get a lab helper to tick your name on a list to record that you have achieved what is required of you. These ticks will count towards your marks for the module and are very important for us to see how you are progressing. Good luck and enjoy! Getting Started Firstly, let’s start up the application called Microsoft Visual Studio .NET, see Figure 1. Figure 1 - Starting Visual Studio You should eventually see a development window something like that shown in Figure 2. Figure 2 – Development Environment Window As shown in Figure 2,create a new project. You should then see a dialogue box like that in Figure 3. Give the new project a meaningful name and save it in a sensible place on the H drive. Your H drive is a network drive that you can access from other machines. You each have private file storage Created On 15/10/2003 1 Last Modified: 07/10/2005
  2. 2. Rapid Application Development 1 (F21RA1) Rick Dewar on it that is regularly backed-up. However, Visual Studio sometimes grumbles that it doesn’t trust projects that are stored on the H drive. This is because the H drive is actually on a machine that does not run a Microsoft operating system. Fortunately, you can trust the H drive, so please ignore any warnings of this ilk. Occasionally, it might be necessary for you to store your projects on the D drive (a writable local drive) to get some functionality to run. However, beware that the D drive is volatile and public and is not a viable place you store your files. If you have to put your files on the D drive to get them to run, make sure you move them back onto your H drive at the end of the session. Figure 3 - Name and Save a New VB Windows Application Having done this you are now confronted with a blank VB form within a new project – see Figure 4. As per Figure 4, add a text box, button and label to the empty form from the toolbox on the left. If the toolbox is not visible enter the View menu and select Toolbox from the list. Now go to the properties window of the text box by right clicking on it in the central design window and selecting properties from the list that appears. These properties should then appear in the window to the right of the screen. Scroll down this list until you come to the Text attribute. Delete the word TextBox1 and you’ll see the text box in the form is now empty, once you have hit enter of have clicked elsewhere. This is called initialising the object since you are setting its initial value. In this case, you are setting it to nothing, or null as we often call it, since it essentially has no value. Next select the command button’s properties and initialise its Text attribute to “Greetings” and set the Enabled property to False. Finally, initialise the Text property of the label to null. You should then have something similar to Figure 5. Created On 15/10/2003 2 Last Modified: 07/10/2005
  3. 3. Rapid Application Development 1 (F21RA1) Rick Dewar Code Tab Figure 4 - Adding Objects to the Form Figure 5 - The form after initialisation Before we continue, let’s do the sensible thing and save our work. At this stage you should Save All. Later, as you become more confident you can just save the current form you are working on. This should highlight the fact to you that a VB project is made up of a number of files. Created On 15/10/2003 3 Last Modified: 07/10/2005
  4. 4. Rapid Application Development 1 (F21RA1) Rick Dewar Now we can run the VB program that we’ve created by selecting Start from the Debug menu. The application won’t do anything, but it’s a useful check to show that all is as it should be. Figure 6 shows what you should obtain. You can enter text into the text box and but cannot click the command button because we’ve disabled it; remember? Figure 6 - Running the VB application for the First Time When you’re finished, remember to close the running application or it will interfere with the VB project and form files you’re editing. Congratulations! You’ve created and run your first VB application. T1: Now get a lab helper to tick your name off the list to show you’ve completed this first task. Remember, these ticks count towards your overall mark for the module. Event Driven Behaviour As we’ve seen, the application doesn’t do much just now, but the form is ready for us to add certain behaviour. To begin with let’s discuss a scenario that describes what we want the application to do. First of all, it would be useful for the command button to become active when someone enters text into the text box. Next, when someone clicks the command button, we’d like a greeting to be displayed in the label. This greeting should incorporate the text entered in the text box by the user previously. In order to create such behaviour, we need to enter VB code. We can access the code window by choosing the appropriate code tab above the central form design window; see annotation in Figure 4. If the code tab is not already visible, double click one of the elements of the form in the design window, eg the text box, and the window in Figure 7 should now appear. Here you can see a Sub procedure related to the object TextBox1 and the event that might happened to it, namely that it has Created On 15/10/2003 4 Last Modified: 07/10/2005
  5. 5. Rapid Application Development 1 (F21RA1) Rick Dewar experienced a TextChanged event. If you pull down the lists in these combo boxes at the top of code window, you’ll see the other objects that you could choose from and the other events that can happen to them, respectively. Figure 7 - The Code Window for the Form There is a lot of text, terms and language in this code that will be unfamiliar to you. Please don’t panic; you will eventually and gradually learn what is important. For the moment we are making simple changes to small parts of the code. You now need to enter code (i.e. instructions) in the Sub procedure (TextBox1_TextChanged) to enable the button in our scenario. The significant line that you need to add is the second line below. If you type “button1.” (notice the full stop), you’ll see a list of valid properties appears. The full stop means you are interested in some property or component of the button. Highlight the property you want (ie enabled) and hit tab. Then enter an equals sign and another list will show valid values of that property. Clever time-saving, error-avoiding feature isn’t it? Private Sub TextBox1_TextChanged(...)... Button1.Enabled = True End Sub Notice the indentation of the second line. This makes it easier for us to read and is very important for you to remember. However, the Visual Studio environment can automatically do this sort of formatting for you. So what we’re saying is that when a change happens to TextBox1, we want to set the enabled attribute of the object Button1 to True. Try and run the application again and see what happens when you add text to the text box. Now let’s add the second part of our scenario where we display the greeting in the label. Here we want to enter: Private Sub Button1_Click(...)... Label1.Text = "Hi " + TextBox1.Text + ". How are you?" End Sub The plus signs here add strings of text together rather than numbers. Feel free to experiment with the content and formatting of this text so you can understand how it works. Having saved your work, when you run the application you should be able to see output like we have in Figure 8. Created On 15/10/2003 5 Last Modified: 07/10/2005
  6. 6. Rapid Application Development 1 (F21RA1) Rick Dewar Figure 8 - Some Simple Behaviour To enhance our scenario, let’s create a new Sub procedure that will clear the contents of the text box when the text in the label changes. To do this, select the Label1 object from the combo box at the top left of the code window and then select the TextChanged method from the right hand combo box. You will see that an empty Sub procedure has been created. Now add a line to this new procedure that will clear/nullify the text of the text box. An empty string can be created using two double quotes. Furthermore, in the same Sub, add another line of code that will disable the button when the label’s text changes. Finally, make sure you save All of the project and then create a standalone executable of the application by choosing Build Solution from the Build menu. Once you’ve done this, you can locate the application (file type .exe) in your file system and run it independently of the Visual Studio environment. T2: Now get a lab helper to tick your name off the list to show you’ve completed this series of tasks. Remember, these ticks count towards your overall mark for the module. Well done, if you’ve got this far and have had all your work ticked off. Please note that your next lab will build on the work you have done here so make sure you do not lose or delete this VB project. Created On 15/10/2003 6 Last Modified: 07/10/2005

×