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.

Event Procedures


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:
ComponentPrefixPurpose
ButtonbtnCommonly used to submit data
CheckboxchkUsed for adding a list of things that can be added to a list, without being mutually exclusive
ComboBoxcboUsed as a dropdown list
FormfrmWhere all the other components go, main backdrop of your program
GroupBoxgrpUsed to group items together eg radio buttons and checkboxes
LabellblUsed for outputting text
ListBoxlstUsed for creating lists
MenumnuStandard menus, eg File, Edit
PictureBoxpicUsed for pictures
RadioButton   radUsed for having select one mutually exclusive item
TextBoxtxtUsed 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:

PropertyValuesPurpose
(Name)letters and numbersHow we refer to that component, all components must have a unique name to the form
AllowDropTrue, FalseWhether or not you can drag and drop data into that component
AutoSizeTrue, FalseResizes to fit the data/components inside it automatically.
AutoSizeModeGrowOnly, GrowAnd ShrinkOnly applies if Autosize is True, its the method to which autosize works
BackColorcolorsBackground color
BackgroundImagean image using the wizardInstead of a color, you can choose to have an image as your background
BackgroundImageLayoutTile, Center, Stretch, Zoom, NoneApplies only when BackgroundImage isn't blank, Describes how you want the BackgroundImage to display
CursorcursorsWhich cursor is displayed when you mouse over that component
DockWhere you want it to dockBeing docked will take up a whole area with just that one component, IE a top part that is all one button
EnabledTrue, FalseWhether or not the component will be available at runtime
FlatStyleFlat, Popup, Standard, SystemSets the style of your components
FontfontsWhich font you want used on your component
ForeColorcolorsWhich color you want your text to vbe
Imagean image using a wizardAllows you to display an image on the component
ImageAlignTopLeft, TopMiddle, TopRight, MiddleLeft, MiddleMiddle, MiddleRight, BottomLeft, BottomMiddle, BottomRightApplies only when using the Image property, where the image is displayed
LocationnumbersWidth, Height of where you want the component to appear on your form
LockedTrue, FalseWhether we can move and resize the component
MarginnumbersHow much space between this components border and another one
MaximumSizenumbersHow much we can increase the size of the component
MinimumSizenumbersHow small we can make the component
ModifiersPublic, Private, Protected, Friend, Protect FriendWho can see this component
PaddingnumbersInterior spacing of a component
SizenumbersWhat size the component is
TabIndexnumbersWhich order does it come in when pressing the Tab button
TabStopTrue, FalseWhether or not the Tab key will bring it to the component
Textletters and numbersWhat text appears on the component
TextAlignTopLeft, TopMiddle, TopRight, MiddleLeft, MiddleMiddle, MiddleRight, BottomLeft, BottomMiddle, BottomRightUsed with the Text property, How the text is aligned
TextImageRelationOverlay, ImageAboveText, TextAboveImage, ImageBeforeText, TextBeforeImageWorks with the Image property, Used to tell where the Text is in relation to the Image
UseCompatibleTextRenderingTrue, FalseUsed with the Text property, Whether or not the Text to be compatible with previous versions of Windows Forms
UseMnemonicTrue, FalseWhether or not the letter preceding an ampersand will be used as the default key
UseVisualStyleBackColorTrue, FalseWhether or not you want the background color to be the one as the default Visual Styles
VisibleTrue, FalseWhether 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.

Next Section: Comments and Variables
Twitter






Link Directory | link directory | GeekySpeaky: Submit Your Site! | Blog Matter

Copyright © 2004-2017 Sean Noble, All rights reserved
Read our Disclosure Policy