Programming in VB Net Donna Malayeri Programming Usable
Programming in VB. Net Donna Malayeri Programming Usable Interfaces, Spring 2004
Overview of VB. Net VB is event driven. All programming is done in response to an event, such as a button click n VB. Net is completely object-oriented (even more so than VB 6) n Examples of objects: buttons, windows, listboxes. n
VB Demo Create a new project File->New->Project
Give it a project name and directory
Object-Oriented Programming n n n Almost everything is an object. You can “make requests” to an object, asking it to perform operations on itself. ¨ For example, button 1. Hide() removes a button from the screen Objects can also have data associated with them, called properties.
Classes Every object has a type, called its class. n Example of a class: Button n n The object’s class tells you what “messages” (called methods) you can send to an object ¨ the properties of an object ¨
Example n n n Suppose button 1 is of type Button We say button 1 is an instance of class Button button 1. Hide() ¨ means “send the Hide message to button 1” ¨ i. e. , call the Hide method on button 1
Methods and Properties n n n All objects of a particular class can receive the same “messages” —they have the same methods For example, Hide() can be called on Button‘s Objects of the same class have the same properties Intellisense and the online help tell you the members (properties and methods) of a class E. g. , all instances of Button have the Height property button 1. height = button 1. height + 10 increases the button’s height by 100 pixels
Show form properties using View->Properties Window In the Design category, change the name of the form to whatever you like
Show the solution explorer by View->Solution Explorer The solution explorer shows all the files in your project. You can get to your forms by doubleclicking on the form name. If you accidentally close the form window, you can get back to it using the Solution Explorer
Show the toolbox by View->Toolbox Select the label control and place it on your form The only tab that you’ll need in the Toolbox is Windows Forms, so you can ignore the others
Click on the label to see its properties Change the label text by changing the Text property Note that at the top, the class of Label 1 is shown. Ignore the “System. Windows. Forms” prefix. The class is Label
Tip: you can get a description of each property by pulling up the splitter at the bottom of the properties window. (it is all the way at the bottom by default. )
Let’s add a textbox and make it multi-line (so that the text wraps around) by setting the Multi. Line property to True. Resize the textbox.
Add another textbox either by doing the same thing as before, or copying and pasting the original one. Let’s change the font and color of the label by changing the Font and Fore. Color (textcolor) properties.
You can chose your favorite color by clicking on the Custom tab, then rightclicking one of the squares in the bottom two rows to get a color chooser. Or you can enter RGB values R, G, B (e. g. 127, 127), or hex values (e. g. , #F 0 F 0 F 0)
We are going to refer to our text fields in the code, so we should give them meaningful names. Let’s call them “Top. Textbox” and “Bottom. Textbox”. We rename them using the (Name) property. Note this is not the text in the text field that we’re changing (that is the Text property), but rather the name we use to refer to that text field in the code. Let’s set the Text property to a blank string while we’re here.
Add a button and change its Text property to “Copy”
VB also has some nice layout tools. For example, we can center the button horizontally on the form. Other options include making two objects the same height or width, or aligning them by one of their sides.
We can set constraints for the top textbox, by anchoring the Top, Left and Right to the form. Anchor the button to the Bottom and the bottom textbox to Bottom, Left, Right.
This is what happens when we run the program and resize the form.
Ignore this Double click on the button in the form to bring up the code window and its event handler. (Note: you can ignore everything that’s highlighted in the screen shot above, you won’t ever need to use it for this class. ) While we are editing this method (defined using the “Sub” keyword for historical reasons), the comboboxes at the top show the form name and method name. You should also ignore everything in the “Windows Form Designer generated code” region.
VB created a class called My. Form for us, and made it inherit from the standard System. Windows. Form. You can basically ignore this. Tip: The editor lets you expand collapse parts of your code. Clicking the “minus” icon on the left of the new method will collapse it. (The same is true for the generated code region. ) If you lose your code, chances are you accidentally clicked the minus. Just click the plus to get it back. You don’t want to click the plus next to the generated code—you’ll get two pages worth of gobbledy-gook. Rule of thumb: don’t edit auto-generated code.
Let’s add some code to sets the value of the Bottom. Textbox to the text currently in the Top. Textbox. First, at the very top, we write “Imports System”. This makes it easier to call the Debug. Write. Line method. Otherwise, we would have to use the full name, System. Diagnostics. Debug. Write. Line can be very helpful in keeping track of what your program is doing, so that you can understand why it isn’t working. It will print to the Output window when your program is running Write “Imports System” at the top of all of your code windows—otherwise you’ll get very strange error messages.
To run the program, select Debug->Start, or click the little arrow button on the toolbar.
Now let’s make a new project and add a button to the form.
We have defined the Square. Double function here. Given a double as a parameter, it squares that double and returns the value. Is. Numeric tells you if its argument is the string representation of a number. CDbl converts from a string to a double.
Here’s the output of that program
Other Concepts
Inheritance Some of the controls you will be using are enhancements of other controls n Example: Label displays a text label Link. Label displays a text label with a hyperlink n n Link. Label inherits from Label
Link. Label documentation
- Slides: 32