In Visual Basic, when you create a new Windows Forms Application an editor will open up with a blank form that you can add controls to. This area where you drag controls from the toolbox to your new form is known as ‘The Designer’. Below is a picture of the designer.
So with Windows Forms, you can drag and drop(from the toolbox), and re-size and arrange the layout of various controls, and design your program’s UI(User Interface). You will see in the image below that there are many pre-packaged controls that come with visual studio that you can drag from the toolbox onto your program’s Form(User Interface/window)
In order for a control to be useful, the control has to have a purpose. For instance, a button has the purpose of being clicked. But how would you know that button has been clicked, or that what was clicked was the button or not something else?
The answer is simple, controls in Visual Basic will raise an event when the user interacts with it. Events are also triggered by various other means. In the case of a button being clicked, the Button.Click event would be raised. With that being said, some would say that writing a Visual Basic application with Visual Studio consists of two different parts.
1.) Drawing out your user interface.
2.) Writing code for events triggered by your application.
This description would saffice for the beginner, but as you progress in your learning, you will find that even the ‘Visual’ part of writing software with Visual Basic is code, and eventually you may find that it is more practical to do almost your entire application via code verses using the design interface. Lets say for instance that we have dragged from the toolbox, a button control, and placed it on a Form as shown in the image below.
And now, lets say we have gone through the process of designing our application’s interface by dragging and arranging controls on the form, like in the image below
We have drawn an interface that allows for the saving and opening of a text file. But alone this interface is useless. Just because we have said “Open File” on a button, does not mean that the button is now able to open a file. There is more work to be done. We have only drawn the interface and not created code for performing file operations, but the design of the interface suggests that we will be editing the content of the larger control on the form(RichTextBox).
So now we have to make those controls do something. If you double click on any of the buttons, Visual Basic will generate a sub routine(code block) for you(that handles the most common user-raised event for that control).
Inside this sub routine is where you will write the instructions of what you want the program to do when the user interacts with that control. Below is an example image of what will be generated when you double click the ‘Open File’ button (in design mode).
Access Modifiers are what determines the level of visibulity for that particular method(this applies to objects as well).
Access Modifiers Definitions
Public – Unrestricted: Any code that can see a public element can access it
Protected – Derivational: Code in the class that declares a protected element, or a class derived from it, can access the element
Friend – Assembly: Code in the assembly that declares a friend element can access it
Protected Friend – Union of Protected and Friend: Code in the same class or the same assembly as a protected
friend element, or within any class derived from the element’s class, can access it
Private – Declaration context: Code in the type that declares a private element, including code within contained types, can access the element
Type Of Method
There are two basic types of methods. Methods that return a value, and methods that do not return a value.
A method that returns a value is called a Function. A Method that does not return a value is called a Subroutine or Sub.
The method name is how the method will be referred to when you desire to execute it.
there are only a few basic rules when it comes to naming a method:
1.)Method must start with a letter or an underscore. 2.) Method May only contain letters, numbers, or an underscore
There are standards and practices for naming in Visual Basic, to learn more about this, visit this link:
These are values that the method requires in order to do its work. You cannot add a handles clause for an event to a method if they do not have matching signatures, invalid data will be passed to the method causing errors.
This can only be used if a control has been declared ‘WithEvents’. Example:
Friend WithEvents Button1 As New System.Windows.Forms.Button
The above example can utilize the Handles Clause feature for assigning an Event handler
Public Button1 As New System.Windows.Forms.Button
The above example cannot utilize the Handles Clause feature for assigning an Event handler, AddHandler must be used.
In Visual Basic you can refer to an event by typing the name of an object and after that when you press the decimal sign at the end of the name of the object, available properties, methods, and events will be listed in a listbox.
The events are the ones with the lightening bolts next to them.
Method Ending Statement
This is pretty cut and dry, if your method is a function then your method ending statement will be End Function. If your method is a sub-routine, then your method ending statement will be End Sub
This was a brief covering from drawing an interface up to the point where you double click the controls in design mode to start coding.