You should be looking at the Visual BASIC IDE with a gray box in the left hand corner. As mentioned previously, this gray box is known as a form. With the form selected, go into the properties window and change the (Name) property to frmMain and change the Text property to Hello. To change these properties, first find them on the left hand side of the Properties Window, then on the right is where you would enter the new values. The (Name) property for items is how we reference them when programming, whereas the Text property is the text that’s shown. In your Toolbox, make sure you’re displaying All Windows Forms. Click on label and then click and drag in your form to bring it there (you could have also double clicked on the word label). Change the label’s Name property to lblHello and the Text property to nothing. (Note: we only change Now go back to the toolbox and add a button. Change the text property of Button1 to Click Me and the Name property to btnClick.
Double click on the button you just added, and you will be in Code View. Your cursor should be in the btnClick_Click event procedure (an event procedure is when something happens to the object for example when you click the button this will happen). Making sure you are between the Private Sub and the End Sub, type lblhello.text=”Hello” The whole code should look like this:
Public Class FrmHello Private Sub btnClick_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClick.Click lblHello.Text = “Hello” End Sub End Class |
The word sub indicates that this is a subprocedure, subroutine, or module. Because of this, variables declared or changed in here won’t be accessible to other subroutines unless we tell it to be. The line of code that has blHello.Text = “Hello”, is common in Visual BASIC. Every component in Visual BASIC is an object and therefore must be referenced as that. You will notice that lblHello is the name you gave it, whereas the .Text is how we reference the other properties of that particular object.
There are multiple events for every type of object. If you don’t want to use the default event, then while in Code View, click above the code on the dropdown boxes. The one to your left will be the components on your form, while the right one will be which event procedures can exist with that component.
Press F5 or go to Debug/Start Debugging. When you run the program, click the button and the label should say Hello. If it doesn’t work, or you get any syntax errors, you may have named something wrong, or types something incorrectly. Make sure you have followed the directions exactly.
When we name a component, we use a predefined prefix (lbl for Label, btn for button) to distinguish it from a variable. Here is a list of the some of the most common components, their identifiers, and what they are used for:
Component | Prefix | Purpose |
Button | btn | Commonly used to submit data |
Checkbox | chk | Used for adding a list of things that can be added to a list, without being mutually exclusive |
ComboBox | cbo | Used as a dropdown list |
Form | frm | Where all the other components go, main backdrop of your program |
GroupBox | grp | Used to group items together eg radio buttons and checkboxes |
Label | lbl | Used for outputting text |
ListBox | lst | Used for creating lists |
Menu | mnu | Standard menus, eg File, Edit |
PictureBox | pic | Used for pictures |
RadioButton | rad | Used for having select one mutually exclusive item |
TextBox | txt | Used for inputting data |
Visual BASIC is nice because there are already a lot of objects that are created for us to use. What’s also nice is that most of these objects use a lot of the same properties. Here’s a list of the commonly used components and what they’re used for:
Property | Values | Purpose |
(Name) | letters and numbers | How we refer to that component, all components must have a unique name to the form |
AllowDrop | True, False | Whether or not you can drag and drop data into that component |
AutoSize | True, False | Resizes to fit the data/components inside it automatically. |
AutoSizeMode | GrowOnly, GrowAnd Shrink | Only applies if Autosize is True, its the method to which autosize works |
BackColor | colors | Background color |
BackgroundImage | an image using the wizard | Instead of a color, you can choose to have an image as your background |
BackgroundImageLayout | Tile, Center, Stretch, Zoom, None | Applies only when BackgroundImage isn’t blank, Describes how you want the BackgroundImage to display |
Cursor | cursors | Which cursor is displayed when you mouse over that component |
Dock | Where you want it to dock | Being docked will take up a whole area with just that one component, IE a top part that is all one button |
Enabled | True, False | Whether or not the component will be available at runtime |
FlatStyle | Flat, Popup, Standard, System | Sets the style of your components |
Font | fonts | Which font you want used on your component |
ForeColor | colors | Which color you want your text to vbe |
Image | an image using a wizard | Allows you to display an image on the component |
ImageAlign | TopLeft, TopMiddle, TopRight, MiddleLeft, MiddleMiddle, MiddleRight, BottomLeft, BottomMiddle, BottomRight | Applies only when using the Image property, where the image is displayed |
Location | numbers | Width, Height of where you want the component to appear on your form |
Locked | True, False | Whether we can move and resize the component |
Margin | numbers | How much space between this components border and another one |
MaximumSize | numbers | How much we can increase the size of the component |
MinimumSize | numbers | How small we can make the component |
Modifiers | Public, Private, Protected, Friend, Protect Friend | Who can see this component |
Padding | numbers | Interior spacing of a component |
Size | numbers | What size the component is |
TabIndex | numbers | Which order does it come in when pressing the Tab button |
TabStop | True, False | Whether or not the Tab key will bring it to the component |
Text | letters and numbers | What text appears on the component |
TextAlign | TopLeft, TopMiddle, TopRight, MiddleLeft, MiddleMiddle, MiddleRight, BottomLeft, BottomMiddle, BottomRight | Used with the Text property, How the text is aligned |
TextImageRelation | Overlay, ImageAboveText, TextAboveImage, ImageBeforeText, TextBeforeImage | Works with the Image property, Used to tell where the Text is in relation to the Image |
UseCompatibleTextRendering | True, False | Used with the Text property, Whether or not the Text to be compatible with previous versions of Windows Forms |
UseMnemonic | True, False | Whether or not the letter preceding an ampersand will be used as the default key |
UseVisualStyleBackColor | True, False | Whether or not you want the background color to be the one as the default Visual Styles |
Visible | True, False | Whether or not the component will be visible at runtime |
There are other properties, but these are the ones you will most likely be dealing with when working with Visual BASIC. Most of them can be changed at runtime, (using the method shown before, changing the text of the label with the button) but the peoperties in parenthesis can only be changed at design time (the mode you are in when adding components to the form.